src/Core/Checkout/Cart/RuleLoader.php line 40

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart;
  3. use Shopware\Core\Content\Rule\RuleCollection;
  4. use Shopware\Core\Content\Rule\RuleEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\RepositoryIterator;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  10. use Shopware\Core\Framework\Log\Package;
  11. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  12. /**
  13.  * @internal Depend on the AbstractRuleLoader which is the definition of public API for this scope
  14.  */
  15. #[Package('checkout')]
  16. class RuleLoader extends AbstractRuleLoader
  17. {
  18.     public function __construct(private readonly EntityRepository $repository)
  19.     {
  20.     }
  21.     public function getDecorated(): AbstractRuleLoader
  22.     {
  23.         throw new DecorationPatternException(self::class);
  24.     }
  25.     public function load(Context $context): RuleCollection
  26.     {
  27.         $criteria = new Criteria();
  28.         $criteria->addSorting(new FieldSorting('priority'FieldSorting::DESCENDING));
  29.         $criteria->addSorting(new FieldSorting('id'));
  30.         $criteria->setLimit(500);
  31.         $criteria->setTitle('cart-rule-loader::load-rules');
  32.         $repositoryIterator = new RepositoryIterator($this->repository$context$criteria);
  33.         $rules = new RuleCollection();
  34.         while (($result $repositoryIterator->fetch()) !== null) {
  35.             /** @var RuleEntity $rule */
  36.             foreach ($result->getEntities() as $rule) {
  37.                 if (!$rule->isInvalid() && $rule->getPayload()) {
  38.                     $rules->add($rule);
  39.                 }
  40.             }
  41.             if ($result->count() < 500) {
  42.                 break;
  43.             }
  44.         }
  45.         return $rules;
  46.     }
  47. }