entityManager = $entityManager; $this->engine = $engine; $this->metadataExtractor = $metadataExtractor; $this->registry = $registry; $this->security = $security; } public static function getSubscribedEvents(): array { return [ 'workflow.completed' => ['onCompletedSendNotification', 2048], ]; } /** * Send a notification to:. * * * the dests of the new step; * * the users which subscribed to workflow, on each step, or on final * * **Warning** take care that this method must be executed **after** the dest users are added to * the step (@see{EntityWorkflowStep::addDestUser}). Currently, this is done during * * @see{EntityWorkflowTransitionEventSubscriber::addDests}. */ public function onCompletedSendNotification(Event $event): void { if (!$event->getSubject() instanceof EntityWorkflow) { return; } /** @var EntityWorkflow $entityWorkflow */ $entityWorkflow = $event->getSubject(); /** @var array $dests array of unique values, where keys is the object's hash */ $dests = []; foreach (array_merge( // the subscriber to each step $entityWorkflow->getSubscriberToStep()->toArray(), // the subscriber to final, only if final $entityWorkflow->isFinal() ? $entityWorkflow->getSubscriberToFinal()->toArray() : [], // the dests for the current step $entityWorkflow->getCurrentStep()->getDestUser()->toArray() ) as $dest) { $dests[spl_object_hash($dest)] = $dest; } $place = $this->metadataExtractor->buildArrayPresentationForPlace($entityWorkflow); $workflow = $this->metadataExtractor->buildArrayPresentationForWorkflow( $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName()) ); foreach ($dests as $subscriber) { if ( $this->security->getUser() === $subscriber ) { continue; } $context = [ 'entity_workflow' => $entityWorkflow, 'dest' => $subscriber, 'place' => $place, 'workflow' => $workflow, 'is_dest' => in_array($subscriber->getId(), array_map(static function (User $u) { return $u->getId(); }, $entityWorkflow->futureDestUsers), true), ]; $notification = new Notification(); $notification ->setRelatedEntityId($entityWorkflow->getId()) ->setRelatedEntityClass(EntityWorkflow::class) ->setTitle($this->engine->render('@ChillMain/Workflow/workflow_notification_on_transition_completed_title.fr.txt.twig', $context)) ->setMessage($this->engine->render('@ChillMain/Workflow/workflow_notification_on_transition_completed_content.fr.txt.twig', $context)) ->addAddressee($subscriber); $this->entityManager->persist($notification); } } }