vendor/shopware/platform/src/Core/Checkout/Cart/RuleLoader.php line 44

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.     /**
  17.      * @var EntityRepositoryInterface
  18.      */
  19.     private $repository;
  20.     public function __construct(EntityRepositoryInterface $repository)
  21.     {
  22.         $this->repository $repository;
  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. }