예제 #1
0
 /**
  * Проверка, на то нужно ли запускать workflow
  *
  * @param WorkflowDispatchEventInterface $e
  *
  * @return boolean|null
  *
  * @throws \Zend\Validator\Exception\InvalidArgumentException
  * @throws  Exception\PrepareDataException
  */
 public function onCheckRunWorkflowHandler(WorkflowDispatchEventInterface $e)
 {
     $metadata = $e->getMetadata();
     if (!$metadata->getFlagHasConditions()) {
         return null;
     }
     try {
         $conditions = $metadata->getConditions();
         $validatorManager = $this->getValidatorManager();
         /** @var ValidatorChain $validatorChains */
         $validatorChains = $validatorManager->get(ValidatorChain::class);
     } catch (\Exception $e) {
         throw new Exception\PrepareDataException($e->getMessage(), $e->getCode(), $e);
     }
     $mvcEvent = $e->getMvcEvent();
     $controller = $mvcEvent->getTarget();
     if (!$controller instanceof AbstractController) {
         $controller = null;
     }
     foreach ($conditions as $condition) {
         try {
             $type = $condition->getType();
             $handler = $condition->getHandler();
             switch ($type) {
                 case 'method':
                     if (null === $controller) {
                         $errMsg = 'Controller not specified';
                         throw new Exception\CheckRunWorkflowEventException($errMsg);
                     }
                     $callback = [$controller, $handler];
                     /** @var ValidatorInterface $callbackValidator */
                     $callbackValidator = $validatorManager->get('callback', $callback);
                     $validatorChains->attach($callbackValidator);
                     break;
                 case 'service':
                     $validatorParams = $condition->getParams();
                     /** @var ValidatorInterface $validator */
                     $validator = $validatorManager->get($handler, $validatorParams);
                     $validatorChains->attach($validator);
                     break;
                 default:
                     $errMsg = sprintf('Preparing data for Workflow will fail. Unknown handler type %s.', $type);
                     throw new Exception\PrepareDataException($errMsg);
             }
         } catch (\Exception $e) {
             throw new Exception\PrepareDataException($e->getMessage(), $e->getCode(), $e);
         }
     }
     return $validatorChains->isValid($e);
 }