PaymentMethodFilter::getAvailablePaymentMethods()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 PaymentMethodFilter implements PaymentMethodFilterInterface
17
{
18
    protected const COMPUTOP_PAYMENT_METHOD = 'computop';
19
    protected const CONFIG_METHOD_PART_GET_CRIF = 'getCrif';
20
    protected const CONFIG_METHOD_PART_PAYMENT_METHODS = 'PaymentMethods';
21
22
    /**
23
     * @var \SprykerEco\Zed\Computop\ComputopConfig
24
     */
25
    protected $config;
26
27
    /**
28
     * @param \SprykerEco\Zed\Computop\ComputopConfig $config
29
     */
30
    public function __construct(ComputopConfig $config)
31
    {
32
        $this->config = $config;
33
    }
34
35
    /**
36
     * @param \Generated\Shared\Transfer\PaymentMethodsTransfer $paymentMethodsTransfer
37
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
38
     *
39
     * @return \Generated\Shared\Transfer\PaymentMethodsTransfer
40
     */
41
    public function filterPaymentMethods(
42
        PaymentMethodsTransfer $paymentMethodsTransfer,
43
        QuoteTransfer $quoteTransfer
44
    ): PaymentMethodsTransfer {
45
        if (!$this->config->isCrifEnabled()) {
46
            return $paymentMethodsTransfer;
47
        }
48
49
        $availableMethods = $this->getAvailablePaymentMethods($quoteTransfer);
50
51
        $result = new ArrayObject();
52
53
        foreach ($paymentMethodsTransfer->getMethods() as $paymentMethod) {
54
            if ($this->isPaymentMethodComputop($paymentMethod) && !$this->isAvailable($paymentMethod, $availableMethods)) {
55
                continue;
56
            }
57
58
            $result->append($paymentMethod);
59
        }
60
61
        $paymentMethodsTransfer->setMethods($result);
62
63
        return $paymentMethodsTransfer;
64
    }
65
66
    /**
67
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
68
     *
69
     * @return string[]
70
     */
71
    protected function getAvailablePaymentMethods(QuoteTransfer $quoteTransfer): array
72
    {
73
        $method = static::CONFIG_METHOD_PART_GET_CRIF .
74
            ucfirst(strtolower($quoteTransfer->getComputopCrif()->getResult())) .
75
            static::CONFIG_METHOD_PART_PAYMENT_METHODS;
76
77
        if (method_exists($this->config, $method)) {
78
            return $this->config->$method();
79
        }
80
81
        return $this->config->getCrifRedPaymentMethods();
82
    }
83
84
    /**
85
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
86
     * @param string[] $availableMethods
87
     *
88
     * @return bool
89
     */
90
    protected function isAvailable(PaymentMethodTransfer $paymentMethodTransfer, $availableMethods): bool
91
    {
92
        return in_array($paymentMethodTransfer->getMethodName(), $availableMethods);
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
97
     *
98
     * @return bool
99
     */
100
    protected function isPaymentMethodComputop(PaymentMethodTransfer $paymentMethodTransfer): bool
101
    {
102
        return strpos($paymentMethodTransfer->getMethodName(), static::COMPUTOP_PAYMENT_METHOD) !== false;
103
    }
104
}
105