kuvalda-amqp/console/BaseController.php

161 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace dominion\amqp\console;
use Yii;
use dominion\amqp\AmqpHelper;
use dominion\amqp\CustomDynamicModel;
use dominion\amqp\AmqpLoger;
/**
*
*/
abstract class BaseController extends \yii\console\Controller
{
public $projectName = '';
public $queueName = '';
public $sendQueueName = ''; //ключ маршрутизации ответа
public $amqp;
public $errorCode = false;
public $typeRule = 'request';
public $errors = [];
public $resultType;
public $arErrorCode = [];
public function publish($result, $param)
{
$sendParams = [
'id' => uniqid() . '-' . uniqid(),
'requestId' => isset($param['id']) ? $param['id'] : '',
'date' => date(\DateTime::ISO8601),
'success' => true,
];
if (!empty($result))
{
$sendParams['result'] = $result;
$sendParams['resultType'] = $this->resultType;
}
if ($this->errorCode > 0)
{
$sendParams['errorCode'] = $this->errorCode;
$sendParams['errorMessage'] = $this->getErrorMessage();
$sendParams['success'] = false;
}
$type = 'result';
$prefixFile = $this->queueName;
if (!empty($this->sendQueueName))
{
$type = 'producer';
$prefixFile = $this->sendQueueName;
AmqpHelper::publishRaw($this->projectName, $this->sendQueueName, $sendParams);
}
AmqpLoger::log($type, $sendParams, $prefixFile);
return $sendParams;
}
public function sendResult($queue, $envelope, $result)
{
$this->publish($result, json_decode($envelope->getBody(), true));
$this->amqp->ack($queue, $envelope);
}
public abstract function processMessage($params);
public function proccess()
{
$this->amqp = new AmqpHelper;
$this->amqp->setProject($this->projectName);
$this->amqp->getQueueObject($this->queueName)->consume(function($envelope, $queue)
{
$value = null;
try
{
$this->errorCode = null;
$this->errors = null;
$body = $envelope->getBody();
$params = json_decode($body, true);
AmqpLoger::log('consumer', empty($params) ? $body : $params, $this->queueName);
$value = $this->validate($params) ? $this->processMessage($params) : null;
}
catch (\Exception $exception)
{
$this->errorCode = 99;
$this->errors['system'] = $exception->getCode() . ' ' . $exception->getMessage();
// throw $exception;
}
$this->sendResult($queue, $envelope, $value);
});
}
public function validate($params)
{
$rules = [
//Поля сообщения запроса
'request' => [
[['id', 'date', 'object', 'api', 'params'], 'required'],
[['id', 'object'], 'string', 'max' => 255],
[['date'], 'string', 'max' => 50],
[['params'], 'arrayValidate'],
],
//Поля сообщения ответа
'response' => [
[['id', 'requestId', 'date', 'success'], 'required'],
[['id', 'requestId'], 'string', 'max' => 255],
[['date'], 'string', 'max' => 50],
[['success'], 'boolean'],
[['errorCode'], 'integer'],
[['result'], 'arrayValidate'],
],
];
if(isset($params['errorCode']))
{
$errorMessage = $params['errorCode'] . ' ' . isset($this->arErrorCode[$params['errorCode']]) ? $this->arErrorCode[$params['errorCode']] : '';
$this->setErrors([$params['errorCode'] => $errorMessage]);
return false;
}
return $this->validateArryByRules($params, $rules[$this->typeRule]);
}
public function validateArryByRules($array, $rules, $errorPrefix = '')
{
if(!is_array($array))
{
$this->setErrors(['Не верный формат массива'], $errorPrefix);
return false;
}
if($array == array_values($array)) //массив должен быть ассоциативным
{
$this->setErrors(['Не верный формат массива'], $errorPrefix);
return false;
}
$model = CustomDynamicModel::validateData($array, $rules);
if ($model->hasErrors())
{
$this->setErrors($model->errors, $errorPrefix);
return false;
}
return true;
}
public function getErrorMessage()
{
$output = '';
foreach ($this->errors as $key => $value)
{
$output .= $key . ': "' . implode('", "', (array)$value) . '"; ';
}
return $output;
}
public function setErrors($errors, $errorPrefix = '', $code = 98)
{
foreach ($errors as $key => $value)
{
$newKey = empty($errorPrefix) ? $key : ($errorPrefix . '.' . $key);
$this->errors[$newKey] = $value;
}
$this->errorCode = 98;
}
}