Completed
Push — master ( 24e2fb...716e2b )
by Oleksandr
16s queued 14s
created

PaymentMethodFilter::filterPaymentMethods()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
cc 5
nc 3
nop 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\Heidelpay\Business\Payment;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\PaymentMethodsTransfer;
1 ignored issue
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;
1 ignored issue
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;
1 ignored issue
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\Shared\Heidelpay\HeidelpayConfig as SharedHeidelpayConfig;
15
use SprykerEco\Zed\Heidelpay\Dependency\Facade\HeidelpayToMoneyFacadeInterface;
16
use SprykerEco\Zed\Heidelpay\HeidelpayConfig;
17
18
class PaymentMethodFilter implements PaymentMethodFilterInterface
19
{
20
    /**
21
     * @var \SprykerEco\Zed\Heidelpay\HeidelpayConfig
22
     */
23
    protected $config;
24
25
    /**
26
     * @var \SprykerEco\Zed\Heidelpay\Dependency\Facade\HeidelpayToMoneyFacadeInterface
27
     */
28
    protected $moneyFacade;
29
30
    /**
31
     * @param \SprykerEco\Zed\Heidelpay\HeidelpayConfig $config
32
     * @param \SprykerEco\Zed\Heidelpay\Dependency\Facade\HeidelpayToMoneyFacadeInterface $moneyFacade
33
     */
34
    public function __construct(
35
        HeidelpayConfig $config,
36
        HeidelpayToMoneyFacadeInterface $moneyFacade
37
    ) {
38
        $this->config = $config;
39
        $this->moneyFacade = $moneyFacade;
40
    }
41
42
    /**
43
     * @param \Generated\Shared\Transfer\PaymentMethodsTransfer $paymentMethodsTransfer
44
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
45
     *
46
     * @return \Generated\Shared\Transfer\PaymentMethodsTransfer
47
     */
48
    public function filterPaymentMethods(
49
        PaymentMethodsTransfer $paymentMethodsTransfer,
50
        QuoteTransfer $quoteTransfer
51
    ): PaymentMethodsTransfer {
52
53
        $result = new ArrayObject();
54
        $grandTotal = $this->moneyFacade->convertIntegerToDecimal($quoteTransfer->getTotals()->getGrandTotal());
55
        foreach ($paymentMethodsTransfer->getMethods() as $paymentMethod) {
56
            $address = $this->isAddressCorrect($quoteTransfer);
0 ignored issues
show
Unused Code introduced by
The assignment to $address is dead and can be removed.
Loading history...
57
            if ($this->isPaymentMethodHeidelpayEasyCredit($paymentMethod) && $this->isTotalOutOfRange($grandTotal) && $this->isAddressCorrect($quoteTransfer)) {
58
                continue;
59
            }
60
61
            $result->append($paymentMethod);
62
        }
63
64
        $paymentMethodsTransfer->setMethods($result);
65
66
        return $paymentMethodsTransfer;
67
    }
68
69
    /**
70
     * @param \Generated\Shared\Transfer\PaymentMethodTransfer $paymentMethodTransfer
71
     *
72
     * @return bool
73
     */
74
    protected function isPaymentMethodHeidelpayEasyCredit(PaymentMethodTransfer $paymentMethodTransfer): bool
75
    {
76
        return $paymentMethodTransfer->getMethodName() === SharedHeidelpayConfig::PAYMENT_METHOD_EASY_CREDIT;
77
    }
78
79
    /**
80
     * @param float $grandTotal
81
     *
82
     * @return bool
83
     */
84
    protected function isTotalOutOfRange(float $grandTotal): bool
85
    {
86
        $isOutOfRange = (
87
            $grandTotal < $this->config->getEasycreditCriteriaGrandTotalLessThan()
88
            || $grandTotal > $this->config->getEasycreditCriteriaGrandTotalMoreThan()
89
        );
90
91
        return $isOutOfRange;
92
    }
93
94
    /**
95
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
96
     *
97
     * @return bool
98
     */
99
    protected function isAddressCorrect(QuoteTransfer $quoteTransfer): bool
100
    {
101
        $isRejectedShippingAddress = strpos(
102
            $quoteTransfer->getShippingAddress()->getAddress1(),
103
            $this->config->getEasycreditCriteriaRejectedDeliveryAddress()
104
        );
105
        $isRejectedBillingAddress = strpos(
106
            $quoteTransfer->getBillingAddress()->getAddress1(),
107
            $this->config->getEasycreditCriteriaRejectedDeliveryAddress()
108
        );
109
110
        return ($isRejectedShippingAddress === false && $isRejectedBillingAddress === false);
111
    }
112
}
113