<?php declare(strict_types=1);
namespace CioSponsoredOrderConfirmation;
use CioSponsoredOrderConfirmation\Event\SponsoredAwaitReleaseInformCustomerEvent;
use CioSponsoredOrderConfirmation\Event\SponsoredAwaitReleaseInformReleaserEvent;
use CioSponsoredOrderConfirmation\Event\SponsoredCancelledEvent;
use CioSponsoredOrderConfirmation\Event\SponsoredReleasedEvent;
use CioSponsoredOrderConfirmation\PaymentHandler\SponsoringCostcenterPaymentHandler;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Order\OrderStates;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Content\MailTemplate\MailTemplateActions;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Event\EventAction\EventActionEntity;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionEntity;
use Shopware\Core\System\StateMachine\StateMachineEntity;
class CioSponsoredOrderConfirmation extends Plugin
{
const SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME = 'sponsored_await_release';
const SPONSORED_ORDER_AWAIT_RELEASE_STATE_TRANSITION_NAME = 'sponsored_await_release_transition';
const SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_CUSTOMER_TECHNICAL_NAME = 'sponsored_await_release_mail_to_customer';
const SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_RELEASER_TECHNICAL_NAME = 'sponsored_await_release_mail_to_releaser';
const SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME = 'sponsored_released';
const SPONSORED_ORDER_RELEASED_STATE_TRANSITION_NAME = 'sponsored_release_transition';
const SPONSORED_ORDER_RELEASED_MAIL_TO_CUSTOMER_TECHNICAL_NAME = 'sponsored_released_mail_to_customer';
const SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME = 'sponsored_cancelled';
const SPONSORED_ORDER_CANCEL_STATE_TRANSITION_NAME = 'sponsored_cancel_transition';
const SPONSORED_ORDER_CANCEL_MAIL_TO_CUSTOMER_TECHNICAL_NAME = 'sponsored_cancel_mail_to_customer';
const CUSTOM_FIELD_PRODUCT_SPONSORED_FLAG = 'custom_products_sponsored_flag';
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createCustomFields();
$this->createOrderState($installContext);
$this->addEmailTemplates($installContext);
$this->addBusinessEvents($installContext);
$this->addPaymentMethod($installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
//Keep UserData? Then do nothing here
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeEmailTypeWithTemplates(self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_CUSTOMER_TECHNICAL_NAME, $uninstallContext->getContext());
$this->removeEmailTypeWithTemplates(self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_RELEASER_TECHNICAL_NAME, $uninstallContext->getContext());
$this->removeEmailTypeWithTemplates(self::SPONSORED_ORDER_RELEASED_MAIL_TO_CUSTOMER_TECHNICAL_NAME, $uninstallContext->getContext());
$this->removeEmailTypeWithTemplates(self::SPONSORED_ORDER_CANCEL_MAIL_TO_CUSTOMER_TECHNICAL_NAME, $uninstallContext->getContext());
}
protected function createCustomFields()
{
/** @var EntityRepositoryInterface $customFieldRepository */
$customFieldRepository = $this->container->get('custom_field.repository');
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetEntity = $customFieldSetRepository->search(
(new Criteria())->addFilter(new EqualsFilter('name', 'custom_produktfields'))
, Context::createDefaultContext()
)->first();
if ($customFieldSetEntity instanceof CustomFieldSetEntity) {
$existingCustomFieldEntity = $customFieldRepository->search(
(new Criteria())
->addFilter(new EqualsFilter('name', self::CUSTOM_FIELD_PRODUCT_SPONSORED_FLAG))
->addFilter(new EqualsFilter('customFieldSetId', $customFieldSetEntity->getId())),
Context::createDefaultContext()
)->first();
if (is_null($existingCustomFieldEntity)) {
$customFieldRepository->create([
[
'name' => self::CUSTOM_FIELD_PRODUCT_SPONSORED_FLAG,
'type' => 'bool',
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Sponsored-Artikel',
'en-GB' => 'Sponsored-Article'
],
'componentName' => 'sw-field',
'customFieldType' => 'switch'
],
'active' => true,
'customFieldSetId' => $customFieldSetEntity->getId()
]
], Context::createDefaultContext());
}
}
}
protected function createOrderState(InstallContext $context)
{
/** @var EntityRepositoryInterface $stateMachineRepository */
$stateMachineRepository = $this->container->get('state_machine.repository');
/** @var EntityRepositoryInterface $stateMachineStateRepository */
$stateMachineStateRepository = $this->container->get('state_machine_state.repository');
/** @var EntityRepositoryInterface $stateMachineTransitionRepository */
$stateMachineTransitionRepository = $this->container->get('state_machine_transition.repository');
/** @var StateMachineEntity $stateMachine */
$stateMachine = $this->getStateMachine($stateMachineRepository, $context->getContext());
$this->createAwaitRelease($stateMachine, $stateMachineStateRepository, $context, $stateMachineTransitionRepository);
$this->createReleased($stateMachine, $stateMachineStateRepository, $context, $stateMachineTransitionRepository);
$this->createCancel($stateMachine, $stateMachineStateRepository, $context, $stateMachineTransitionRepository);
}
protected function addEmailTemplates(InstallContext $context)
{
$mailTemplateTypeId = Uuid::randomHex();
$this->createEmailTypeWithTemplates([
'id' => $mailTemplateTypeId,
'name' => '"Freigabe erforderlich"-Email für Sponsoring-Bestellungen an den Freigeber',
'technicalName' => self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_RELEASER_TECHNICAL_NAME,
'availableEntities' => [
'order' => 'order',
'customer' => 'customer'
]
],
[
[
'id' => Uuid::randomHex(),
'mailTemplateTypeId' => $mailTemplateTypeId,
'senderName' => '{{ salesChannel.name }}',
'subject' => [
'de-DE' => 'Die Bestellung {{ order.orderNumber }} befindet sich jetzt im Zustand "Freigabe erforderlich"',
'en-GB' => 'The order {{ order.orderNumber }} is now in the "Release required" state'
],
'contentPlain' => [
'de-DE' => "Es wurde eine neue Bestellung {{ order.orderNumber }} aufgegeben, welche ihre Freigabe erfordert.\n\n{{ releaseUrl }}",
'en-GB' => "A new order {{ order.orderNumber }} has been placed, awaiting its release.\n\n{{ releaseUrl }}"
],
'contentHtml' => [
'de-DE' => '<p>Es wurde eine neue Bestellung {{ order.orderNumber }} aufgegeben, welche ihre Freigabe erwartet.</p><p><a href="{{ releaseUrl }}">{{ releaseUrl }}</a></p>',
'en-GB' => '<p>A new order {{ order.orderNumber }} has been placed, awaiting its release.</p><p><a href="{{ releaseUrl }}">{{ releaseUrl }}</a></p>'
],
]
], $context->getContext());
$mailTemplateTypeId = Uuid::randomHex();
$this->createEmailTypeWithTemplates([
'id' => $mailTemplateTypeId,
'name' => '"Freigabe erforderlich"-Email für Sponsoring-Bestellungen an den Besteller',
'technicalName' => self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_CUSTOMER_TECHNICAL_NAME,
'availableEntities' => [
'order' => 'order',
'customer' => 'customer'
]
],
[
[
'id' => Uuid::randomHex(),
'mailTemplateTypeId' => $mailTemplateTypeId,
'senderName' => '{{ salesChannel.name }}',
'subject' => [
'de-DE' => 'Ihre Bestellung {{ order.orderNumber }} wartet auf die Freigabe',
'en-GB' => 'Your order with number {{ order.orderNumber }} is waiting for release'
],
'contentPlain' => [
'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 }}.",
'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 }}."
],
'contentHtml' => [
'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>',
'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>'
],
]
], $context->getContext());
$mailTemplateTypeId = Uuid::randomHex();
$this->createEmailTypeWithTemplates([
'id' => $mailTemplateTypeId,
'name' => '"Freigabe erteilt"-Email für Sponsoring-Bestellungen an den Besteller',
'technicalName' => self::SPONSORED_ORDER_RELEASED_MAIL_TO_CUSTOMER_TECHNICAL_NAME,
'availableEntities' => [
'order' => 'order',
'customer' => 'customer'
]
],
[
[
'id' => Uuid::randomHex(),
'mailTemplateTypeId' => $mailTemplateTypeId,
'senderName' => '{{ salesChannel.name }}',
'subject' => [
'de-DE' => 'Ihre Bestellung {{ order.orderNumber }} wurde freigegeben',
'en-GB' => 'Your order {{ order.orderNumber }} has been released'
],
'contentPlain' => [
'de-DE' => "Ihrer Bestellung {{ order.orderNumber }} wurde freigegeben.\n\nBei Rückfragen zur Freigabe erreichen Sie Ihren Ansprechpartner unter der Email {{ releaserEmail }}.",
'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 }}."
],
'contentHtml' => [
'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>',
'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>'
],
]
], $context->getContext());
$mailTemplateTypeId = Uuid::randomHex();
$this->createEmailTypeWithTemplates([
'id' => $mailTemplateTypeId,
'name' => '"Freigabe abgelehnt"-Email für Sponsoring-Bestellungen an den Besteller',
'technicalName' => self::SPONSORED_ORDER_CANCEL_MAIL_TO_CUSTOMER_TECHNICAL_NAME,
'availableEntities' => [
'order' => 'order',
'customer' => 'customer'
]
],
[
[
'id' => Uuid::randomHex(),
'mailTemplateTypeId' => $mailTemplateTypeId,
'senderName' => '{{ salesChannel.name }}',
'subject' => [
'de-DE' => 'Ihre Bestellung {{ order.orderNumber }} wurde abgelehnt',
'en-GB' => 'Your order {{ order.orderNumber }} has been rejected'
],
'contentPlain' => [
'de-DE' => "Ihrer Bestellung {{ order.orderNumber }} wurde abgelehnt.\n\nBei Rückfragen zur Freigabe erreichen Sie Ihren Ansprechpartner unter der Email {{ releaserEmail }}.",
'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 }}."
],
'contentHtml' => [
'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>',
'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>'
],
]
], $context->getContext());
}
protected function addBusinessEvents(InstallContext $context)
{
$mailType = $this->getEmailType(self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_RELEASER_TECHNICAL_NAME, $context->getContext());
$this->createBusinessEvent(
[
'id' => Uuid::randomHex(),
'eventName' => SponsoredAwaitReleaseInformReleaserEvent::EVENT_NAME,
'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
'config' => [
'mail_template_type_id' => $mailType->getId(),
'mail_template_id' => $mailType->getMailTemplates()->first()->getId(),
],
'title' => $mailType->getName()
], $context->getContext());
$mailType = $this->getEmailType(self::SPONSORED_ORDER_AWAIT_RELEASE_MAIL_TO_CUSTOMER_TECHNICAL_NAME, $context->getContext());
$this->createBusinessEvent(
[
'id' => Uuid::randomHex(),
'eventName' => SponsoredAwaitReleaseInformCustomerEvent::EVENT_NAME,
'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
'config' => [
'mail_template_type_id' => $mailType->getId(),
'mail_template_id' => $mailType->getMailTemplates()->first()->getId(),
],
'title' => $mailType->getName()
], $context->getContext());
$mailType = $this->getEmailType(self::SPONSORED_ORDER_RELEASED_MAIL_TO_CUSTOMER_TECHNICAL_NAME, $context->getContext());
$this->createBusinessEvent(
[
'id' => Uuid::randomHex(),
'eventName' => SponsoredReleasedEvent::EVENT_NAME,
'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
'config' => [
'mail_template_type_id' => $mailType->getId(),
'mail_template_id' => $mailType->getMailTemplates()->first()->getId(),
],
'title' => $mailType->getName()
], $context->getContext());
$mailType = $this->getEmailType(self::SPONSORED_ORDER_CANCEL_MAIL_TO_CUSTOMER_TECHNICAL_NAME, $context->getContext());
$this->createBusinessEvent(
[
'id' => Uuid::randomHex(),
'eventName' => SponsoredCancelledEvent::EVENT_NAME,
'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
'config' => [
'mail_template_type_id' => $mailType->getId(),
'mail_template_id' => $mailType->getMailTemplates()->first()->getId(),
],
'title' => $mailType->getName()
], $context->getContext());
}
private function addPaymentMethod(Context $context): void
{
$paymentMethodExists = $this->getPaymentMethodId();
// Payment method exists already, no need to continue here
if ($paymentMethodExists) {
return;
}
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$pluginId = $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
$examplePaymentData = [
// payment handler will be selected by the identifier
'handlerIdentifier' => SponsoringCostcenterPaymentHandler::class,
'name' => 'Kostenstelle Sponsoring',
'description' => '',
'pluginId' => $pluginId,
];
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentRepository->create([$examplePaymentData], $context);
}
private function getPaymentMethodId(): ?string
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
// Fetch ID for update
$paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier', SponsoringCostcenterPaymentHandler::class));
return $paymentRepository->searchIds($paymentCriteria, Context::createDefaultContext())->firstId();
}
protected function getStateMachine(EntityRepositoryInterface $stateMachineRepository, Context $context): ?StateMachineEntity
{
$stateMachineCriteria = new Criteria();
$stateMachineCriteria->addFilter(new EqualsFilter('technicalName', 'order.state'));
return $stateMachineRepository->search($stateMachineCriteria, $context)->first();
}
protected function getStateMachineState($technicalName, $stateMachineId, EntityRepositoryInterface $stateMachineStateRepository, Context $context): ?StateMachineStateEntity
{
$stateMachineStateCriteria = new Criteria();
$stateMachineStateCriteria->addFilter(new EqualsFilter('stateMachineId', $stateMachineId));
$stateMachineStateCriteria->addFilter(new EqualsFilter('technicalName', $technicalName));
return $stateMachineStateRepository->search($stateMachineStateCriteria, $context)->first();
}
protected function getStateMachineTransition(array $newStateMachineTransition, EntityRepositoryInterface $stateMachineTransitionRepository, Context $context): ?StateMachineTransitionEntity
{
$stateMachineTransitionCriteria = new Criteria();
$stateMachineTransitionCriteria->addFilter(new EqualsFilter('actionName', $newStateMachineTransition['actionName']));
$stateMachineTransitionCriteria->addFilter(new EqualsFilter('stateMachineId', $newStateMachineTransition['stateMachineId']));
$stateMachineTransitionCriteria->addFilter(new EqualsFilter('fromStateId', $newStateMachineTransition['fromStateId']));
$stateMachineTransitionCriteria->addFilter(new EqualsFilter('toStateId', $newStateMachineTransition['toStateId']));
return $stateMachineTransitionRepository->search($stateMachineTransitionCriteria, $context)->first();
}
protected function createEmailTypeWithTemplates(array $type, array $templates, Context $context)
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
try {
$mailTemplateTypeRepository->create([$type], $context);
$mailTemplateRepository->create($templates, $context);
} catch (UniqueConstraintViolationException $exception) {
// No, we've already installed the mails, it's fine.
}
}
protected function removeEmailTypeWithTemplates(string $typeName, Context $context)
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
/** @var MailTemplateTypeEntity $myCustomMailTemplateType */
$myCustomMailTemplateType = $mailTemplateTypeRepository->search(
(new Criteria())->addFilter(new EqualsFilter('technicalName', $typeName)), $context
)->first();
$ids = [];
if ($myCustomMailTemplateType instanceof MailTemplateTypeEntity) {
$mailTemplateIds = $mailTemplateRepository->searchIds(
(new Criteria())->addFilter(new EqualsFilter('mailTemplateTypeId', $myCustomMailTemplateType->getId())), $context
)->getIds();
//Get the Ids from the fetched Entities
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $mailTemplateIds);
}
$mailTemplateTypeRepository->delete([
['id' => $myCustomMailTemplateType->getId()]
], $context);
$mailTemplateRepository->delete($ids, $context);
}
protected function getEmailType(string $emailTypeTechnicalName, Context $context): MailTemplateTypeEntity
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
return $mailTemplateTypeRepository->search((new Criteria())->addAssociation('mailTemplates')->addFilter(new EqualsFilter('technicalName', $emailTypeTechnicalName)), $context)->first();
}
protected function createBusinessEvent(array $event, Context $context)
{
/** @var EntityRepositoryInterface $eventActionRepository */
$eventActionRepository = $this->container->get('event_action.repository');
if ($this->hasBusinessEvent($event['eventName'], $event['config'], $context)) {
$event['id'] = $this->getBusinessEvent($event['eventName'], $event['config'], $context)->getId();
$eventActionRepository->update([$event], $context);
} else {
$eventActionRepository->create([$event], $context);
}
}
protected function getBusinessEvent(string $eventName, array $config, Context $context): EventActionEntity
{
/** @var EntityRepositoryInterface $eventActionRepository */
$eventActionRepository = $this->container->get('event_action.repository');
return $eventActionRepository->search((new Criteria())
->addFilter(new EqualsFilter('eventName', $eventName))
->addFilter(new EqualsFilter('config.mail_template_type_id', $config['mail_template_type_id']))
->addFilter(new EqualsFilter('config.mail_template_id', $config['mail_template_id'])), $context)->first();
}
protected function hasBusinessEvent(string $eventName, array $config, Context $context): bool
{
/** @var EntityRepositoryInterface $eventActionRepository */
$eventActionRepository = $this->container->get('event_action.repository');
return ($eventActionRepository->search((new Criteria())
->addFilter(new EqualsFilter('eventName', $eventName))
->addFilter(new EqualsFilter('config.mail_template_type_id', $config['mail_template_type_id']))
->addFilter(new EqualsFilter('config.mail_template_id', $config['mail_template_id'])), $context)->count() > 0);
}
private function createAwaitRelease(StateMachineEntity $stateMachine, EntityRepositoryInterface $stateMachineStateRepository, InstallContext $context, EntityRepositoryInterface $stateMachineTransitionRepository): void
{
$stateMachineState = $this->getStateMachineState(self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext());
if (!$stateMachineState instanceof StateMachineStateEntity) {
$newState = [
'technicalName' => self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME,
'stateMachineId' => $stateMachine->getId(),
'translations' => [
'de-DE' => [
'name' => 'Sponsoring wartet auf Freigabe'
],
'en-GB' => [
'name' => 'Sponsoring awaits release'
]
]
];
$stateMachineStateRepository->create([$newState], $context->getContext());
}
$newStateMachineTransitions = [
[
'actionName' => self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TRANSITION_NAME,
'stateMachineId' => $stateMachine->getId(),
'fromStateId' => $this->getStateMachineState(OrderStates::STATE_OPEN, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext())->getId(),
'toStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext())->getId(),
]
];
foreach ($newStateMachineTransitions as $newStateMachineTransition) {
$stateMachineTransition = $this->getStateMachineTransition($newStateMachineTransition, $stateMachineTransitionRepository, $context->getContext());
if (!$stateMachineTransition instanceof StateMachineTransitionEntity) {
$stateMachineTransitionRepository->create([$newStateMachineTransition], $context->getContext());
}
}
}
private function createReleased(StateMachineEntity $stateMachine, EntityRepositoryInterface $stateMachineStateRepository, InstallContext $context, EntityRepositoryInterface $stateMachineTransitionRepository): void
{
$stateMachineState = $this->getStateMachineState(self::SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext());
if (!$stateMachineState instanceof StateMachineStateEntity) {
$newState = [
'technicalName' => self::SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME,
'stateMachineId' => $stateMachine->getId(),
'translations' => [
'de-DE' => [
'name' => 'Sponsoring wurde freigegeben'
],
'en-GB' => [
'name' => 'Sponsoring released'
]
]
];
$stateMachineStateRepository->create([$newState], $context->getContext());
}
$newStateMachineTransitions = [
[
'actionName' => self::SPONSORED_ORDER_RELEASED_STATE_TRANSITION_NAME,
'stateMachineId' => $stateMachine->getId(),
'fromStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext())->getId(),
'toStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext())->getId(),
]
];
foreach ($newStateMachineTransitions as $newStateMachineTransition) {
$stateMachineTransition = $this->getStateMachineTransition($newStateMachineTransition, $stateMachineTransitionRepository, $context->getContext());
if (!$stateMachineTransition instanceof StateMachineTransitionEntity) {
$stateMachineTransitionRepository->create([$newStateMachineTransition], $context->getContext());
}
}
}
private function createCancel(StateMachineEntity $stateMachine, EntityRepositoryInterface $stateMachineStateRepository, InstallContext $context, EntityRepositoryInterface $stateMachineTransitionRepository): void
{
$stateMachineState = $this->getStateMachineState(self::SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext());
if (!$stateMachineState instanceof StateMachineStateEntity) {
$newState = [
'technicalName' => self::SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME,
'stateMachineId' => $stateMachine->getId(),
'translations' => [
'de-DE' => [
'name' => 'Sponsoring wurde abgelehnt'
],
'en-GB' => [
'name' => 'Sponsoring rejected'
]
]
];
$stateMachineStateRepository->create([$newState], $context->getContext());
}
$newStateMachineTransitions = [
[
'actionName' => self::SPONSORED_ORDER_CANCEL_STATE_TRANSITION_NAME,
'stateMachineId' => $stateMachine->getId(),
'fromStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_RELEASED_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext())->getId(),
'toStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext())->getId(),
],
[
'actionName' => self::SPONSORED_ORDER_CANCEL_STATE_TRANSITION_NAME,
'stateMachineId' => $stateMachine->getId(),
'fromStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_AWAIT_RELEASE_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext())->getId(),
'toStateId' => $this->getStateMachineState(self::SPONSORED_ORDER_CANCEL_STATE_TECHNICAL_NAME, $stateMachine->getId(), $stateMachineStateRepository, $context->getContext())->getId(),
]
];
foreach ($newStateMachineTransitions as $newStateMachineTransition) {
$stateMachineTransition = $this->getStateMachineTransition($newStateMachineTransition, $stateMachineTransitionRepository, $context->getContext());
if (!$stateMachineTransition instanceof StateMachineTransitionEntity) {
$stateMachineTransitionRepository->create([$newStateMachineTransition], $context->getContext());
}
}
}
}