Passed
Push — feature/paypal-express ( f5d10f...058644 )
by Volodymyr
04:35 queued 13s
created

PaypalResponseMapper::findCountryTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
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\Yves\Braintree\Model\Mapper\PaypalResponse;
9
10
use Generated\Shared\Transfer\AddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\AddressTransfer 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\BraintreePaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\BraintreePaymentTransfer 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\CountryCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...untryCollectionTransfer 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\CountryTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CountryTransfer 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 Generated\Shared\Transfer\CustomerTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CustomerTransfer 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...
15
use Generated\Shared\Transfer\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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...
16
use Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...SuccessResponseTransfer 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...
17
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...
18
use Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface;
19
use SprykerEco\Shared\Braintree\BraintreeConfig;
20
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToCountryClientInterface;
21
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface;
22
23
class PaypalResponseMapper implements PaypalResponseMapperInterface
24
{
25
    protected const KEY_NONCE = 'nonce';
26
    protected const KEY_DETAILS = 'details';
27
    protected const KEY_EMAIL = 'email';
28
    protected const KEY_FIRST_NAME = 'firstName';
29
    protected const KEY_LAST_NAME = 'lastName';
30
    protected const KEY_PAYER_ID = 'payerId';
31
    protected const KEY_SHIPPING_ADDRESS = 'shippingAddress';
32
    protected const KEY_RECIPIENT_NAME = 'recipientName';
33
    protected const KEY_LINE1 = 'line1';
34
    protected const KEY_CITY = 'city';
35
    protected const KEY_STATE = 'state';
36
    protected const KEY_POSTAL_CODE = 'postalCode';
37
    protected const KEY_COUNTRY_CODE = 'countryCode';
38
    protected const KEY_CURRENCY = 'currency';
39
    protected const KEY_AMOUNT = 'amount';
40
41
    /**
42
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface
43
     */
44
    protected $paymentClient;
45
46
    /**
47
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToCountryClientInterface
48
     */
49
    protected $countryClient;
50
51
    /**
52
     * @var \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface
53
     */
54
    protected $moneyPlugin;
55
56
    /**
57
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface $paymentClient
58
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToCountryClientInterface $countryClient
59
     * @param \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface $moneyPlugin
60
     */
61
    public function __construct(
62
        BraintreeToPaymentClientInterface $paymentClient,
63
        BraintreeToCountryClientInterface $countryClient,
64
        MoneyPluginInterface $moneyPlugin
65
    ) {
66
        $this->paymentClient = $paymentClient;
67
        $this->countryClient = $countryClient;
68
        $this->moneyPlugin = $moneyPlugin;
69
    }
70
71
    /**
72
     * @param array $payload
73
     *
74
     * @return \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer
75
     */
76
    public function mapSuccessResponseToPaypalExpressSuccessResponseTransfer(array $payload): PaypalExpressSuccessResponseTransfer
77
    {
78
        $transfer = new PaypalExpressSuccessResponseTransfer();
79
80
        $transfer->setNonce($payload[static::KEY_NONCE] ?? null);
81
        $transfer->setEmail($payload[static::KEY_DETAILS][static::KEY_EMAIL] ?? null);
82
        $transfer->setFirstName($payload[static::KEY_DETAILS][static::KEY_FIRST_NAME] ?? null);
83
        $transfer->setLastName($payload[static::KEY_DETAILS][static::KEY_LAST_NAME] ?? null);
84
        $transfer->setPayerId($payload[static::KEY_DETAILS][static::KEY_PAYER_ID] ?? null);
85
        $transfer->setRecipientName($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_RECIPIENT_NAME] ?? null);
86
        $transfer->setLine1($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_LINE1] ?? null);
87
        $transfer->setCity($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_CITY] ?? null);
88
        $transfer->setState($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_STATE] ?? null);
89
        $transfer->setPostalCode($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_POSTAL_CODE] ?? null);
90
        $transfer->setCountryCode($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_COUNTRY_CODE] ?? null);
91
        $transfer->setCurrency($payload[static::KEY_CURRENCY] ?? null);
92
        $transfer->setAmount($payload[static::KEY_AMOUNT] ?? null);
93
94
        return $transfer;
95
    }
96
97
    /**
98
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
99
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
100
     *
101
     * @return \Generated\Shared\Transfer\QuoteTransfer
102
     */
103
    public function mapPaypalExpressSuccessResponseTransferToQuoteTransfer(
104
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer,
105
        QuoteTransfer $quoteTransfer
106
    ): QuoteTransfer {
107
        $this->addCustomer($quoteTransfer, $paypalExpressSuccessResponseTransfer);
108
        $this->addShippingAddress($quoteTransfer, $paypalExpressSuccessResponseTransfer);
109
        $this->addBillingTransfer($quoteTransfer, $paypalExpressSuccessResponseTransfer);
110
        $this->addPaymentTransfer($quoteTransfer, $paypalExpressSuccessResponseTransfer);
111
112
        return $quoteTransfer;
113
    }
