vendor/shopware/core/Checkout/Cart/RuleLoader.php line 43

  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.  * @final 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.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(private readonly EntityRepository $repository)
  22.     {
  23.     }
  24.     public function getDecorated(): AbstractRuleLoader
  25.     {
  26.         throw new DecorationPatternException(self::class);
  27.     }
  28.     public function load(Context $context): RuleCollection
  29.     {
  30.         $criteria = new Criteria();
  31.         $criteria->addSorting(new FieldSorting('priority'FieldSorting::DESCENDING));
  32.         $criteria->addSorting(new FieldSorting('id'));
  33.         $criteria->setLimit(500);
  34.         $criteria->setTitle('cart-rule-loader::load-rules');
  35.         $repositoryIterator = new RepositoryIterator($this->repository$context$criteria);
  36.         $rules = new RuleCollection();
  37.         while (($result $repositoryIterator->fetch()) !== null) {
  38.             /** @var RuleEntity $rule */
  39.             foreach ($result->getEntities() as $rule) {
  40.                 if (!$rule->isInvalid() && $rule->getPayload()) {
  41.                     $rules->add($rule);
  42.                 }
  43.             }
  44.             if ($result->count() < 500) {
  45.                 break;
  46.             }
  47.         }
  48.         return $rules;
  49.     }
  50. }