-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from Setono/refactor-handler
Refactor the assign callouts handler
- Loading branch information
Showing
8 changed files
with
178 additions
and
96 deletions.
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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusCalloutPlugin\Message\Handler; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use DoctrineBatchUtils\BatchProcessing\SimpleBatchIteratorAggregate; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait; | ||
use Setono\SyliusCalloutPlugin\Checker\Eligibility\CalloutEligibilityCheckerInterface; | ||
use Setono\SyliusCalloutPlugin\Event\ProductQueryBuilderEvent; | ||
use Setono\SyliusCalloutPlugin\Model\CalloutInterface; | ||
use Setono\SyliusCalloutPlugin\Model\ProductInterface; | ||
use Setono\SyliusCalloutPlugin\Repository\CalloutRepositoryInterface; | ||
|
||
class AbstractAssignCalloutsHandler | ||
{ | ||
use ORMManagerTrait; | ||
|
||
public function __construct( | ||
protected readonly CalloutRepositoryInterface $calloutRepository, | ||
protected readonly EventDispatcherInterface $eventDispatcher, | ||
protected readonly CalloutEligibilityCheckerInterface $eligibilityChecker, | ||
ManagerRegistry $managerRegistry, | ||
/** @var class-string $productClass */ | ||
protected readonly string $productClass, | ||
) { | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
/** | ||
* @param iterable<ProductInterface> $products If the $products argument is empty, all products will be fetched from the database | ||
* @param iterable<CalloutInterface> $callouts If the $callouts argument is empty, all callouts will be fetched from the database | ||
*/ | ||
protected function assign(iterable $products = [], iterable $callouts = []): void | ||
{ | ||
$manager = $this->getManager($this->productClass); | ||
|
||
if ([] === $products) { | ||
$qb = $manager | ||
->createQueryBuilder() | ||
->select('o') | ||
->from($this->productClass, 'o') | ||
// todo the following two 'wheres' should be moved to event subscribers and be able to be disabled in the configuration of the plugin | ||
->andWhere('o.enabled = true') | ||
->andWhere('SIZE(o.channels) > 0') | ||
; | ||
|
||
$this->eventDispatcher->dispatch(new ProductQueryBuilderEvent($qb)); | ||
|
||
/** @var iterable<ProductInterface> $products */ | ||
$products = SimpleBatchIteratorAggregate::fromQuery($qb->getQuery(), 100); | ||
} | ||
|
||
$resetCallouts = false; | ||
// we know that the only case where we want to reset the callouts is when we assign _all_ callouts | ||
if ([] === $callouts) { | ||
$callouts = $this->calloutRepository->findEnabled(); | ||
$resetCallouts = true; | ||
} | ||
|
||
foreach ($products as $product) { | ||
if ($resetCallouts) { | ||
$product->resetPreQualifiedCallouts(); | ||
} | ||
|
||
foreach ($callouts as $callout) { | ||
$product->removePreQualifiedCallout($callout); | ||
if ($this->eligibilityChecker->isEligible($product, $callout)) { | ||
$product->addPreQualifiedCallout($callout); | ||
} | ||
} | ||
} | ||
|
||
$manager->flush(); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusCalloutPlugin\Message\Handler; | ||
|
||
use Setono\SyliusCalloutPlugin\Message\Command\AssignCallout; | ||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; | ||
|
||
final class AssignCalloutHandler extends AbstractAssignCalloutsHandler | ||
{ | ||
public function __invoke(AssignCallout $message): void | ||
{ | ||
$callout = $this->calloutRepository->findOneByCode($message->callout); | ||
if (null === $callout) { | ||
throw new UnrecoverableMessageHandlingException(sprintf('Callout with code "%s" does not exist', $message->callout)); | ||
} | ||
|
||
if (null !== $message->version && $message->version !== $callout->getVersion()) { | ||
// this means the callout has been updated since the message was sent | ||
throw new UnrecoverableMessageHandlingException(sprintf('Callout with id %s has version %s, but version %s was expected', $message->callout, (string) $callout->getVersion(), $message->version)); | ||
} | ||
|
||
if (!$callout->isEnabled()) { | ||
throw new UnrecoverableMessageHandlingException(sprintf('Callout with id %s is not enabled', $message->callout)); | ||
} | ||
|
||
$callouts = [$callout]; | ||
|
||
$this->assign(callouts: $callouts); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusCalloutPlugin\Message\Handler; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
use Setono\SyliusCalloutPlugin\Message\Command\AssignCalloutsToProduct; | ||
use Setono\SyliusCalloutPlugin\Model\ProductInterface; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class AssignCalloutsToProductHandler extends AbstractAssignCalloutsHandler | ||
{ | ||
public function __invoke(AssignCalloutsToProduct $message): void | ||
{ | ||
$manager = $this->getManager($this->productClass); | ||
|
||
/** @var ProductRepositoryInterface|EntityRepository $productRepository */ | ||
$productRepository = $manager->getRepository($this->productClass); | ||
Assert::isInstanceOf($productRepository, ProductRepositoryInterface::class); | ||
|
||
$product = $productRepository->findOneByCode($message->product); | ||
if (!$product instanceof ProductInterface) { | ||
throw new UnrecoverableMessageHandlingException(sprintf('Product with code "%s" does not exist', $message->product)); | ||
} | ||
|
||
$products = [$product]; | ||
|
||
$this->assign($products); | ||
} | ||
} |
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