Passed
Pull Request — master (#4)
by Aleksey
03:26
created

filterPaymentMethods()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 2
dl 0
loc 17
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\CrefoPay\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\Zed\CrefoPay\Business\Mapper\PaymentMethod\CrefoPayPaymentMethodMapperInterface;
15
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
16
17
class CrefoPayPaymentMethodFilter implements CrefoPayPaymentMethodFilterInterface
18
{
19
    /**
20
     * @var \SprykerEco\Zed\CrefoPay\Business\Mapper\PaymentMethod\CrefoPayPaymentMethodMapperInterface
21
     */
22
    protected $paymentMethodMapper;
23
24
    /**
25
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
26
     */
27
    protected $config;
28
29
    /**
30
     * @var string[]
31
     */
32
    protected $availableMethods = [];
33
34
    /**
35
     * @param \SprykerEco\Zed\CrefoPay\Business\Mapper\PaymentMethod\CrefoPayPaymentMethodMapperInterface $paymentMethodMapper
36
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
37
     */
38
    public function __construct(
39
        CrefoPayPaymentMethodMapperInterface $paymentMethodMapper,
40
        CrefoPayConfig $config
41
    ) {
42
        $this->config = $config;
43
        $this->paymentMethodMapper = $paymentMethodMapper;
44
    }
45
46
    /**
47
     * @param \Generated\Shared\Transfer\PaymentMethodsTransfer $paymentMethodsTransfer
48
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
49
     *
50
     * @return \Generated\Shared\Transfer\PaymentMethodsTransfer
51
     */
52
    public function filterPaymentMethods(
53
        PaymentMethodsTransfer $paymentMethodsTransfer,
54
        QuoteTransfer $quoteTransfer
55
    ): PaymentMethodsTransfer {
56
        $result = new ArrayObject();
57
58
        foreach ($paymentMethodsTransfer->getMethods() as $paymentMethod) {
59
            if ($this->isPaymentProviderCrefoPay($paymentMethod) && !$this->isAvailable($paymentMethod, $quoteTransfer)) {
60
                continue;
61
            }
62
63
            $result->append($paymentMethod);
64
        }
65
66
        $paymentMethodsTransfer->setMethods($result);
67
68
        return $paymentMethodsTransfer;
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
73
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
74
     *
75
     * @return bool
76
     */
77
    protected function isAvailable(PaymentMethodTransfer $paymentMethodTransfer, QuoteTransfer $quoteTransfer): bool
78
    {
79
        $allowedPaymentMethods = $quoteTransfer->getCrefoPayTransaction()->getAllowedPaymentMethods();
80
        $externalPaymentMethodName = $this->paymentMethodMapper
81
            ->mapInternalToExternalPaymentMethodName(
82
                $paymentMethodTransfer->getMethodName()
83
            );
84
85
        return in_array($externalPaymentMethodName, $allowedPaymentMethods);
86
    }
87
88
    /**
89
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
90
     *
91
     * @return bool
92
     */
93
    protected function isPaymentProviderCrefoPay(PaymentMethodTransfer $paymentMethodTransfer): bool
94
    {
95
        return strpos($paymentMethodTransfer->getMethodName(), $this->config->getProviderName()) !== false;
96
    }
97
}
98