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

Open in your IDE?
  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\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  10. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  11. /**
  12.  * @internal
  13.  */
  14. class RuleLoader extends AbstractRuleLoader
  15. {
  16.     private EntityRepositoryInterface $repository;
  17.     public function __construct(EntityRepositoryInterface $repository)
  18.     {
  19.         $this->repository $repository;
  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. }