Passed
Pull Request — master (#9)
by Volodymyr
05:00
created

mapSuccessResponseToPaypalExpressSuccessResponseTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 17
rs 9.8333
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 SprykerEco\Shared\Braintree\BraintreeConfig;
19
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToCountryClientInterface;
20
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface;
21
22
class PaypalResponseMapper implements PaypalResponseMapperInterface
23
{
24
    protected const KEY_NONCE = 'nonce';
25
    protected const KEY_DETAILS = 'details';
26
    protected const KEY_EMAIL = 'email';
27
    protected const KEY_FIRST_NAME = 'firstName';
28
    protected const KEY_LAST_NAME = 'lastName';
29
    protected const KEY_PAYER_ID = 'payerId';
30
    protected const KEY_SHIPPING_ADDRESS = 'shippingAddress';
31
    protected const KEY_RECIPIENT_NAME = 'recipientName';
32
    protected const KEY_LINE1 = 'line1';
33
    protected const KEY_CITY = 'city';
34
    protected const KEY_STATE = 'state';
35
    protected const KEY_POSTAL_CODE = 'postalCode';
36
    protected const KEY_COUNTRY_CODE = 'countryCode';
37
38
    /**
39
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface
40
     */
41
    protected $paymentClient;
42
43
    /**
44
     * @var BraintreeToCountryClientInterface
45
     */
46
    protected $countryClient;
47
48
    /**
49
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface $paymentClient
50
     * @param BraintreeToCountryClientInterface $countryClient
51
     */
52
    public function __construct(
53
        BraintreeToPaymentClientInterface $paymentClient,
54
        BraintreeToCountryClientInterface $countryClient
55
    ) {
56
        $this->paymentClient = $paymentClient;
57
        $this->countryClient = $countryClient;
58
    }
59
60
    /**
61
     * @param array $payload
62
     *
63
     * @return \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer
64
     */
65
    public function mapSuccessResponseToPaypalExpressSuccessResponseTransfer(array $payload): PaypalExpressSuccessResponseTransfer
66
    {
67
        $transfer = new PaypalExpressSuccessResponseTransfer();
68
69
        $transfer->setNonce($payload[static::KEY_NONCE] ?? null);
70
        $transfer->setEmail($payload[static::KEY_DETAILS][static::KEY_EMAIL] ?? null);
71
        $transfer->setFirstName($payload[static::KEY_DETAILS][static::KEY_FIRST_NAME] ?? null);
72
        $transfer->setLastName($payload[static::KEY_DETAILS][static::KEY_LAST_NAME] ?? null);
73
        $transfer->setPayerId($payload[static::KEY_DETAILS][static::KEY_PAYER_ID] ?? null);
74
        $transfer->setRecipientName($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_RECIPIENT_NAME] ?? null);
75
        $transfer->setLine1($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_LINE1] ?? null);
76
        $transfer->setCity($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_CITY] ?? null);
77
        $transfer->setState($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_STATE] ?? null);
78
        $transfer->setPostalCode($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_POSTAL_CODE] ?? null);
79
        $transfer->setCountryCode($payload[static::KEY_DETAILS][static::KEY_SHIPPING_ADDRESS][static::KEY_COUNTRY_CODE] ?? null);
80
81
        return $transfer;
82
    }
83
84
    /**
85
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
86
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
87
     *
88
     * @return \Generated\Shared\Transfer\QuoteTransfer
89
     */
