123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?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())) {
- $actionRepository = $this->getEntityManager()->getRepository('WorkflowBundle:Action');
- $actions = $actionRepository->findAllByEventAndObjectClass($eventName, $entityClass);
- foreach ($actions as $action) {
- $routing_key = "";
- if(getenv("AMQP_KEY") !== false){
- $routing_key = getenv("AMQP_KEY");
- }
- $this->publishMessage($action, $entity, $routing_key);
- }
- }
- }
-
- /**
- * @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())) {
- $routing_key = "";
- if(getenv("AMQP_KEY") !== false){
- $routing_key = getenv("AMQP_KEY");
- }
- $this->publishMessage($action, $entity);
- }
- }
- /**
- * @param Action $action
- * @param Entity $entity
- */
- public function publishMessage(Action $action, $entity, $routing_key = "")
- {
- $entityClass = get_class($entity);
- $actionName = $action->getName();
- $entityId = $entity->getId();
- $msg = array(
- 'id' => uniqid(),
- 'content' => $action->render($entity),
- 'entityClass' => $entityClass,
- 'entityId' => $entityId,
- );
- $this->producer->publish(serialize($msg), $routing_key);
-
- $msg = serialize($msg);
- $logger = $this->serviceContainer->get('logger');
- $logger->info("PUBLISH-RABBIT {$actionName} | {$entityClass}_id_{$entityId} | msg: {$msg}");
- }
- }
|