From 0e6a883b4b0f657f4aca952f139eb0d23651de79 Mon Sep 17 00:00:00 2001 From: m-muxfeld-diw <143803170+m-muxfeld-diw@users.noreply.github.com> Date: Wed, 4 Sep 2024 08:56:32 +0200 Subject: [PATCH] PISHPS-304: added OrderTagService (#821) * PISHPS-304: added OrderTagService * PISHPS-304: made Migration1725347559MollieTags sw 6.4 compatible * PISHPS-304: refactored Migration1725347559MollieTags::createTag it no longer uses the queryBuilder * PISHPS-304: OrderTagService now searches the tag by name --- .../Subscription/Actions/CreateAction.php | 54 ++++++++++- .../Migration1725347559MollieTags.php | 64 +++++++++++++ src/Resources/config/services.xml | 5 + .../config/services/subscription/services.xml | 1 + .../Exceptions/CouldNotTagOrderException.php | 19 ++++ src/Service/Tags/OrderTagService.php | 92 +++++++++++++++++++ src/Struct/Tags/AbstractTag.php | 45 +++++++++ src/Struct/Tags/SubscriptionTag.php | 15 +++ 8 files changed, 294 insertions(+), 1 deletion(-) create mode 100644 src/Migration/Migration1725347559MollieTags.php create mode 100644 src/Service/Tags/Exceptions/CouldNotTagOrderException.php create mode 100644 src/Service/Tags/OrderTagService.php create mode 100644 src/Struct/Tags/AbstractTag.php create mode 100644 src/Struct/Tags/SubscriptionTag.php diff --git a/src/Components/Subscription/Actions/CreateAction.php b/src/Components/Subscription/Actions/CreateAction.php index 1367f9e72..1685a7ddd 100644 --- a/src/Components/Subscription/Actions/CreateAction.php +++ b/src/Components/Subscription/Actions/CreateAction.php @@ -3,10 +3,23 @@ namespace Kiener\MolliePayments\Components\Subscription\Actions; use Exception; +use Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderEventFactory; +use Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderFactory; use Kiener\MolliePayments\Components\Subscription\Actions\Base\BaseAction; +use Kiener\MolliePayments\Components\Subscription\DAL\Repository\SubscriptionRepository; use Kiener\MolliePayments\Components\Subscription\DAL\Subscription\SubscriptionStatus; +use Kiener\MolliePayments\Components\Subscription\Services\Builder\MollieDataBuilder; +use Kiener\MolliePayments\Components\Subscription\Services\Builder\SubscriptionBuilder; +use Kiener\MolliePayments\Components\Subscription\Services\SubscriptionCancellation\CancellationValidator; +use Kiener\MolliePayments\Components\Subscription\Services\SubscriptionHistory\SubscriptionHistoryHandler; use Kiener\MolliePayments\Components\Subscription\Services\Validator\MixedOrderValidator; +use Kiener\MolliePayments\Gateway\MollieGatewayInterface; +use Kiener\MolliePayments\Service\CustomerService; +use Kiener\MolliePayments\Service\SettingsService; +use Kiener\MolliePayments\Service\Tags\Exceptions\CouldNotTagOrderException; +use Kiener\MolliePayments\Service\Tags\OrderTagService; use Kiener\MolliePayments\Struct\OrderLineItemEntity\OrderLineItemEntityAttributes; +use Psr\Log\LoggerInterface; use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity; use Shopware\Core\Checkout\Order\OrderEntity; use Shopware\Core\System\SalesChannel\SalesChannelContext; @@ -15,7 +28,40 @@ class CreateAction extends BaseAction { private const INITIAL_STATUS = SubscriptionStatus::PENDING; - + /** + * @var OrderTagService + */ + private $orderTagService; + + public function __construct( + SettingsService $pluginSettings, + SubscriptionRepository $repoSubscriptions, + SubscriptionBuilder $subscriptionBuilder, + MollieDataBuilder $mollieRequestBuilder, + CustomerService $customers, + MollieGatewayInterface $gwMollie, + CancellationValidator $cancellationValidator, + FlowBuilderFactory $flowBuilderFactory, + FlowBuilderEventFactory $flowBuilderEventFactory, + SubscriptionHistoryHandler $subscriptionHistory, + LoggerInterface $logger, + OrderTagService $orderTagService + ) { + parent::__construct( + $pluginSettings, + $repoSubscriptions, + $subscriptionBuilder, + $mollieRequestBuilder, + $customers, + $gwMollie, + $cancellationValidator, + $flowBuilderFactory, + $flowBuilderEventFactory, + $subscriptionHistory, + $logger + ); + $this->orderTagService = $orderTagService; + } /** * @param OrderEntity $order @@ -84,6 +130,12 @@ public function createSubscription(OrderEntity $order, SalesChannelContext $cont $this->getStatusHistory()->markCreated($subscription, self::INITIAL_STATUS, $context->getContext()); + try { + $this->orderTagService->addTagToSubscriptionOrder($subscription, $context->getContext()); + } catch (CouldNotTagOrderException $exception) { + $this->getLogger()->error('Could not tag order with subscription: ' . $exception->getMessage()); + } + return $subscription->getId(); } } diff --git a/src/Migration/Migration1725347559MollieTags.php b/src/Migration/Migration1725347559MollieTags.php new file mode 100644 index 000000000..1450f70fb --- /dev/null +++ b/src/Migration/Migration1725347559MollieTags.php @@ -0,0 +1,64 @@ +createTag($connection, $tag->getId(), $tag->getName()); + } + } + + /** + * @param Connection $connection + * @return void + */ + public function updateDestructive(Connection $connection): void + { + // implement update destructive + } + + private function createTag( + Connection $connection, + string $id, + string $name + ): void { + $query = <<prepare($query); + + $parameters = [ + 'id' => Uuid::fromHexToBytes($id), + 'name' => $name, + 'created_at' => (new \DateTime())->format('Y-m-d H:i:s'), + 'updated_at' => null, + ]; + + $stmt->execute($parameters); + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 0d4d82a60..cbdc52a45 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -231,6 +231,11 @@ + + + + + diff --git a/src/Resources/config/services/subscription/services.xml b/src/Resources/config/services/subscription/services.xml index 28bcafe53..10072ff3e 100755 --- a/src/Resources/config/services/subscription/services.xml +++ b/src/Resources/config/services/subscription/services.xml @@ -115,6 +115,7 @@ + diff --git a/src/Service/Tags/Exceptions/CouldNotTagOrderException.php b/src/Service/Tags/Exceptions/CouldNotTagOrderException.php new file mode 100644 index 000000000..c3c640348 --- /dev/null +++ b/src/Service/Tags/Exceptions/CouldNotTagOrderException.php @@ -0,0 +1,19 @@ +orderRepository = $orderRepository; + $this->tagRepository = $tagRepository; + } + + /** + * @throws CouldNotTagOrderException + */ + public function addTagToSubscriptionOrder(SubscriptionEntity $entity, Context $context): void + { + $orderId = $entity->getOrderId(); + $subscriptionTag = SubscriptionTag::create(); + + // Fetch the order + $criteria = new Criteria([$orderId]); + $criteria->addAssociation('tags'); + + /** @var null|OrderEntity $order */ + $order = $this->orderRepository->search($criteria, $context)->get($orderId); + + if (!$order instanceof OrderEntity) { + throw CouldNotTagOrderException::forSubscription(sprintf('Order with ID "%s" not found', $orderId)); + } + + $criteria = new Criteria(); + $criteria->addFilter(new EqualsFilter('name', $subscriptionTag->getName())); + /** @var null|TagEntity $tag */ + $tag = $this->tagRepository->search($criteria, $context)->first(); + + if (!$tag instanceof TagEntity) { + throw CouldNotTagOrderException::forSubscription(sprintf('Tag with name "%s" and ID "%s" not found', $subscriptionTag->getName(), $subscriptionTag->getId())); + } + + $orderTags = $order->getTags(); + + if (!$orderTags instanceof TagCollection) { + throw CouldNotTagOrderException::forSubscription(sprintf('Order with ID "%s" does not provide its tag collection', $entity->getOrderId())); + } + + $orderTags->add($tag); + + $this->orderRepository->update([ + [ + 'id' => $orderId, + 'tags' => array_map( + Closure::fromCallable([$this, 'serializeTag']), + $orderTags->getElements() + ), + ], + ], $context); + } + + /** + * @return array + */ + private function serializeTag(TagEntity $tag): array + { + return ['id' => $tag->getId()]; + } +} diff --git a/src/Struct/Tags/AbstractTag.php b/src/Struct/Tags/AbstractTag.php new file mode 100644 index 000000000..ace672cb2 --- /dev/null +++ b/src/Struct/Tags/AbstractTag.php @@ -0,0 +1,45 @@ +name = $name; + $this->id = $id; + } + + abstract public static function create(): self; + + /** + * @return static + */ + protected static function createObject(string $name, string $id): self + { + return new static($name, $id); + } + + public function getName(): string + { + return $this->name; + } + + public function getId(): string + { + return $this->id; + } +} diff --git a/src/Struct/Tags/SubscriptionTag.php b/src/Struct/Tags/SubscriptionTag.php new file mode 100644 index 000000000..53aa554b0 --- /dev/null +++ b/src/Struct/Tags/SubscriptionTag.php @@ -0,0 +1,15 @@ +