90
    public function mapPaypalExpressSuccessResponseTransferToQuoteTransfer(
91
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer,
92
        QuoteTransfer $quoteTransfer
93
    ): QuoteTransfer {
94
        $customerTransfer = $quoteTransfer->getCustomer() ?? new CustomerTransfer();
95
        $shippingAddressTransfer = $quoteTransfer->getShippingAddress() ?? new AddressTransfer();
96
        $countryTransfer = $this->getCountryTransfer($paypalExpressSuccessResponseTransfer->getCountryCode());
97
98
        $quoteTransfer->setCustomer($customerTransfer);
99
        $quoteTransfer->setShippingAddress($shippingAddressTransfer);
100
101
        $quoteTransfer->getCustomer()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
102
        $quoteTransfer->getCustomer()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
103
        $quoteTransfer->getCustomer()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
104
        $quoteTransfer->getShippingAddress()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
105
        $quoteTransfer->getShippingAddress()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
106
        $quoteTransfer->getShippingAddress()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
107
        $quoteTransfer->getShippingAddress()->setAddress1($paypalExpressSuccessResponseTransfer->getLine1());
108
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
109
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
110
        $quoteTransfer->getShippingAddress()->setState($paypalExpressSuccessResponseTransfer->getState());
111
        $quoteTransfer->getShippingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
112
        $quoteTransfer->getShippingAddress()->setCountry($countryTransfer);
113
        $quoteTransfer->getShippingAddress()->setIso2Code($countryTransfer->getIso2Code());
114
115
        $quoteTransfer->getBillingAddress()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
116
        $quoteTransfer->getBillingAddress()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
117
        $quoteTransfer->getBillingAddress()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
118
        $quoteTransfer->getBillingAddress()->setAddress1($paypalExpressSuccessResponseTransfer->getLine1());
119
        $quoteTransfer->getBillingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
120
        $quoteTransfer->getBillingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
121
        $quoteTransfer->getBillingAddress()->setState($paypalExpressSuccessResponseTransfer->getState());
122
        $quoteTransfer->getBillingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
123
        $quoteTransfer->getBillingAddress()->setCountry($countryTransfer);
124
        $quoteTransfer->getBillingAddress()->setIso2Code($countryTransfer->getIso2Code());
125
126
127
        $this->addPaymentTransfer($quoteTransfer, $paypalExpressSuccessResponseTransfer);
128
129
        return $quoteTransfer;
130
    }
131
132
    /**
133
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
134
     * @param PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
135
     *
136
     * @return void
137
     */
138
    protected function addPaymentTransfer(
139
        QuoteTransfer $quoteTransfer,
140
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer): void
141
    {
142
        $brainTreePaymentTransfer = new BraintreePaymentTransfer();
143
        $brainTreePaymentTransfer->setNonce($paypalExpressSuccessResponseTransfer->getNonce());
144
        $brainTreePaymentTransfer->setBillingAddress($quoteTransfer->getBillingAddress());
145
        $brainTreePaymentTransfer->setClientIp('ip'); //TODO: ???
146
        $brainTreePaymentTransfer->setLanguageIso2Code(strtolower($quoteTransfer->getBillingAddress()->getIso2Code()));
147
        $brainTreePaymentTransfer->setCurrencyIso3Code('EUR'); // TODO: ???
148
        $brainTreePaymentTransfer->setEmail($quoteTransfer->getShippingAddress()->getEmail());
149
150
        $paymentTransfer = new PaymentTransfer();
151
        $paymentTransfer->setPaymentProvider(BraintreeConfig::PROVIDER_NAME);
152
        $paymentTransfer->setBraintreePayPalExpress($brainTreePaymentTransfer);
153
        $paymentTransfer->setBraintree($brainTreePaymentTransfer);
154
        $paymentTransfer->setPaymentSelection(PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS);
155
        $paymentTransfer->setAmount(72335); //TODO: ???
156
157
        $quoteTransfer->setPayment($paymentTransfer);
158
    }
159
160
    /**
161
     * @param string $iso2Code
162
     *
163
     * @return CountryTransfer|null
164
     */
165
    protected function getCountryTransfer(string $iso2Code): ?CountryTransfer
166
    {
167
        $countryTransfer = (new CountryTransfer())
168
            ->setIso2Code($iso2Code);
169
170
        $countryCollectionTransfer = new CountryCollectionTransfer();
171
        $countryCollectionTransfer->addCountries($countryTransfer);
172
173
        $countries = $this->countryClient->findCountriesByIso2Codes($countryCollectionTransfer);
174
175
        foreach ($countries->getCountries() as $country) {
176
            return $country;
177
        }
178
179
        return null;
180
    }
181
}
182