vendor/shopware/core/Framework/DataAbstractionLayer/Pricing/ListingPriceCollection.php line 9

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Pricing;
  3. use Shopware\Core\Defaults;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\Struct\Collection;
  6. class ListingPriceCollection extends Collection
  7. {
  8.     public function getContextPrice(Context $context): ?ListingPrice
  9.     {
  10.         $ruleIds $context->getRuleIds();
  11.         $ruleIds[] = null;
  12.         foreach ($ruleIds as $ruleId) {
  13.             $price $this->getRulePrice($ruleId$context);
  14.             if ($price) {
  15.                 return $price;
  16.             }
  17.         }
  18.         return null;
  19.     }
  20.     public function getApiAlias(): string
  21.     {
  22.         return 'listing_price_collection';
  23.     }
  24.     protected function getExpectedClass(): ?string
  25.     {
  26.         return ListingPrice::class;
  27.     }
  28.     private function getRulePrice(?string $ruleIdContext $context): ?ListingPrice
  29.     {
  30.         $prices $this->filterByRuleId($this->elements$ruleId);
  31.         $price $this->getCurrencyPrice($prices$context->getCurrencyId());
  32.         if (!$price) {
  33.             $price $this->getCurrencyPrice($pricesDefaults::CURRENCY);
  34.         }
  35.         return $price;
  36.     }
  37.     private function getCurrencyPrice(array $pricesstring $currencyId): ?ListingPrice
  38.     {
  39.         $prices $this->filterByCurrencyId($prices$currencyId);
  40.         return array_shift($prices);
  41.     }
  42.     private function filterByCurrencyId(iterable $pricesstring $currencyId): array
  43.     {
  44.         $filtered = [];
  45.         /** @var ListingPrice $price */
  46.         foreach ($prices as $price) {
  47.             if ($price->getCurrencyId() === $currencyId) {
  48.                 $filtered[] = $price;
  49.             }
  50.         }
  51.         return $filtered;
  52.     }
  53.     private function filterByRuleId(iterable $prices, ?string $ruleId): array
  54.     {
  55.         $filtered = [];
  56.         /** @var ListingPrice $price */
  57.         foreach ($prices as $price) {
  58.             if ($price->getRuleId() === $ruleId) {
  59.                 $filtered[] = $price;
  60.             }
  61.         }
  62.         return $filtered;
  63.     }
  64. }