114
115
    /**
116
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
117
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
118
     *
119
     * @return void
120
     */
121
    protected function addPaymentTransfer(
122
        QuoteTransfer $quoteTransfer,
123
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
124
    ): void {
125
        $brainTreePaymentTransfer = new BraintreePaymentTransfer();
126
        $brainTreePaymentTransfer->setNonce($paypalExpressSuccessResponseTransfer->getNonce());
127
        $brainTreePaymentTransfer->setBillingAddress($quoteTransfer->getBillingAddress());
128
        $brainTreePaymentTransfer->setLanguageIso2Code(strtolower($quoteTransfer->getBillingAddress()->getIso2Code()));
129
        $brainTreePaymentTransfer->setCurrencyIso3Code($paypalExpressSuccessResponseTransfer->getCurrency());
130
        $brainTreePaymentTransfer->setEmail($quoteTransfer->getShippingAddress()->getEmail());
131
132
        $paymentTransfer = new PaymentTransfer();
133
        $paymentTransfer->setPaymentProvider(BraintreeConfig::PROVIDER_NAME);
134
        $paymentTransfer->setBraintreePayPalExpress($brainTreePaymentTransfer);
135
        $paymentTransfer->setBraintree($brainTreePaymentTransfer);
136
        $paymentTransfer->setPaymentSelection(PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS);
137
        $paymentTransfer->setAmount($this->moneyPlugin->convertDecimalToInteger($paypalExpressSuccessResponseTransfer->getAmount()));
138
139
        $quoteTransfer->setPayment($paymentTransfer);
140
    }
141
142
    /**
143
     * @param string $iso2Code
144
     *
145
     * @return \Generated\Shared\Transfer\CountryTransfer|null
146
     */
147
    protected function findCountryTransfer(string $iso2Code): ?CountryTransfer
148
    {
149
        $countryTransfer = (new CountryTransfer())
150
            ->setIso2Code($iso2Code);
151
152
        $countryCollectionTransfer = new CountryCollectionTransfer();
153
        $countryCollectionTransfer->addCountries($countryTransfer);
154
        $countryCollectionTransfer = $this->countryClient->findCountriesByIso2Codes($countryCollectionTransfer);
155
156
        return array_shift($countryCollectionTransfer->getCountries());
0 ignored issues
show
Bug introduced by
$countryCollectionTransfer->getCountries() cannot be passed to array_shift() as the parameter $array expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
        return array_shift(/** @scrutinizer ignore-type */ $countryCollectionTransfer->getCountries());
Loading history...
157
    }
158
159
    /**
160
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
161
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
162
     *
163
     * @return void
164
     */
165
    protected function addShippingAddress(
166
        QuoteTransfer $quoteTransfer,
167
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
168
    ): void {
169
        if (!$quoteTransfer->getBillingAddress()) {
170
            $quoteTransfer->setBillingAddress($this->getAddressTransfer($paypalExpressSuccessResponseTransfer));
171
        }
172
    }
173
174
    /**
175
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
176
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
177
     *
178
     * @return void
179
     */
180
    protected function addBillingTransfer(
181
        QuoteTransfer $quoteTransfer,
182
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
183
    ): void {
184
        if (!$quoteTransfer->getBillingAddress()) {
185
            $quoteTransfer->setBillingAddress($this->getAddressTransfer($paypalExpressSuccessResponseTransfer));
186
        }
187
    }
188
189
    /**
190
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
191
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
192
     *
193
     * @return void
194
     */
195
    protected function addCustomer(
196
        QuoteTransfer $quoteTransfer,
197
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
198
    ): void {
199
        $customerTransfer = $quoteTransfer->getCustomer() ?? new CustomerTransfer();
200
201
        $quoteTransfer->getCustomer()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
202
        $quoteTransfer->getCustomer()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
203
        $quoteTransfer->getCustomer()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
204
205
        if (!$quoteTransfer->getCustomer()->getIdCustomer()) {
206
            $quoteTransfer->getCustomer()->setIsGuest(true);
207
        }
208
209
        $quoteTransfer->setCustomer($customerTransfer);
210
    }
211
212
    /**
213
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
214
     *
215
     * @return \Generated\Shared\Transfer\AddressTransfer
216
     */
217
    protected function getAddressTransfer(
218
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
219
    ): AddressTransfer {
220
        $addressTransfer = new AddressTransfer();
221
        $addressTransfer->fromArray($paypalExpressSuccessResponseTransfer->toArray());
222
        $addressTransfer->setAddress1($paypalExpressSuccessResponseTransfer->getLine1());
223
        $addressTransfer->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
224
225
        $countryTransfer = $this->findCountryTransfer($paypalExpressSuccessResponseTransfer->getCountryCode());
226
        if ($countryTransfer) {
227
            $addressTransfer->setCountry($countryTransfer);
228
            $addressTransfer->setIso2Code($countryTransfer->getIso2Code());
229
        }
230
231
        return $addressTransfer;
232
    }
233
}
234