Completed
Push — master ( 56a686...736967 )
by Oleksandr
15s queued 11s
created

isQuoteShippingAddressChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
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\DirectDebit\PaymentOption;
9
10
use Generated\Shared\Transfer\HeidelpayDirectDebitPaymentOptionsTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...tPaymentOptionsTransfer 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...
11
use Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...bitRegistrationTransfer 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\HeidelpayPaymentOptionTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...ayPaymentOptionTransfer 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;
15
use SprykerEco\Zed\Heidelpay\Business\Payment\DirectDebit\Registration\DirectDebitRegistrationReaderInterface;
16
17
class DirectDebitLastSuccessfulRegistration implements DirectDebitPaymentOptionInterface
18
{
19
    /**
20
     * @var \SprykerEco\Zed\Heidelpay\Business\Payment\DirectDebit\Registration\DirectDebitRegistrationReaderInterface
21
     */
22
    protected $registrationReader;
23
24
    /**
25
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\DirectDebit\Registration\DirectDebitRegistrationReaderInterface $registrationReader
26
     */
27
    public function __construct(DirectDebitRegistrationReaderInterface $registrationReader)
28
    {
29
        $this->registrationReader = $registrationReader;
30
    }
31
32
    /**
33
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
34
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitPaymentOptionsTransfer $paymentOptionsTransfer
35
     *
36
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitPaymentOptionsTransfer
37
     */
38
    public function addPaymentOption(
39
        QuoteTransfer $quoteTransfer,
40
        HeidelpayDirectDebitPaymentOptionsTransfer $paymentOptionsTransfer
41
    ): HeidelpayDirectDebitPaymentOptionsTransfer {
42
        $lastSuccessfulRegistrationTransfer = $this->getLastSuccessfulRegistration($quoteTransfer);
43
44
        if ($lastSuccessfulRegistrationTransfer->getIdDirectDebitRegistration() === null) {
45
            return $paymentOptionsTransfer;
46
        }
47
48
        $paymentOptionsTransfer
49
            ->setLastSuccessfulRegistration($lastSuccessfulRegistrationTransfer)
50
            ->addOption($this->createPaymentOptionTransfer());
51
52
        return $paymentOptionsTransfer;
53
    }
54
55
    /**
56
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
57
     *
58
     * @return bool
59
     */
60
    public function isOptionAvailableForQuote(QuoteTransfer $quoteTransfer): bool
61
    {
62
        return $this->isRegisteredShippingAddressUsed($quoteTransfer);
63
    }
64
65
    /**
66
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
67
     *
68
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
69
     */
70
    protected function getLastSuccessfulRegistration(QuoteTransfer $quoteTransfer): HeidelpayDirectDebitRegistrationTransfer
71
    {
72
        $lastSuccessfulRegistrationTransfer = $this->getLastSuccessfulRegistrationFromQuote($quoteTransfer);
73
74
        if ($lastSuccessfulRegistrationTransfer !== null) {
75
            return $lastSuccessfulRegistrationTransfer;
76
        }
77
78
        return $this->registrationReader->getLastSuccessfulRegistration($quoteTransfer);
79
    }
80
81
    /**
82
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
83
     *
84
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer|null
85
     */
86
    protected function getLastSuccessfulRegistrationFromQuote(QuoteTransfer $quoteTransfer): ?HeidelpayDirectDebitRegistrationTransfer
87
    {
88
        if (!$this->isLastSuccessfulRegistrationExistsInQuote($quoteTransfer)) {
89
            return null;
90
        }
91
92
        $lastSuccessfulRegistrationTransfer = $quoteTransfer
93
            ->getPayment()
94
            ->getHeidelpayDirectDebit()
95
            ->getPaymentOptions()
96
            ->getLastSuccessfulRegistration();
97
98
        if ($this->isQuoteShippingAddressChanged($quoteTransfer, $lastSuccessfulRegistrationTransfer)) {
99
            return null;
100
        }
101
102
        return $lastSuccessfulRegistrationTransfer;
103
    }
104
105
    /**
106
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
107
     *
108
     * @return bool
109
     */
110
    protected function isLastSuccessfulRegistrationExistsInQuote(QuoteTransfer $quoteTransfer): bool
111
    {
112
        $directDebitPaymentTransfer = $quoteTransfer
113
            ->getPayment()
114
            ->getHeidelpayDirectDebit();
115
116
        return $directDebitPaymentTransfer !== null
117
            && $directDebitPaymentTransfer->getPaymentOptions() !== null
118
            && $directDebitPaymentTransfer->getPaymentOptions()->getLastSuccessfulRegistration() !== null;
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
123
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer $lastRegistrationTransfer
124
     *
125
     * @return bool
126
     */
127
    protected function isQuoteShippingAddressChanged(
128
        QuoteTransfer $quoteTransfer,
129
        HeidelpayDirectDebitRegistrationTransfer $lastRegistrationTransfer
130
    ): bool {
131
        return $quoteTransfer->getShippingAddress()->getIdCustomerAddress() !== $lastRegistrationTransfer->getIdCustomerAddress();
132
    }
133
134
    /**
135
     * @return \Generated\Shared\Transfer\HeidelpayPaymentOptionTransfer
136
     */
137
    protected function createPaymentOptionTransfer(): HeidelpayPaymentOptionTransfer
138
    {
139
        return (new HeidelpayPaymentOptionTransfer())
140
            ->setCode(HeidelpayConfig::DIRECT_DEBIT_PAYMENT_OPTION_EXISTING_REGISTRATION);
141
    }
142
143
    /**
144
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
145
     *
146
     * @return bool
147
     */
148
    protected function isRegisteredShippingAddressUsed(QuoteTransfer $quoteTransfer): bool
149
    {
150
        return $quoteTransfer->getShippingAddress() !== null
151
            && $quoteTransfer->getShippingAddress()->getIdCustomerAddress() !== null;
152
    }
153
}
154