AfterPayPaymentMethodsFilter::isAvailable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\AfterPay\Business\Payment\Filter;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\PaymentMethodsTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentMethodsTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\PaymentMethodTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentMethodTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use SprykerEco\Shared\AfterPay\AfterPayConfig;
15
use SprykerEco\Zed\AfterPay\Business\Payment\Filter\Provider\AfterPayPaymentMethodsProviderInterface;
16
17
class AfterPayPaymentMethodsFilter implements AfterPayPaymentMethodsFilterInterface
18
{
19
    /**
20
     * @var \Generated\Shared\Transfer\AfterPayAvailablePaymentMethodsTransfer
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ePaymentMethodsTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
     */
22
    protected $availablePaymentMethods;
23
24
    /**
25
     * @var \SprykerEco\Zed\AfterPay\Business\Payment\Filter\Provider\AfterPayPaymentMethodsProviderInterface
26
     */
27
    protected $paymentMethodsProvider;
28
29
    /**
30
     * @param \SprykerEco\Zed\AfterPay\Business\Payment\Filter\Provider\AfterPayPaymentMethodsProviderInterface $paymentMethodsProvider
31
     */
32
    public function __construct(AfterPayPaymentMethodsProviderInterface $paymentMethodsProvider)
33
    {
34
        $this->paymentMethodsProvider = $paymentMethodsProvider;
35
    }
36
37
    /**
38
     * @param \Generated\Shared\Transfer\PaymentMethodsTransfer $paymentMethodsTransfer
39
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
40
     *
41
     * @return \Generated\Shared\Transfer\PaymentMethodsTransfer
42
     */
43
    public function filterPaymentMethods(
44
        PaymentMethodsTransfer $paymentMethodsTransfer,
45
        QuoteTransfer $quoteTransfer
46
    ): PaymentMethodsTransfer {
47
        $this->availablePaymentMethods = $this->paymentMethodsProvider->getAvailablePaymentMethods($quoteTransfer);
48
49
        $result = new ArrayObject();
50
51
        foreach ($paymentMethodsTransfer->getMethods() as $paymentMethod) {
52
            if ($this->isPaymentProviderAfterPay($paymentMethod) && !$this->isAvailable($paymentMethod)) {
53
                continue;
54
            }
55
56
            $result->append($paymentMethod);
57
        }
58
59
        $paymentMethodsTransfer->setMethods($result);
60
61
        return $paymentMethodsTransfer;
62
    }
63
64
    /**
65
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
66
     *
67
     * @return bool
68
     */
69
    protected function isAvailable(PaymentMethodTransfer $paymentMethodTransfer): bool
70
    {
71
        return in_array(
72
            $paymentMethodTransfer->getMethodName(),
73
            $this->availablePaymentMethods->getAvailablePaymentMethodNames(),
74
        );
75
    }
76
77
    /**
78
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
79
     *
80
     * @return bool
81
     */
82
    protected function isPaymentProviderAfterPay(PaymentMethodTransfer $paymentMethodTransfer): bool
83
    {
84
        return strpos($paymentMethodTransfer->getMethodName(), AfterPayConfig::PROVIDER_NAME) !== false;
85
    }
86
}
87