Skip to content

Commit

Permalink
PISHPS-304: added OrderTagService (#821)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
m-muxfeld-diw authored Sep 4, 2024
1 parent 7594fbe commit 0e6a883
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/Components/Subscription/Actions/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}
64 changes: 64 additions & 0 deletions src/Migration/Migration1725347559MollieTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types=1);

namespace Kiener\MolliePayments\Migration;

use Doctrine\DBAL\Connection;
use Kiener\MolliePayments\Struct\Tags\AbstractTag;
use Kiener\MolliePayments\Struct\Tags\SubscriptionTag;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Migration\MigrationStep;
use Shopware\Core\Framework\Uuid\Uuid;

/**
* @internal
*/
#[Package('core')]
class Migration1725347559MollieTags extends MigrationStep
{
public function getCreationTimestamp(): int
{
return 1725347559;
}

public function update(Connection $connection): void
{
$tags = [SubscriptionTag::create()];

foreach ($tags as $tag) {
/** @var $tag AbstractTag */
$this->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 = <<<SQL
INSERT INTO tag
(id, name, created_at, updated_at)
VALUES (:id, :name, :created_at, :updated_at)
SQL;

$stmt = $connection->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);
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@
<argument type="service" id="Shopware\Core\Content\Mail\Service\MailSender"/>
</service>


<service id="Kiener\MolliePayments\Service\Tags\OrderTagService">
<argument id="order.repository" type="service"/>
<argument id="tag.repository" type="service"/>
</service>
<service id="Kiener\MolliePayments\Checkout\Cart\ExpressCartItemAddRoute" decorates="Shopware\Core\Checkout\Cart\SalesChannel\CartItemAddRoute" public="true">
<argument type="service" id=".inner"/>
<argument type="service" id="service_container"/>
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/subscription/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<argument type="service" id="Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderEventFactory"/>
<argument type="service" id="Kiener\MolliePayments\Components\Subscription\Services\SubscriptionHistory\SubscriptionHistoryHandler"/>
<argument type="service" id="mollie_payments.logger"/>
<argument type="service" id="Kiener\MolliePayments\Service\Tags\OrderTagService"/>
</service>

<service id="Kiener\MolliePayments\Components\Subscription\Actions\ConfirmAction">
Expand Down
19 changes: 19 additions & 0 deletions src/Service/Tags/Exceptions/CouldNotTagOrderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Service\Tags\Exceptions;

class CouldNotTagOrderException extends \Exception
{
public const SUBSCRIPTION_CODE = 1;

private function __construct(string $message, int $code)
{
parent::__construct($message, $code);
}

public static function forSubscription(string $message): self
{
return new self($message, self::SUBSCRIPTION_CODE);
}
}
92 changes: 92 additions & 0 deletions src/Service/Tags/OrderTagService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Service\Tags;

use Closure;
use Kiener\MolliePayments\Components\Subscription\DAL\Subscription\SubscriptionEntity;
use Kiener\MolliePayments\Service\Tags\Exceptions\CouldNotTagOrderException;
use Kiener\MolliePayments\Struct\Tags\SubscriptionTag;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\Tag\TagCollection;
use Shopware\Core\System\Tag\TagEntity;

class OrderTagService
{
/**
* @var EntityRepository
*/
private $orderRepository;

/**
* @var EntityRepository
*/
private $tagRepository;

public function __construct(
EntityRepository $orderRepository,
EntityRepository $tagRepository
) {
$this->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<string, string>
*/
private function serializeTag(TagEntity $tag): array
{
return ['id' => $tag->getId()];
}
}
45 changes: 45 additions & 0 deletions src/Struct/Tags/AbstractTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Struct\Tags;

use Shopware\Core\Framework\Struct\Struct;

abstract class AbstractTag extends Struct
{
/**
* @var string
*/
private $name;

/**
* @var string
*/
private $id;

final private function __construct(string $name, string $id)
{
$this->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;
}
}
15 changes: 15 additions & 0 deletions src/Struct/Tags/SubscriptionTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Struct\Tags;

class SubscriptionTag extends AbstractTag
{
public const TAG_NAME = 'mollie-subscription-tag';
public const TAG_ID = 'c4b7c9b6e0c5435c8a74f5de6051b678';

public static function create(): self
{
return parent::createObject(self::TAG_NAME, self::TAG_ID);
}
}

0 comments on commit 0e6a883

Please sign in to comment.