Passed
Pull Request — master (#28)
by Volodymyr
05:26
created

PaymentMethodFilter::getAvailablePaymentMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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