-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PISHPS-304: added OrderTagService #821
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
855e75a
PISHPS-304: added OrderTagService
m-muxfeld-diw 46447a7
Merge branch 'master' into users/mm/pishps-304
m-muxfeld-diw 92d3494
PISHPS-304: made Migration1725347559MollieTags sw 6.4 compatible
m-muxfeld-diw 501da35
PISHPS-304: refactored Migration1725347559MollieTags::createTag it no…
m-muxfeld-diw b02199e
PISHPS-304: OrderTagService now searches the tag by name
m-muxfeld-diw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,93 @@ | ||
<?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)); | ||
} | ||
|
||
// 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<string, string> | ||
*/ | ||
private function serializeTag(TagEntity $tag): array | ||
{ | ||
return ['id' => $tag->getId(),]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this comma intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was, but I don't mind removing it |
||
} | ||
} |
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you use here the tag name, merchants can delete and create tags on the fly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