Passed
Push — feature/eco-574/eco-2266-check... ( 7e1004...8cf5ab )
by Aleksey
04:15
created

AfterPayPaymentMethodsFilter::isAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
    protected $paymentMethodsProvider;
25
26
    public function __construct(AfterPayPaymentMethodsProviderInterface $paymentMethodsProvider)
27
    {
28
        $this->paymentMethodsProvider = $paymentMethodsProvider;
29
    }
30
31
    /**
32
     * @param \Generated\Shared\Transfer\PaymentMethodsTransfer $paymentMethodsTransfer
33
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
34
     *
35
     * @return \Generated\Shared\Transfer\PaymentMethodsTransfer
36
     */
37
    public function filterPaymentMethods(
38
        PaymentMethodsTransfer $paymentMethodsTransfer,
39
        QuoteTransfer $quoteTransfer
40
    ): PaymentMethodsTransfer {
41
        $this->availablePaymentMethods = $this->paymentMethodsProvider->getAvailablePaymentMethods($quoteTransfer);
42
43
        $result = new ArrayObject();
44
45
        foreach ($paymentMethodsTransfer->getMethods() as $paymentMethod) {
46
            if ($this->isPaymentProviderAfterPay($paymentMethod) && !$this->isAvailable($paymentMethod)) {
47
                continue;
48
            }
49
50
            $result->append($paymentMethod);
51
        }
52
53
        $paymentMethodsTransfer->setMethods($result);
54
55
        return $paymentMethodsTransfer;
56
    }
57
58
    /**
59
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
60
     *
61
     * @return bool
62
     */
63
    protected function isAvailable(PaymentMethodTransfer $paymentMethodTransfer): bool
64
    {
65
        return in_array(
66
            $paymentMethodTransfer->getMethodName(),
67
            $this->availablePaymentMethods->getAvailablePaymentMethodNames()
68
        );
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
73
     *
74
     * @return bool
75
     */
76
    protected function isPaymentProviderAfterPay(PaymentMethodTransfer $paymentMethodTransfer): bool
77
    {
78
        return strpos($paymentMethodTransfer->getMethodName(), AfterPayConfig::PROVIDER_NAME) !== false;
79
    }
80
}
81