Passed
Pull Request — master (#44)
by Michael
12:21 queued 08:36
created

PaymentMethodByCurrencyFilter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 3
b 0
f 0
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterPaymentMethods() 0 13 3
A __construct() 0 3 1
A isComputopAvailableMethod() 0 15 3
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
    /**
19
     * @var string
20
     */
21
    protected const COMPUTOP_PAYMENT_METHOD = 'computop';
22
23
    /**
24
     * @var \SprykerEco\Zed\Computop\ComputopConfig
25
     */
26
    protected $config;
27
28
    /**
29
     * @param \SprykerEco\Zed\Computop\ComputopConfig $config
30
     */
31
    public function __construct(ComputopConfig $config)
32
    {
33
        $this->config = $config;
34
    }
35
36
    /**
37
     * @param \Generated\Shared\Transfer\PaymentMethodsTransfer $paymentMethodsTransfer
38
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
39
     *
40
     * @return \Generated\Shared\Transfer\PaymentMethodsTransfer
41
     */
42
    public function filterPaymentMethods(PaymentMethodsTransfer $paymentMethodsTransfer, QuoteTransfer $quoteTransfer): PaymentMethodsTransfer
43
    {
44
        $currencyCode = $quoteTransfer->getCurrencyOrFail()->getCodeOrFail();
45
        $availableCurrencies = $this->config->getComputopPaymentMethodCurrencyFilterMap();
46
47
        $applicablePaymentMethods = new ArrayObject();
48
        foreach ($paymentMethodsTransfer->getMethods() as $paymentMethod) {
49
            if ($this->isComputopAvailableMethod($paymentMethod, $currencyCode, $availableCurrencies)) {
50
                $applicablePaymentMethods->append($paymentMethod);
51
            }
52
        }
53
54
        return $paymentMethodsTransfer->setMethods($applicablePaymentMethods);
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
59
     * @param string $currencyCode
60
     * @param array $availableCurrencies
61
     *
62
     * @return bool
63
     */
64
    protected function isComputopAvailableMethod(
65
        PaymentMethodTransfer $paymentMethodTransfer,
66
        string $currencyCode,
67
        array $availableCurrencies
68
    ): bool {
69
        if (substr($paymentMethodTransfer->getPaymentMethodKeyOrFail(), 0, 8) !== static::COMPUTOP_PAYMENT_METHOD) {
70
            return true;
71
        }
72
73
        $availableCurrencyForSelectedPaymentMethod = $availableCurrencies[$paymentMethodTransfer->getPaymentMethodKey()] ?? null;
74
        if (!$availableCurrencyForSelectedPaymentMethod) {
75
            return true;
76
        }
77
78
        return in_array(strtoupper($currencyCode), $availableCurrencyForSelectedPaymentMethod, true);
79
    }
80
}
81