custom/plugins/CioSponsoredOrderConfirmation/src/CioSponsoredOrderConfirmation.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CioSponsoredOrderConfirmation;
  3. use CioSponsoredOrderConfirmation\Event\SponsoredAwaitReleaseInformCustomerEvent;
  4. use CioSponsoredOrderConfirmation\Event\SponsoredAwaitReleaseInformReleaserEvent;
  5. use CioSponsoredOrderConfirmation\Event\SponsoredCancelledEvent;
  6. use CioSponsoredOrderConfirmation\Event\SponsoredReleasedEvent;
  7. use CioSponsoredOrderConfirmation\PaymentHandler\SponsoringCostcenterPaymentHandler;
  8. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  9. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  10. use Shopware\Core\Checkout\Order\OrderStates;
  11. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  12. use Shopware\Core\Content\MailTemplate\MailTemplateActions;
  13. use Shopware\Core\Framework\Context;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\Event\EventAction\EventActionEntity;
  18. use Shopware\Core\Framework\Plugin;
  19. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  20. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  21. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  22. use Shopware\Core\Framework\Uuid\Uuid;
  23. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
  24. use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
  25. use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionEntity;
  26. use Shopware\Core\System\StateMachine\StateMachineEntity;
  27. class CioSponsoredOrderConfirmation extends Plugin
  28. {
  29.     const SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME 'sponsored_await_release';
  30.     const SPONSORED_ORDER_AWAIT_RELEASE_STATE_TRANSITION_NAME 'sponsored_await_release_transition';
  31.     const SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_CUSTOMER_TECHNICAL_NAME 'sponsored_await_release_mail_to_customer';
  32.     const SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_RELEASER_TECHNICAL_NAME 'sponsored_await_release_mail_to_releaser';
  33.     const SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME 'sponsored_released';
  34.     const SPONSORED_ORDER_RELEASED_STATE_TRANSITION_NAME 'sponsored_release_transition';
  35.     const SPONSORED_ORDER_RELEASED_MAIL_TO_CUSTOMER_TECHNICAL_NAME 'sponsored_released_mail_to_customer';
  36.     const SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME 'sponsored_cancelled';
  37.     const SPONSORED_ORDER_CANCEL_STATE_TRANSITION_NAME 'sponsored_cancel_transition';
  38.     const SPONSORED_ORDER_CANCEL_MAIL_TO_CUSTOMER_TECHNICAL_NAME 'sponsored_cancel_mail_to_customer';
  39.     const CUSTOM_FIELD_PRODUCT_SPONSORED_FLAG 'custom_products_sponsored_flag';
  40.     public function install(InstallContext $installContext): void
  41.     {
  42.         parent::install($installContext);
  43.         $this->createCustomFields();
  44.         $this->createOrderState($installContext);
  45.         $this->addEmailTemplates($installContext);
  46.         $this->addBusinessEvents($installContext);
  47.         $this->addPaymentMethod($installContext->getContext());
  48.     }
  49.     public function uninstall(UninstallContext $uninstallContext): void
  50.     {
  51.         parent::uninstall($uninstallContext);
  52.         //Keep UserData? Then do nothing here
  53.         if ($uninstallContext->keepUserData()) {
  54.             return;
  55.         }
  56.         $this->removeEmailTypeWithTemplates(self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_CUSTOMER_TECHNICAL_NAME$uninstallContext->getContext());
  57.         $this->removeEmailTypeWithTemplates(self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_RELEASER_TECHNICAL_NAME$uninstallContext->getContext());
  58.         $this->removeEmailTypeWithTemplates(self::SPONSORED_ORDER_RELEASED_MAIL_TO_CUSTOMER_TECHNICAL_NAME$uninstallContext->getContext());
  59.         $this->removeEmailTypeWithTemplates(self::SPONSORED_ORDER_CANCEL_MAIL_TO_CUSTOMER_TECHNICAL_NAME$uninstallContext->getContext());
  60.     }
  61.     protected function createCustomFields()
  62.     {
  63.         /** @var EntityRepositoryInterface $customFieldRepository */
  64.         $customFieldRepository $this->container->get('custom_field.repository');
  65.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  66.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  67.         $customFieldSetEntity $customFieldSetRepository->search(
  68.             (new Criteria())->addFilter(new EqualsFilter('name''custom_produktfields'))
  69.             , Context::createDefaultContext()
  70.         )->first();
  71.         if ($customFieldSetEntity instanceof CustomFieldSetEntity) {
  72.             $existingCustomFieldEntity $customFieldRepository->search(
  73.                 (new Criteria())
  74.                     ->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_PRODUCT_SPONSORED_FLAG))
  75.                     ->addFilter(new EqualsFilter('customFieldSetId'$customFieldSetEntity->getId())),
  76.                 Context::createDefaultContext()
  77.             )->first();
  78.             if (is_null($existingCustomFieldEntity)) {
  79.                 $customFieldRepository->create([
  80.                     [
  81.                         'name' => self::CUSTOM_FIELD_PRODUCT_SPONSORED_FLAG,
  82.                         'type' => 'bool',
  83.                         'config' => [
  84.                             'type' => 'text',
  85.                             'label' => [
  86.                                 'de-DE' => 'Sponsored-Artikel',
  87.                                 'en-GB' => 'Sponsored-Article'
  88.                             ],
  89.                             'componentName' => 'sw-field',
  90.                             'customFieldType' => 'switch'
  91.                         ],
  92.                         'active' => true,
  93.                         'customFieldSetId' => $customFieldSetEntity->getId()
  94.                     ]
  95.                 ], Context::createDefaultContext());
  96.             }
  97.         }
  98.     }
  99.     protected function createOrderState(InstallContext $context)
  100.     {
  101.         /** @var EntityRepositoryInterface $stateMachineRepository */
  102.         $stateMachineRepository $this->container->get('state_machine.repository');
  103.         /** @var EntityRepositoryInterface $stateMachineStateRepository */
  104.         $stateMachineStateRepository $this->container->get('state_machine_state.repository');
  105.         /** @var EntityRepositoryInterface $stateMachineTransitionRepository */
  106.         $stateMachineTransitionRepository $this->container->get('state_machine_transition.repository');
  107.         /** @var StateMachineEntity $stateMachine */
  108.         $stateMachine $this->getStateMachine($stateMachineRepository$context->getContext());
  109.         $this->createAwaitRelease($stateMachine$stateMachineStateRepository$context$stateMachineTransitionRepository);
  110.         $this->createReleased($stateMachine$stateMachineStateRepository$context$stateMachineTransitionRepository);
  111.         $this->createCancel($stateMachine$stateMachineStateRepository$context$stateMachineTransitionRepository);
  112.     }
  113.     protected function addEmailTemplates(InstallContext $context)
  114.     {
  115.         $mailTemplateTypeId Uuid::randomHex();
  116.         $this->createEmailTypeWithTemplates([
  117.             'id' => $mailTemplateTypeId,
  118.             'name' => '"Freigabe erforderlich"-Email für Sponsoring-Bestellungen an den Freigeber',
  119.             'technicalName' => self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_RELEASER_TECHNICAL_NAME,
  120.             'availableEntities' => [
  121.                 'order' => 'order',
  122.                 'customer' => 'customer'
  123.             ]
  124.         ],
  125.             [
  126.                 [
  127.                     'id' => Uuid::randomHex(),
  128.                     'mailTemplateTypeId' => $mailTemplateTypeId,
  129.                     'senderName' => '{{ salesChannel.name }}',
  130.                     'subject' => [
  131.                         'de-DE' => 'Die Bestellung {{ order.orderNumber }} befindet sich jetzt im Zustand "Freigabe erforderlich"',
  132.                         'en-GB' => 'The order {{ order.orderNumber }} is now in the "Release required" state'
  133.                     ],
  134.                     'contentPlain' => [
  135.                         'de-DE' => "Es wurde eine neue Bestellung {{ order.orderNumber }} aufgegeben, welche ihre Freigabe erfordert.\n\n{{ releaseUrl }}",
  136.                         'en-GB' => "A new order {{ order.orderNumber }} has been placed, awaiting its release.\n\n{{ releaseUrl }}"
  137.                     ],
  138.                     'contentHtml' => [
  139.                         'de-DE' => '<p>Es wurde eine neue Bestellung {{ order.orderNumber }} aufgegeben, welche ihre Freigabe erwartet.</p><p><a href="{{ releaseUrl }}">{{ releaseUrl }}</a></p>',
  140.                         'en-GB' => '<p>A new order {{ order.orderNumber }} has been placed, awaiting its release.</p><p><a href="{{ releaseUrl }}">{{ releaseUrl }}</a></p>'
  141.                     ],
  142.                 ]
  143.             ], $context->getContext());
  144.         $mailTemplateTypeId Uuid::randomHex();
  145.         $this->createEmailTypeWithTemplates([
  146.             'id' => $mailTemplateTypeId,
  147.             'name' => '"Freigabe erforderlich"-Email für Sponsoring-Bestellungen an den Besteller',
  148.             'technicalName' => self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_CUSTOMER_TECHNICAL_NAME,
  149.             'availableEntities' => [
  150.                 'order' => 'order',
  151.                 'customer' => 'customer'
  152.             ]
  153.         ],
  154.             [
  155.                 [
  156.                     'id' => Uuid::randomHex(),
  157.                     'mailTemplateTypeId' => $mailTemplateTypeId,
  158.                     'senderName' => '{{ salesChannel.name }}',
  159.                     'subject' => [
  160.                         'de-DE' => 'Ihre Bestellung {{ order.orderNumber }} wartet auf die Freigabe',
  161.                         'en-GB' => 'Your order with number {{ order.orderNumber }} is waiting for release'
  162.                     ],
  163.                     'contentPlain' => [
  164.                         'de-DE' => "Ihrer Bestellung {{ order.orderNumber }} muss erst noch die Freigabe erteilt werden.\n\nBei Rückfragen zur Freigabe erreichen Sie Ihren Ansprechpartner unter der Email {{ releaserEmail }}.",
  165.                         'en-GB' => "Your order {{ order.orderNumber }} has yet to be released.\n\nIf you have any questions about the release, you can reach your contact person at email {{ releaserEmail }}."
  166.                     ],
  167.                     'contentHtml' => [
  168.                         'de-DE' => '<p>Ihrer Bestellung {{ order.orderNumber }} muss erst noch die Freigabe erteilt werden.</p><p>Bei Rückfragen zur Freigabe erreichen Sie Ihren Ansprechpartner unter der Email <a href="mailto:{{ releaserEmail }}">{{ releaserEmail }}</a>.</p>',
  169.                         'en-GB' => '<p>Your order {{ order.orderNumber }} has yet to be released.</p><p>If you have any questions about the release, you can reach your contact person at email <a href="mailto:{{ releaserEmail }}">{ releaserEmail }}</a>.</p>'
  170.                     ],
  171.                 ]
  172.             ], $context->getContext());
  173.         $mailTemplateTypeId Uuid::randomHex();
  174.         $this->createEmailTypeWithTemplates([
  175.             'id' => $mailTemplateTypeId,
  176.             'name' => '"Freigabe erteilt"-Email für Sponsoring-Bestellungen an den Besteller',
  177.             'technicalName' => self::SPONSORED_ORDER_RELEASED_MAIL_TO_CUSTOMER_TECHNICAL_NAME,
  178.             'availableEntities' => [
  179.                 'order' => 'order',
  180.                 'customer' => 'customer'
  181.             ]
  182.         ],
  183.             [
  184.                 [
  185.                     'id' => Uuid::randomHex(),
  186.                     'mailTemplateTypeId' => $mailTemplateTypeId,
  187.                     'senderName' => '{{ salesChannel.name }}',
  188.                     'subject' => [
  189.                         'de-DE' => 'Ihre Bestellung {{ order.orderNumber }} wurde freigegeben',
  190.                         'en-GB' => 'Your order {{ order.orderNumber }} has been released'
  191.                     ],
  192.                     'contentPlain' => [
  193.                         'de-DE' => "Ihrer Bestellung {{ order.orderNumber }} wurde freigegeben.\n\nBei Rückfragen zur Freigabe erreichen Sie Ihren Ansprechpartner unter der Email {{ releaserEmail }}.",
  194.                         'en-GB' => "Your order {{ order.orderNumber }} has been released.\n\nIf you have any questions about the release, you can reach your contact person at the email {{ releaserEmail }}."
  195.                     ],
  196.                     'contentHtml' => [
  197.                         'de-DE' => '<p>Ihrer Bestellung {{ order.orderNumber }} wurde freigegeben.</p><p>Bei Rückfragen zur Freigabe erreichen Sie Ihren Ansprechpartner unter der Email <a href="mailto:{{ releaserEmail }}">{{ releaserEmail }}</a>.</p>',
  198.                         'en-GB' => '<p>Your order number {{ order.orderNumber }} has been released.</p><p>If you have any questions about the release, you can reach your contact person at email <a href="mailto:{{ releaserEmail }}">{ releaserEmail }}</a>.</p>'
  199.                     ],
  200.                 ]
  201.             ], $context->getContext());
  202.         $mailTemplateTypeId Uuid::randomHex();
  203.         $this->createEmailTypeWithTemplates([
  204.             'id' => $mailTemplateTypeId,
  205.             'name' => '"Freigabe abgelehnt"-Email für Sponsoring-Bestellungen an den Besteller',
  206.             'technicalName' => self::SPONSORED_ORDER_CANCEL_MAIL_TO_CUSTOMER_TECHNICAL_NAME,
  207.             'availableEntities' => [
  208.                 'order' => 'order',
  209.                 'customer' => 'customer'
  210.             ]
  211.         ],
  212.             [
  213.                 [
  214.                     'id' => Uuid::randomHex(),
  215.                     'mailTemplateTypeId' => $mailTemplateTypeId,
  216.                     'senderName' => '{{ salesChannel.name }}',
  217.                     'subject' => [
  218.                         'de-DE' => 'Ihre Bestellung {{ order.orderNumber }} wurde abgelehnt',
  219.                         'en-GB' => 'Your order {{ order.orderNumber }} has been rejected'
  220.                     ],
  221.                     'contentPlain' => [
  222.                         'de-DE' => "Ihrer Bestellung {{ order.orderNumber }} wurde abgelehnt.\n\nBei Rückfragen zur Freigabe erreichen Sie Ihren Ansprechpartner unter der Email {{ releaserEmail }}.",
  223.                         'en-GB' => "Your order {{ order.orderNumber }} has been rejected.\n\nIf you have any questions about the release, you can reach your contact person at the email {{ releaserEmail }}."
  224.                     ],
  225.                     'contentHtml' => [
  226.                         'de-DE' => '<p>Ihrer Bestellung {{ order.orderNumber }} wurde abgelehnt.</p><p>Bei Rückfragen zur Freigabe erreichen Sie Ihren Ansprechpartner unter der Email <a href="mailto:{{ releaserEmail }}">{{ releaserEmail }}</a>.</p>',
  227.                         'en-GB' => '<p>Your order number {{ order.orderNumber }} has been rejected.</p><p>If you have any questions about the release, you can reach your contact person at email <a href="mailto:{{ releaserEmail }}">{ releaserEmail }}</a>.</p>'
  228.                     ],
  229.                 ]
  230.             ], $context->getContext());
  231.     }
  232.     protected function addBusinessEvents(InstallContext $context)
  233.     {
  234.         $mailType $this->getEmailType(self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_RELEASER_TECHNICAL_NAME$context->getContext());
  235.         $this->createBusinessEvent(
  236.             [
  237.                 'id' => Uuid::randomHex(),
  238.                 'eventName' => SponsoredAwaitReleaseInformReleaserEvent::EVENT_NAME,
  239.                 'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
  240.                 'config' => [
  241.                     'mail_template_type_id' => $mailType->getId(),
  242.                     'mail_template_id' => $mailType->getMailTemplates()->first()->getId(),
  243.                 ],
  244.                 'title' => $mailType->getName()
  245.             ], $context->getContext());
  246.         $mailType $this->getEmailType(self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_CUSTOMER_TECHNICAL_NAME$context->getContext());
  247.         $this->createBusinessEvent(
  248.             [
  249.                 'id' => Uuid::randomHex(),
  250.                 'eventName' => SponsoredAwaitReleaseInformCustomerEvent::EVENT_NAME,
  251.                 'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
  252.                 'config' => [
  253.                     'mail_template_type_id' => $mailType->getId(),
  254.                     'mail_template_id' => $mailType->getMailTemplates()->first()->getId(),
  255.                 ],
  256.                 'title' => $mailType->getName()
  257.             ], $context->getContext());
  258.         $mailType $this->getEmailType(self::SPONSORED_ORDER_RELEASED_MAIL_TO_CUSTOMER_TECHNICAL_NAME$context->getContext());
  259.         $this->createBusinessEvent(
  260.             [
  261.                 'id' => Uuid::randomHex(),
  262.                 'eventName' => SponsoredReleasedEvent::EVENT_NAME,
  263.                 'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
  264.                 'config' => [
  265.                     'mail_template_type_id' => $mailType->getId(),
  266.                     'mail_template_id' => $mailType->getMailTemplates()->first()->getId(),
  267.                 ],
  268.                 'title' => $mailType->getName()
  269.             ], $context->getContext());
  270.         $mailType $this->getEmailType(self::SPONSORED_ORDER_CANCEL_MAIL_TO_CUSTOMER_TECHNICAL_NAME$context->getContext());
  271.         $this->createBusinessEvent(
  272.             [
  273.                 'id' => Uuid::randomHex(),
  274.                 'eventName' => SponsoredCancelledEvent::EVENT_NAME,
  275.                 'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
  276.                 'config' => [
  277.                     'mail_template_type_id' => $mailType->getId(),
  278.                     'mail_template_id' => $mailType->getMailTemplates()->first()->getId(),
  279.                 ],
  280.                 'title' => $mailType->getName()
  281.             ], $context->getContext());
  282.     }
  283.     private function addPaymentMethod(Context $context): void
  284.     {
  285.         $paymentMethodExists $this->getPaymentMethodId();
  286.         // Payment method exists already, no need to continue here
  287.         if ($paymentMethodExists) {
  288.             return;
  289.         }
  290.         /** @var PluginIdProvider $pluginIdProvider */
  291.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  292.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  293.         $examplePaymentData = [
  294.             // payment handler will be selected by the identifier
  295.             'handlerIdentifier' => SponsoringCostcenterPaymentHandler::class,
  296.             'name' => 'Kostenstelle Sponsoring',
  297.             'description' => '',
  298.             'pluginId' => $pluginId,
  299.         ];
  300.         /** @var EntityRepositoryInterface $paymentRepository */
  301.         $paymentRepository $this->container->get('payment_method.repository');
  302.         $paymentRepository->create([$examplePaymentData], $context);
  303.     }
  304.     private function getPaymentMethodId(): ?string
  305.     {
  306.         /** @var EntityRepositoryInterface $paymentRepository */
  307.         $paymentRepository $this->container->get('payment_method.repository');
  308.         // Fetch ID for update
  309.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'SponsoringCostcenterPaymentHandler::class));
  310.         return $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  311.     }
  312.     protected function getStateMachine(EntityRepositoryInterface $stateMachineRepositoryContext $context): ?StateMachineEntity
  313.     {
  314.         $stateMachineCriteria = new Criteria();
  315.         $stateMachineCriteria->addFilter(new EqualsFilter('technicalName''order.state'));
  316.         return $stateMachineRepository->search($stateMachineCriteria$context)->first();
  317.     }
  318.     protected function getStateMachineState($technicalName$stateMachineIdEntityRepositoryInterface $stateMachineStateRepositoryContext $context): ?StateMachineStateEntity
  319.     {
  320.         $stateMachineStateCriteria = new Criteria();
  321.         $stateMachineStateCriteria->addFilter(new EqualsFilter('stateMachineId'$stateMachineId));
  322.         $stateMachineStateCriteria->addFilter(new EqualsFilter('technicalName'$technicalName));
  323.         return $stateMachineStateRepository->search($stateMachineStateCriteria$context)->first();
  324.     }
  325.     protected function getStateMachineTransition(array $newStateMachineTransitionEntityRepositoryInterface $stateMachineTransitionRepositoryContext $context): ?StateMachineTransitionEntity
  326.     {
  327.         $stateMachineTransitionCriteria = new Criteria();
  328.         $stateMachineTransitionCriteria->addFilter(new EqualsFilter('actionName'$newStateMachineTransition['actionName']));
  329.         $stateMachineTransitionCriteria->addFilter(new EqualsFilter('stateMachineId'$newStateMachineTransition['stateMachineId']));
  330.         $stateMachineTransitionCriteria->addFilter(new EqualsFilter('fromStateId'$newStateMachineTransition['fromStateId']));
  331.         $stateMachineTransitionCriteria->addFilter(new EqualsFilter('toStateId'$newStateMachineTransition['toStateId']));
  332.         return $stateMachineTransitionRepository->search($stateMachineTransitionCriteria$context)->first();
  333.     }
  334.     protected function createEmailTypeWithTemplates(array $type, array $templatesContext $context)
  335.     {
  336.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  337.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  338.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  339.         $mailTemplateRepository $this->container->get('mail_template.repository');
  340.         try {
  341.             $mailTemplateTypeRepository->create([$type], $context);
  342.             $mailTemplateRepository->create($templates$context);
  343.         } catch (UniqueConstraintViolationException $exception) {
  344.             // No, we've already installed the mails, it's fine.
  345.         }
  346.     }
  347.     protected function removeEmailTypeWithTemplates(string $typeNameContext $context)
  348.     {
  349.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  350.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  351.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  352.         $mailTemplateRepository $this->container->get('mail_template.repository');
  353.         /** @var MailTemplateTypeEntity $myCustomMailTemplateType */
  354.         $myCustomMailTemplateType $mailTemplateTypeRepository->search(
  355.             (new Criteria())->addFilter(new EqualsFilter('technicalName'$typeName)), $context
  356.         )->first();
  357.         $ids = [];
  358.         if ($myCustomMailTemplateType instanceof MailTemplateTypeEntity) {
  359.             $mailTemplateIds $mailTemplateRepository->searchIds(
  360.                 (new Criteria())->addFilter(new EqualsFilter('mailTemplateTypeId'$myCustomMailTemplateType->getId())), $context
  361.             )->getIds();
  362.             //Get the Ids from the fetched Entities
  363.             $ids array_map(static function ($id) {
  364.                 return ['id' => $id];
  365.             }, $mailTemplateIds);
  366.         }
  367.         $mailTemplateTypeRepository->delete([
  368.             ['id' => $myCustomMailTemplateType->getId()]
  369.         ], $context);
  370.         $mailTemplateRepository->delete($ids$context);
  371.     }
  372.     protected function getEmailType(string $emailTypeTechnicalNameContext $context): MailTemplateTypeEntity
  373.     {
  374.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  375.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  376.         return $mailTemplateTypeRepository->search((new Criteria())->addAssociation('mailTemplates')->addFilter(new EqualsFilter('technicalName'$emailTypeTechnicalName)), $context)->first();
  377.     }
  378.     protected function createBusinessEvent(array $eventContext $context)
  379.     {
  380.         /** @var EntityRepositoryInterface $eventActionRepository */
  381.         $eventActionRepository $this->container->get('event_action.repository');
  382.         if ($this->hasBusinessEvent($event['eventName'], $event['config'], $context)) {
  383.             $event['id'] = $this->getBusinessEvent($event['eventName'], $event['config'], $context)->getId();
  384.             $eventActionRepository->update([$event], $context);
  385.         } else {
  386.             $eventActionRepository->create([$event], $context);
  387.         }
  388.     }
  389.     protected function getBusinessEvent(string $eventName, array $configContext $context): EventActionEntity
  390.     {
  391.         /** @var EntityRepositoryInterface $eventActionRepository */
  392.         $eventActionRepository $this->container->get('event_action.repository');
  393.         return $eventActionRepository->search((new Criteria())
  394.             ->addFilter(new EqualsFilter('eventName'$eventName))
  395.             ->addFilter(new EqualsFilter('config.mail_template_type_id'$config['mail_template_type_id']))
  396.             ->addFilter(new EqualsFilter('config.mail_template_id'$config['mail_template_id'])), $context)->first();
  397.     }
  398.     protected function hasBusinessEvent(string $eventName, array $configContext $context): bool
  399.     {
  400.         /** @var EntityRepositoryInterface $eventActionRepository */
  401.         $eventActionRepository $this->container->get('event_action.repository');
  402.         return ($eventActionRepository->search((new Criteria())
  403.                 ->addFilter(new EqualsFilter('eventName'$eventName))
  404.                 ->addFilter(new EqualsFilter('config.mail_template_type_id'$config['mail_template_type_id']))
  405.                 ->addFilter(new EqualsFilter('config.mail_template_id'$config['mail_template_id'])), $context)->count() > 0);
  406.     }
  407.     private function createAwaitRelease(StateMachineEntity $stateMachineEntityRepositoryInterface $stateMachineStateRepositoryInstallContext $contextEntityRepositoryInterface $stateMachineTransitionRepository): void
  408.     {
  409.         $stateMachineState $this->getStateMachineState(self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext());
  410.         if (!$stateMachineState instanceof StateMachineStateEntity) {
  411.             $newState = [
  412.                 'technicalName' => self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME,
  413.                 'stateMachineId' => $stateMachine->getId(),
  414.                 'translations' => [
  415.                     'de-DE' => [
  416.                         'name' => 'Sponsoring wartet auf Freigabe'
  417.                     ],
  418.                     'en-GB' => [
  419.                         'name' => 'Sponsoring awaits release'
  420.                     ]
  421.                 ]
  422.             ];
  423.             $stateMachineStateRepository->create([$newState], $context->getContext());
  424.         }
  425.         $newStateMachineTransitions = [
  426.             [
  427.                 'actionName' => self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TRANSITION_NAME,
  428.                 'stateMachineId' => $stateMachine->getId(),
  429.                 'fromStateId' => $this->getStateMachineState(OrderStates::STATE_OPEN$stateMachine->getId(), $stateMachineStateRepository$context->getContext())->getId(),
  430.                 'toStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext())->getId(),
  431.             ]
  432.         ];
  433.         foreach ($newStateMachineTransitions as $newStateMachineTransition) {
  434.             $stateMachineTransition $this->getStateMachineTransition($newStateMachineTransition$stateMachineTransitionRepository$context->getContext());
  435.             if (!$stateMachineTransition instanceof StateMachineTransitionEntity) {
  436.                 $stateMachineTransitionRepository->create([$newStateMachineTransition], $context->getContext());
  437.             }
  438.         }
  439.     }
  440.     private function createReleased(StateMachineEntity $stateMachineEntityRepositoryInterface $stateMachineStateRepositoryInstallContext $contextEntityRepositoryInterface $stateMachineTransitionRepository): void
  441.     {
  442.         $stateMachineState $this->getStateMachineState(self::SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext());
  443.         if (!$stateMachineState instanceof StateMachineStateEntity) {
  444.             $newState = [
  445.                 'technicalName' => self::SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME,
  446.                 'stateMachineId' => $stateMachine->getId(),
  447.                 'translations' => [
  448.                     'de-DE' => [
  449.                         'name' => 'Sponsoring wurde freigegeben'
  450.                     ],
  451.                     'en-GB' => [
  452.                         'name' => 'Sponsoring released'
  453.                     ]
  454.                 ]
  455.             ];
  456.             $stateMachineStateRepository->create([$newState], $context->getContext());
  457.         }
  458.         $newStateMachineTransitions = [
  459.             [
  460.                 'actionName' => self::SPONSORED_ORDER_RELEASED_STATE_TRANSITION_NAME,
  461.                 'stateMachineId' => $stateMachine->getId(),
  462.                 'fromStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext())->getId(),
  463.                 'toStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext())->getId(),
  464.             ]
  465.         ];
  466.         foreach ($newStateMachineTransitions as $newStateMachineTransition) {
  467.             $stateMachineTransition $this->getStateMachineTransition($newStateMachineTransition$stateMachineTransitionRepository$context->getContext());
  468.             if (!$stateMachineTransition instanceof StateMachineTransitionEntity) {
  469.                 $stateMachineTransitionRepository->create([$newStateMachineTransition], $context->getContext());
  470.             }
  471.         }
  472.     }
  473.     private function createCancel(StateMachineEntity $stateMachineEntityRepositoryInterface $stateMachineStateRepositoryInstallContext $contextEntityRepositoryInterface $stateMachineTransitionRepository): void
  474.     {
  475.         $stateMachineState $this->getStateMachineState(self::SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext());
  476.         if (!$stateMachineState instanceof StateMachineStateEntity) {
  477.             $newState = [
  478.                 'technicalName' => self::SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME,
  479.                 'stateMachineId' => $stateMachine->getId(),
  480.                 'translations' => [
  481.                     'de-DE' => [
  482.                         'name' => 'Sponsoring wurde abgelehnt'
  483.                     ],
  484.                     'en-GB' => [
  485.                         'name' => 'Sponsoring rejected'
  486.                     ]
  487.                 ]
  488.             ];
  489.             $stateMachineStateRepository->create([$newState], $context->getContext());
  490.         }
  491.         $newStateMachineTransitions = [
  492.             [
  493.                 'actionName' => self::SPONSORED_ORDER_CANCEL_STATE_TRANSITION_NAME,
  494.                 'stateMachineId' => $stateMachine->getId(),
  495.                 'fromStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext())->getId(),
  496.                 'toStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext())->getId(),
  497.             ],
  498.             [
  499.                 'actionName' => self::SPONSORED_ORDER_CANCEL_STATE_TRANSITION_NAME,
  500.                 'stateMachineId' => $stateMachine->getId(),
  501.                 'fromStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext())->getId(),
  502.                 'toStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME$stateMachine->getId(), $stateMachineStateRepository$context->getContext())->getId(),
  503.             ]
  504.         ];
  505.         foreach ($newStateMachineTransitions as $newStateMachineTransition) {
  506.             $stateMachineTransition $this->getStateMachineTransition($newStateMachineTransition$stateMachineTransitionRepository$context->getContext());
  507.             if (!$stateMachineTransition instanceof StateMachineTransitionEntity) {
  508.                 $stateMachineTransitionRepository->create([$newStateMachineTransition], $context->getContext());
  509.             }
  510.         }
  511.     }
  512. }