예제 #1
0
 /**
  * @param WorkflowDispatchEventInterface $value
  *
  * @return bool
  * @throws \OldTown\Workflow\ZF2\Toolkit\Validator\Exception\InvalidConvertTypeException
  */
 public function isValid($value)
 {
     if (!$value instanceof WorkflowDispatchEventInterface) {
         $this->error(self::VALIDATE_VALUE_INVALID);
         return false;
     }
     $name = array_key_exists('paramNameInPrepareData', $this->options) ? $this->options['paramNameInPrepareData'] : null;
     if (!is_string($name)) {
         $this->error(self::PARAM_NAME_IN_PREPARE_DATA_INVALID);
         return false;
     }
     $prepareData = $value->getPrepareData();
     if (!array_key_exists($name, $prepareData)) {
         $this->error(self::PARAM_NOT_FOUND_IN_PREPARE_DATA);
         return false;
     }
     $actualValue = $prepareData[$name];
     $typeExpectedValue = array_key_exists('typeExpectedValue', $this->options) ? $this->options['typeExpectedValue'] : 'string';
     $rawExpectedValue = array_key_exists('expectedValue', $this->options) ? $this->options['expectedValue'] : null;
     try {
         $expectedValue = $this->getExpectedValue($rawExpectedValue, $typeExpectedValue);
     } catch (Exception\InvalidConvertTypeException $e) {
         $this->error(self::ERROR_CONVERT_EXPECTED_VALUE);
         return false;
     }
     return $expectedValue === $actualValue;
 }
예제 #2
0
 /**
  * @param WorkflowDispatchEventInterface $value
  *
  * @return bool
  */
 public function isValid($value)
 {
     if (!$value instanceof WorkflowDispatchEventInterface) {
         $this->error(self::VALIDATE_VALUE_INVALID);
         return false;
     }
     if (null === $this->getAllowedHttpMethods()) {
         return true;
     }
     $request = $value->getMvcEvent()->getRequest();
     if (!$request instanceof Request) {
         $this->error(self::REQUEST_INVALID);
         return false;
     }
     /** @var Request  $request*/
     $method = $request->getMethod();
     $method = strtolower($method);
     $allowedHttpMethods = $this->getAllowedHttpMethods();
     if (!array_key_exists($method, $allowedHttpMethods)) {
         $this->error(self::HTTP_METHOD_INVALID);
         return false;
     }
     return true;
 }
