CrefoPayPaymentMethodFilter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 20
c 3
b 1
f 0
dl 0
loc 78
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filterPaymentMethods() 0 17 4
A __construct() 0 6 1
A isPaymentProviderCrefoPay() 0 3 1
A isAvailable() 0 13 2
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
     * @param \SprykerEco\Zed\CrefoPay\Business\Mapper\PaymentMethod\CrefoPayPaymentMethodMapperInterface $paymentMethodMapper
31
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
32
     */
33
    public function __construct(
34
        CrefoPayPaymentMethodMapperInterface $paymentMethodMapper,
35
        CrefoPayConfig $config
36
    ) {
37
        $this->config = $config;
38
        $this->paymentMethodMapper = $paymentMethodMapper;
39
    }
40
41
    /**
42
     * @param \Generated\Shared\Transfer\PaymentMethodsTransfer $paymentMethodsTransfer
43
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
44
     *
45
     * @return \Generated\Shared\Transfer\PaymentMethodsTransfer
46
     */
47
    public function filterPaymentMethods(
48
        PaymentMethodsTransfer $paymentMethodsTransfer,
49
        QuoteTransfer $quoteTransfer
50
    ): PaymentMethodsTransfer {
51
        $result = new ArrayObject();
52
53
        foreach ($paymentMethodsTransfer->getMethods() as $paymentMethod) {
54
            if ($this->isPaymentProviderCrefoPay($paymentMethod) && !$this->isAvailable($paymentMethod, $quoteTransfer)) {
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\PaymentMethodTransfer $paymentMethodTransfer
68
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
69
     *
70
     * @return bool
71
     */
72
    protected function isAvailable(PaymentMethodTransfer $paymentMethodTransfer, QuoteTransfer $quoteTransfer): bool
73
    {
74
        if (!$quoteTransfer->getCrefoPayTransaction()) {
75
            return false;
76
        }
77
78
        $allowedPaymentMethods = $quoteTransfer->getCrefoPayTransaction()->getAllowedPaymentMethods();
79
        $externalPaymentMethodName = $this->paymentMethodMapper
80
            ->mapInternalToExternalPaymentMethodName(
81
                $paymentMethodTransfer->getMethodName(),
82
            );
83
84
        return in_array($externalPaymentMethodName, $allowedPaymentMethods);
85
    }
86
87
    /**
88
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
89
     *
90
     * @return bool
91
     */
92
    protected function isPaymentProviderCrefoPay(PaymentMethodTransfer $paymentMethodTransfer): bool
93
    {
94
        return strpos($paymentMethodTransfer->getMethodName(), $this->config->getProviderName()) !== false;
95
    }
96
}
97