Passed
Push — feature/eco-3623-payone-pay-u-... ( 874a59...7427d2 )
by Roman
16:19 queued 08:34
created

filterPaymentMethods()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Computop\Business\Payment;
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\Zed\Computop\ComputopConfig;
15
16
class PaymentMethodByCurrencyFilter implements PaymentMethodFilterInterface
17
{
18
    protected const COMPUTOP_PAYMENT_METHOD = 'computop';
19
20
    /**
21
     * @var \SprykerEco\Zed\Computop\ComputopConfig
22
     */
23
    protected $config;
24
25
    /**
26
     * @param \SprykerEco\Zed\Computop\ComputopConfig $config
27
     */
28
    public function __construct(ComputopConfig $config)
29
    {
30
        $this->config = $config;
31
    }
32
33
    /**
34
     * @param \Generated\Shared\Transfer\PaymentMethodsTransfer $paymentMethodsTransfer
35
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
36
     *
37
     * @return \Generated\Shared\Transfer\PaymentMethodsTransfer
38
     */
39
    public function filterPaymentMethods(PaymentMethodsTransfer $paymentMethodsTransfer, QuoteTransfer $quoteTransfer): PaymentMethodsTransfer
40
    {
41
        $currencyCode = $quoteTransfer->getCurrency()->getCode();
42
        $availableCurrencies = $this->config->getComputopPaymentMethodCurrencyFilterMap();
43
44
        $result = new ArrayObject();
45
        foreach ($paymentMethodsTransfer->getMethods() as $paymentMethod) {
46
            if ($this->isComputopAvailableMethod($paymentMethod, $currencyCode, $availableCurrencies)) {
47
                $result->append($paymentMethod);
48
            }
49
        }
50
51
        return $paymentMethodsTransfer->setMethods($result);
52
    }
53
54
    /**
55
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
56
     * @param string $currencyCode
57
     * @param array $availableCurrencies
58
     *
59
     * @return bool
60
     */
61
    protected function isComputopAvailableMethod(
62
        PaymentMethodTransfer $paymentMethodTransfer,
63
        string $currencyCode,
64
        array $availableCurrencies
65
    ): bool {
66
        if (strpos($paymentMethodTransfer->getPaymentMethodKey(), static::COMPUTOP_PAYMENT_METHOD) === false) {
67
            return true;
68
        }
69
70
        if (!isset($availableCurrencies[$paymentMethodTransfer->getPaymentMethodKey()])) {
71
            return true;
72
        }
73
74
        if (in_array(strtoupper($currencyCode), $availableCurrencies[$paymentMethodTransfer->getPaymentMethodKey()], true)) {
75
            return true;
76
        }
77
78
        return false;
79
    }
80
}
81