vendor/store.shopware.com/moorlfoundation/src/Core/Subscriber/MoorlFoundationSubscriber.php line 67

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