예제 #3
0
 /**
  * Запуск workflow
  *
  * @param WorkflowDispatchEventInterface $e
  *
  * @return TransitionResultInterface
  *
  *
  * @throws Exception\InvalidArgumentException
  * @throws  Exception\WorkflowDispatchEventException
  * @throws \OldTown\Workflow\ZF2\ServiceEngine\Exception\InvalidInitializeWorkflowEntryException
  * @throws \OldTown\Workflow\ZF2\ServiceEngine\Exception\DoActionException
  */
 public function onRunWorkflowHandler(WorkflowDispatchEventInterface $e)
 {
     $runWorkflowParam = $e->getRunWorkflowParam();
     $runWorkflowParam->valid();
     $transientVars = $this->factoryTransientVars();
     $prepareData = $e->getPrepareData();
     foreach ($prepareData as $key => $value) {
         $transientVars[$key] = $value;
     }
     $runWorkflowParam = $e->getRunWorkflowParam();
     $runWorkflowParam->valid();
     $workflowManagerName = $runWorkflowParam->getManagerName();
     $workflowActionName = $runWorkflowParam->getActionName();
     $workflowActivity = $runWorkflowParam->getRunType();
     $result = null;
     switch ($runWorkflowParam->getRunType()) {
         case RunWorkflowParamInterface::WORKFLOW_RUN_INITIALIZE:
             $workflowName = $runWorkflowParam->getWorkflowName();
             $result = $this->getWorkflowService()->initialize($workflowManagerName, $workflowName, $workflowActionName, $transientVars);
             break;
         case RunWorkflowParamInterface::WORKFLOW_RUN_TYPE_DO_ACTION:
             $entryId = $runWorkflowParam->getEntryId();
             $result = $this->getWorkflowService()->doAction($workflowManagerName, $entryId, $workflowActionName, $transientVars);
             break;
         default:
             $errMsg = sprintf('Invalid activity %s', $workflowActivity);
             throw new Exception\InvalidArgumentException($errMsg);
     }
     return $result;
 }
 /**
  * Получение метаданных для запуска workflow
  *
  * @param WorkflowDispatchEventInterface $e
  *
  * @return RunWorkflowParam|null
  *
  * @throws Exception\InvalidMetadataException
  * @throws Exception\ResolveEntryIdEventException
  * @throws Exception\InvalidWorkflowManagerAliasException
  * @throws Exception\RuntimeException
  */
 public function onMetadataWorkflowToRun(WorkflowDispatchEventInterface $e)
 {
     $mvcEvent = $e->getMvcEvent();
     $controller = $mvcEvent->getTarget();
     if (!$controller instanceof AbstractController) {
         return null;
     }
     $routeMatch = $mvcEvent->getRouteMatch();
     if (!$routeMatch) {
         return null;
     }
     $action = $routeMatch->getParam('action', 'not-found');
     $actionMethod = AbstractController::getMethodFromAction($action);
     if (!method_exists($controller, $actionMethod)) {
         return null;
     }
     $controllerClassName = get_class($controller);
     $metadata = $this->getMetadataReader()->loadMetadataForAction($controllerClassName, $actionMethod);
     if (!$metadata instanceof MetadataInterface) {
         $errMsg = sprintf('Metadata not implement %s', MetadataInterface::class);
         throw new Exception\InvalidMetadataException($errMsg);
     }
     $workflowManagerNameParam = $metadata->getWorkflowManagerNameRouterParam();
     $workflowManagerNameFromRoute = $routeMatch->getParam($workflowManagerNameParam, null);
     $workflowManagerAliasParam = $metadata->getWorkflowManagerAliasRouterParam();
     $workflowManagerAlias = $routeMatch->getParam($workflowManagerAliasParam, null);
     $workflowManagerName = $workflowManagerNameFromRoute;
     if (null !== $workflowManagerAlias) {
         $workflowService = $this->getWorkflowService();
         if (!$workflowService->hasWorkflowManagerAlias($workflowManagerAlias)) {
             $errMsg = sprintf('Invalid workflow manager alias: %s', $workflowManagerAlias);
             throw new Exception\InvalidWorkflowManagerAliasException($errMsg);
         }
         $workflowManagerNameByAlias = $workflowService->getManagerNameByAlias($workflowManagerAlias);
         if (null !== $workflowManagerNameFromRoute && $workflowManagerNameFromRoute !== $workflowManagerNameByAlias) {
             $errMsg = sprintf('Name collision workflow manager.The name of the alias: %s. The name of the router: %s', $workflowManagerNameByAlias, $workflowManagerNameFromRoute);
             throw new Exception\RuntimeException($errMsg);
         }
         $workflowManagerName = $workflowManagerNameByAlias;
     }
     $workflowActionNameParam = $metadata->getWorkflowActionNameRouterParam();
     $workflowActionName = $routeMatch->getParam($workflowActionNameParam, null);
     if (null === $workflowManagerName || null === $workflowActionName) {
         $this->getLog()->debug('Metadata Workflow to start not found', ['workflowManagerNameFromRoute' => $workflowManagerNameFromRoute, 'workflowManagerAlias' => $workflowManagerAlias, 'workflowManagerName' => $workflowManagerName, 'workflowActionName' => $workflowActionName]);
         return null;
     }
     $runType = $e->getMetadata()->getWorkflowRunType();
     $workflowNameParam = $metadata->getWorkflowNameRouterParam();
     $workflowName = $routeMatch->getParam($workflowNameParam, null);
     $runWorkflowParam = new RunWorkflowParam();
     $runWorkflowParam->setRunType($runType);
     $runWorkflowParam->setManagerName($workflowManagerName);
     $runWorkflowParam->setActionName($workflowActionName);
     if (RunWorkflowParam::WORKFLOW_RUN_INITIALIZE === $runType && null !== $workflowName) {
         $runWorkflowParam->setWorkflowName($workflowName);
         return $runWorkflowParam;
     }
     if (RunWorkflowParam::WORKFLOW_RUN_TYPE_DO_ACTION === $runType) {
         $event = $this->resolveEntryIdEventFactory();
         $event->setWorkflowDispatchEvent($e);
         $event->setRunType($runType);
         $event->setActionName($workflowActionName);
         $event->setManagerName($workflowManagerName);
         $event->setWorkflowName($workflowName);
         $event->setManagerAlias($workflowManagerAlias);
         $resolveEntryIdResults = $this->getEventManager()->trigger(ResolveEntryIdEventInterface::RESOLVE_ENTRY_ID_EVENT, $event, function ($item) {
             return is_numeric($item);
         });
         $resolveEntryIdResult = $resolveEntryIdResults->last();
         if (is_numeric($resolveEntryIdResult)) {
             $runWorkflowParam->setEntryId($resolveEntryIdResult);
             return $runWorkflowParam;
         }
     }
     return null;
 }