|
@@ -0,0 +1,97 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace WorkflowBundle\Services;
|
|
|
+
|
|
|
+use OldSound\RabbitMqBundle\RabbitMq\Producer;
|
|
|
+use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
+use WorkflowBundle\Entity\Action;
|
|
|
+use WorkflowBundle\Utils\DoctrineEvents;
|
|
|
+use WorkflowBundle\Utils\WorkFlowEntityClasses;
|
|
|
+
|
|
|
+class ProducerService
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var Producer
|
|
|
+ */
|
|
|
+ private $producer;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var ContainerInterface
|
|
|
+ */
|
|
|
+ private $serviceContainer;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param ContainerInterface $serviceContainer
|
|
|
+ */
|
|
|
+ public function __construct(ContainerInterface $serviceContainer)
|
|
|
+ {
|
|
|
+ $this->serviceContainer = $serviceContainer;
|
|
|
+ /* @var $this->producer Producer */
|
|
|
+ $this->producer = $serviceContainer->get('old_sound_rabbit_mq.flowdat_tasklogger_producer');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return EntityManagerInterface
|
|
|
+ */
|
|
|
+ public function getEntityManager()
|
|
|
+ {
|
|
|
+ return $this->serviceContainer->get('doctrine.orm.entity_manager');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param Entity $entity
|
|
|
+ * @param string $eventName
|
|
|
+ */
|
|
|
+ public function execute($entity, $eventName = DoctrineEvents::PRE_PERSIST)
|
|
|
+ {
|
|
|
+ $entityClass = get_class($entity);
|
|
|
+
|
|
|
+ // la $entity esta dentro de las entidades con workflow
|
|
|
+ if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
|
|
|
+ $doctrine2WorkFlowActionRepository = $this->getEntityManager()->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
|
|
|
+ $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
|
|
|
+ foreach ($doctrine2WorkFlowActions as $doctrine2WorkFlowAction) {
|
|
|
+ $actions = $doctrine2WorkFlowAction->getActions();
|
|
|
+ foreach ($actions as $action) {
|
|
|
+ $this->publishMessage($action, $entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param array $actionName
|
|
|
+ * @param array $entityClass
|
|
|
+ * @param array $entityId
|
|
|
+ */
|
|
|
+ public function executeAction($actionName, $entityClass, $entityId)
|
|
|
+ {
|
|
|
+ $entityManager = $this->getEntityManager();
|
|
|
+ $actionRepository = $entityManager->getRepository('WorkflowBundle:Action');
|
|
|
+ $action = $actionRepository->findOneBy(array(
|
|
|
+ 'name' => $actionName,
|
|
|
+ 'objectClass' => $entityClass,
|
|
|
+ ));
|
|
|
+ $entityRepository = $entityManager->getRepository($entityClass);
|
|
|
+ $entity = $entityRepository->find($entityId);
|
|
|
+ if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
|
|
|
+ $this->publishMessage($action, $entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param Action $action
|
|
|
+ * @param Entity $entity
|
|
|
+ */
|
|
|
+ public function publishMessage(Action $action, $entity)
|
|
|
+ {
|
|
|
+ $msg = array(
|
|
|
+ 'id' => uniqid(),
|
|
|
+ 'content' => $action->render($entity),
|
|
|
+ );
|
|
|
+ $this->producer->publish(serialize($msg));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|