vendor/shopware/core/Checkout/Payment/PaymentMethodCollection.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Payment;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  4. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  5. /**
  6.  * @method void                     add(PaymentMethodEntity $entity)
  7.  * @method void                     set(string $key, PaymentMethodEntity $entity)
  8.  * @method PaymentMethodEntity[]    getIterator()
  9.  * @method PaymentMethodEntity[]    getElements()
  10.  * @method PaymentMethodEntity|null get(string $key)
  11.  * @method PaymentMethodEntity|null first()
  12.  * @method PaymentMethodEntity|null last()
  13.  */
  14. class PaymentMethodCollection extends EntityCollection
  15. {
  16.     public function filterByActiveRules(SalesChannelContext $salesChannelContext): PaymentMethodCollection
  17.     {
  18.         return $this->filter(
  19.             function (PaymentMethodEntity $paymentMethod) use ($salesChannelContext) {
  20.                 if ($paymentMethod->getAvailabilityRuleId() === null) {
  21.                     return true;
  22.                 }
  23.                 return \in_array($paymentMethod->getAvailabilityRuleId(), $salesChannelContext->getRuleIds(), true);
  24.             }
  25.         );
  26.     }
  27.     public function getPluginIds(): array
  28.     {
  29.         return $this->fmap(function (PaymentMethodEntity $paymentMethod) {
  30.             return $paymentMethod->getPluginId();
  31.         });
  32.     }
  33.     public function filterByPluginId(string $id): self
  34.     {
  35.         return $this->filter(function (PaymentMethodEntity $paymentMethod) use ($id) {
  36.             return $paymentMethod->getPluginId() === $id;
  37.         });
  38.     }
  39.     /**
  40.      * Sorts the selected payment method first
  41.      * If a different default payment method is defined, it will be sorted second
  42.      * All other payment methods keep their respective sorting
  43.      */
  44.     public function sortPaymentMethodsByPreference(SalesChannelContext $context): void
  45.     {
  46.         $ids array_merge(
  47.             [$context->getPaymentMethod()->getId(), $context->getSalesChannel()->getPaymentMethodId()],
  48.             $this->getIds()
  49.         );
  50.         $this->sortByIdArray($ids);
  51.     }
  52.     public function getApiAlias(): string
  53.     {
  54.         return 'payment_method_collection';
  55.     }
  56.     protected function getExpectedClass(): string
  57.     {
  58.         return PaymentMethodEntity::class;
  59.     }
  60. }