-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
7594fbe
commit 0e6a883
Showing
8 changed files
with
294 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |