From 855e75aaa3ef11b6a7ccafa89c0068141419287c Mon Sep 17 00:00:00 2001 From: Marvin Muxfeld Date: Tue, 3 Sep 2024 10:46:22 +0200 Subject: [PATCH 1/4] PISHPS-304: added OrderTagService --- .../Subscription/Actions/CreateAction.php | 54 ++++++++++- .../Migration1725347559MollieTags.php | 69 ++++++++++++++ src/Resources/config/services.xml | 6 ++ .../config/services/subscription/services.xml | 1 + .../Exceptions/CouldNotTagOrderException.php | 19 ++++ src/Service/Tags/OrderTagService.php | 93 +++++++++++++++++++ src/Struct/Tags/AbstractTag.php | 45 +++++++++ src/Struct/Tags/SubscriptionTag.php | 15 +++ 8 files changed, 301 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..d365784e1 --- /dev/null +++ b/src/Migration/Migration1725347559MollieTags.php @@ -0,0 +1,69 @@ +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 { + // Create a new QueryBuilder instance + $queryBuilder = $connection->createQueryBuilder(); + + // Build the INSERT query + $queryBuilder + ->insert('tag') + ->values([ + 'id' => ':id', + 'name' => ':name', + 'created_at' => ':created_at', + 'updated_at' => ':updated_at', + ]) + ->setParameters([ + 'id' => Uuid::fromHexToBytes($id), + 'name' => $name, + 'created_at' => (new \DateTime())->format('Y-m-d H:i:s'), // current timestamp + 'updated_at' => null, + ]); + + /** @phpstan-ignore-next-line for what ever reason stan says this line doesn't exist, but it does */ + $queryBuilder->executeStatement(); + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 5f0063151..d0e819843 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -224,6 +224,12 @@ + + + + + + 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)); + } + + // Fetch or create the tag + $criteria = new Criteria(); + $criteria->addFilter(new EqualsFilter('id', $subscriptionTag->getId())); + /** @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 @@ + Date: Tue, 3 Sep 2024 13:54:45 +0200 Subject: [PATCH 2/4] PISHPS-304: made Migration1725347559MollieTags sw 6.4 compatible --- src/Migration/Migration1725347559MollieTags.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Migration/Migration1725347559MollieTags.php b/src/Migration/Migration1725347559MollieTags.php index d365784e1..d59bd7f33 100644 --- a/src/Migration/Migration1725347559MollieTags.php +++ b/src/Migration/Migration1725347559MollieTags.php @@ -63,7 +63,13 @@ private function createTag( 'updated_at' => null, ]); - /** @phpstan-ignore-next-line for what ever reason stan says this line doesn't exist, but it does */ - $queryBuilder->executeStatement(); + if (method_exists($queryBuilder, 'executeStatement')) { + // Execute the query (Shopware >= 6.4) + $queryBuilder->executeStatement(); + return; + } + + // Execute the query (Shopware < 6.4) + $queryBuilder->execute(); } } From 501da35c7aea416a73a896dcf494f1b7a75294ca Mon Sep 17 00:00:00 2001 From: Marvin Muxfeld Date: Tue, 3 Sep 2024 14:16:40 +0200 Subject: [PATCH 3/4] PISHPS-304: refactored Migration1725347559MollieTags::createTag it no longer uses the queryBuilder --- .../Migration1725347559MollieTags.php | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/src/Migration/Migration1725347559MollieTags.php b/src/Migration/Migration1725347559MollieTags.php index d59bd7f33..1450f70fb 100644 --- a/src/Migration/Migration1725347559MollieTags.php +++ b/src/Migration/Migration1725347559MollieTags.php @@ -44,32 +44,21 @@ private function createTag( string $id, string $name ): void { - // Create a new QueryBuilder instance - $queryBuilder = $connection->createQueryBuilder(); + $query = <<insert('tag') - ->values([ - 'id' => ':id', - 'name' => ':name', - 'created_at' => ':created_at', - 'updated_at' => ':updated_at', - ]) - ->setParameters([ - 'id' => Uuid::fromHexToBytes($id), - 'name' => $name, - 'created_at' => (new \DateTime())->format('Y-m-d H:i:s'), // current timestamp - 'updated_at' => null, - ]); + $stmt = $connection->prepare($query); - if (method_exists($queryBuilder, 'executeStatement')) { - // Execute the query (Shopware >= 6.4) - $queryBuilder->executeStatement(); - return; - } + $parameters = [ + 'id' => Uuid::fromHexToBytes($id), + 'name' => $name, + 'created_at' => (new \DateTime())->format('Y-m-d H:i:s'), + 'updated_at' => null, + ]; - // Execute the query (Shopware < 6.4) - $queryBuilder->execute(); + $stmt->execute($parameters); } } From b02199e16761fffb7f760253ffa3bc75dfa87007 Mon Sep 17 00:00:00 2001 From: Marvin Muxfeld Date: Wed, 4 Sep 2024 08:20:53 +0200 Subject: [PATCH 4/4] PISHPS-304: OrderTagService now searches the tag by name --- src/Service/Tags/OrderTagService.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Service/Tags/OrderTagService.php b/src/Service/Tags/OrderTagService.php index 76e0b3e66..d15a18c8a 100644 --- a/src/Service/Tags/OrderTagService.php +++ b/src/Service/Tags/OrderTagService.php @@ -54,9 +54,8 @@ public function addTagToSubscriptionOrder(SubscriptionEntity $entity, Context $c throw CouldNotTagOrderException::forSubscription(sprintf('Order with ID "%s" not found', $orderId)); } - // Fetch or create the tag $criteria = new Criteria(); - $criteria->addFilter(new EqualsFilter('id', $subscriptionTag->getId())); + $criteria->addFilter(new EqualsFilter('name', $subscriptionTag->getName())); /** @var null|TagEntity $tag */ $tag = $this->tagRepository->search($criteria, $context)->first(); @@ -88,6 +87,6 @@ public function addTagToSubscriptionOrder(SubscriptionEntity $entity, Context $c */ private function serializeTag(TagEntity $tag): array { - return ['id' => $tag->getId(),]; + return ['id' => $tag->getId()]; } }