custom/plugins/MoorlFoundation/src/Core/Subscriber/MoorlFoundationSubscriber.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFoundation\Core\Subscriber;
  3. use MoorlFoundation\Core\Service\TranslationService;
  4. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
  6. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  7. use Shopware\Core\Checkout\Order\OrderDefinition;
  8. use Shopware\Core\Checkout\Order\OrderStates;
  9. use Shopware\Core\Content\Media\Event\MediaFileExtensionWhitelistEvent;
  10. use Shopware\Core\Defaults;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  13. use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions;
  14. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  15. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  16. use Shopware\Core\System\StateMachine\Transition;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class MoorlFoundationSubscriber implements EventSubscriberInterface
  20. {
  21.     private SystemConfigService $systemConfigService;
  22.     private TranslationService $translationService;
  23.     private StateMachineRegistry $stateMachineRegistry;
  24.     public function __construct(
  25.         SystemConfigService $systemConfigService,
  26.         TranslationService $translationService,
  27.         StateMachineRegistry $stateMachineRegistry
  28.     )
  29.     {
  30.         $this->systemConfigService $systemConfigService;
  31.         $this->translationService $translationService;
  32.         $this->stateMachineRegistry $stateMachineRegistry;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             MediaFileExtensionWhitelistEvent::class => 'onMediaFileExtensionWhitelist',
  38.             EntityWrittenContainerEvent::class => 'onEntityWrittenContainerEvent',
  39.             'state_enter.order_transaction.state.cancelled' => 'stateChanged',
  40.             'state_enter.order_transaction.state.failed' => 'stateChanged',
  41.         ];
  42.     }
  43.     public function onMediaFileExtensionWhitelist(MediaFileExtensionWhitelistEvent $event)
  44.     {
  45.         $whitelist $event->getWhitelist();
  46.         $whitelistConfig $this->systemConfigService->get('MoorlFoundation.config.fileExtensions');
  47.         if ($whitelistConfig) {
  48.             $whitelistConfig explode(","$whitelistConfig);
  49.             $whitelistConfig array_map('trim'$whitelistConfig);
  50.             if (is_array($whitelistConfig)) {
  51.                 $whitelist array_merge($whitelist$whitelistConfig);
  52.             }
  53.         }
  54.         $event->setWhitelist($whitelist);
  55.     }
  56.     public function onEntityWrittenContainerEvent(EntityWrittenContainerEvent $event): void
  57.     {
  58.         foreach ($event->getEvents() as $entityWrittenEvent) {
  59.             if ($entityWrittenEvent instanceof EntityWrittenEvent) {
  60.                 $this->translationService->translate($entityWrittenEvent->getEntityName(), $entityWrittenEvent->getWriteResults(), $entityWrittenEvent->getContext());
  61.             }
  62.         }
  63.     }
  64.     public function stateChanged(OrderStateMachineStateChangeEvent $event): void
  65.     {
  66.         if (!$this->systemConfigService->get('MoorlFoundation.config.orderAutoCancel')) {
  67.             return;
  68.         }
  69.         $order $event->getOrder();
  70.         $this->stateMachineRegistry->transition(new Transition(
  71.             OrderDefinition::ENTITY_NAME,
  72.             $order->getId(),
  73.             StateMachineTransitionActions::ACTION_CANCEL,
  74.             'stateId'
  75.         ), $event->getContext());
  76.     }
  77. }