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