vendor/ibexa/shipping/src/bundle/EventSubscriber/Notification/TriggerNotificationsSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\Shipping\EventSubscriber\Notification;
  8. use Ibexa\Contracts\Notifications\Service\NotificationServiceInterface;
  9. use Ibexa\Contracts\Notifications\Value\Notification\SymfonyNotificationAdapter;
  10. use Ibexa\Contracts\Notifications\Value\Recipent\SymfonyRecipientAdapter;
  11. use Ibexa\Contracts\Shipping\Notification\ShipmentStatusChange;
  12. use Ibexa\Contracts\Shipping\Shipment\ShipmentInterface;
  13. use Ibexa\Shipping\Workflow\WorkflowConfigurationInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Notifier\Recipient\Recipient;
  16. use Symfony\Component\Workflow\Event\EnteredEvent;
  17. use Symfony\Component\Workflow\WorkflowEvents;
  18. final class TriggerNotificationsSubscriber implements EventSubscriberInterface
  19. {
  20. private WorkflowConfigurationInterface $workflowConfiguration;
  21. private NotificationServiceInterface $notificationService;
  22. public function __construct(
  23. WorkflowConfigurationInterface $workflowConfiguration,
  24. NotificationServiceInterface $notificationService
  25. ) {
  26. $this->workflowConfiguration = $workflowConfiguration;
  27. $this->notificationService = $notificationService;
  28. }
  29. public static function getSubscribedEvents(): array
  30. {
  31. return [
  32. WorkflowEvents::ENTERED => [['onWorkflowEntered', -10]],
  33. ];
  34. }
  35. public function onWorkflowEntered(EnteredEvent $event): void
  36. {
  37. $workflow = $event->getWorkflow();
  38. $shipmentWorkflow = $this->workflowConfiguration->getConfiguredWorkflowName();
  39. if ($shipmentWorkflow !== $workflow->getName()) {
  40. return;
  41. }
  42. $shipment = $event->getSubject();
  43. if (!$shipment instanceof ShipmentInterface) {
  44. return;
  45. }
  46. $metadata = $workflow
  47. ->getMetadataStore()
  48. ->getPlaceMetadata($shipment->getStatus());
  49. $triggerNotification = $metadata['trigger_notification'] ?? false;
  50. if (!$triggerNotification) {
  51. return;
  52. }
  53. $transition = $event->getTransition();
  54. $marking = $event->getMarking();
  55. $context = $event->getContext();
  56. $user = $shipment->getOrder()->getUser();
  57. $this->notificationService->send(
  58. new SymfonyNotificationAdapter(
  59. new ShipmentStatusChange(
  60. $shipment,
  61. $workflow,
  62. $transition,
  63. $marking,
  64. $context,
  65. )
  66. ),
  67. [new SymfonyRecipientAdapter(new Recipient($user->getEmail()))],
  68. );
  69. }
  70. }