Passed
Pull Request — master (#9)
by Volodymyr
17:33 queued 09:33
created

mapPaypalExpressSuccessResponseTransferToQuoteTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 2
dl 0
loc 30
rs 9.6333
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\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...
13
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...
14
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...
15
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...
16
use SprykerEco\Shared\Braintree\BraintreeConfig;
17
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface;
18
19
class PaypalResponseMapper implements PaypalResponseMapperInterface
20
{
21
    protected const KEY_NONCE = 'nonce';
22
    protected const KEY_DETAILS = 'details';
23
    protected const KEY_EMAIL = 'email';
24
    protected const KEY_FIRST_NAME = 'firstName';
25
    protected const KEY_LAST_NAME = 'lastName';
26
    protected const KEY_PAYER_ID = 'payerId';
27
    protected const KEY_SHIPPING_ADDRESS = 'shippingAddress';
28
    protected const KEY_RECIPIENT_NAME = 'recipientName';
29
    protected const KEY_LINE1 = 'line1';
30
    protected const KEY_CITY = 'city';
31
    protected const KEY_STATE = 'state';
32
    protected const KEY_POSTAL_CODE = 'postalCode';
33
    protected const KEY_COUNTRY_CODE = 'countryCode';
34
35
    /**
36
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface
37
     */
38
    protected $paymentClient;
39
40
    /**
41
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface $paymentClient
42
     */
43
    public function __construct(
44
        BraintreeToPaymentClientInterface $paymentClient
45
    ) {
46
        $this->paymentClient = $paymentClient;
47
    }
48
49
    /**
50
     * @param array $payload
51
     *
52
     * @return \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer
53
     */
54
    public function mapSuccessResponseToPaypalExpressSuccessResponseTransfer(array $payload): PaypalExpressSuccessResponseTransfer
55
    {
56
        $transfer = new PaypalExpressSuccessResponseTransfer();
57
58
        $transfer->setNonce($payload[static::KEY_NONCE] ?? null);
59
        $transfer->setEmail($payload[static::KEY_DETAILS][static::KEY_EMAIL] ?? null);
60
        $transfer->setFirstName($payload[static::KEY_DETAILS][static::KEY_FIRST_NAME] ?? null);
61
        $transfer->setLastName($payload[static::KEY_DETAILS][static::KEY_LAST_NAME] ?? null);
62
        $transfer->setPayerId($payload[static::KEY_DETAILS][static::KEY_PAYER_ID] ?? null);
63
        $transfer->setRecipientName($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_RECIPIENT_NAME] ?? null);
64
        $transfer->setLine1($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_LINE1] ?? null);
65
        $transfer->setCity($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_CITY] ?? null);
66
        $transfer->setState($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_STATE] ?? null);
67
        $transfer->setPostalCode($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_POSTAL_CODE] ?? null);
68
        $transfer->setCountryCode($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_COUNTRY_CODE] ?? null);
69
70
        return $transfer;
71
    }
72
73
    /**
74
     * @param \Generated\Shared\Transfer\PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
75
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
76
     *
77
     * @return \Generated\Shared\Transfer\QuoteTransfer
78
     */
79
    public function mapPaypalExpressSuccessResponseTransferToQuoteTransfer(
80
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer,
81
        QuoteTransfer $quoteTransfer
82
    ): QuoteTransfer {
83
        $customerTransfer = $quoteTransfer->getCustomer() ?? new CustomerTransfer();
84
        $shippingAddressTransfer = $quoteTransfer->getShippingAddress() ?? new AddressTransfer();
85
86
        $quoteTransfer->setCustomer($customerTransfer);
87
        $quoteTransfer->setShippingAddress($shippingAddressTransfer);
88
89
        $quoteTransfer->getCustomer()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
90
        $quoteTransfer->getCustomer()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
91
        $quoteTransfer->getCustomer()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
92
        $quoteTransfer->getShippingAddress()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
93
        $quoteTransfer->getShippingAddress()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
94
        $quoteTransfer->getShippingAddress()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
95
        $quoteTransfer->getShippingAddress()->setAddress1($paypalExpressSuccessResponseTransfer->getLine1());
96
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
97
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
98
        $quoteTransfer->getShippingAddress()->setState($paypalExpressSuccessResponseTransfer->getState());
99
        $quoteTransfer->getShippingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
100
        $quoteTransfer->getShippingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
101
        $quoteTransfer->setBillingSameAsShipping(true);
102
103
        $this->addPaymentTransfer($quoteTransfer);
104
105
        //TODO: Get country code
106
        //TODO: Nonce, payerId
107
108
        return $quoteTransfer;
109
    }
110
111
    /**
112
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
113
     *
114
     * @return void
115
     */
116
    protected function addPaymentTransfer(QuoteTransfer $quoteTransfer): void
117
    {
118
        $paymentTransfer = new PaymentTransfer();
119
        $paymentTransfer->setPaymentProvider(BraintreeConfig::PROVIDER_NAME);
120
121
        $brainTreePaymentTransfer = new BraintreePaymentTransfer();
122
        $paymentTransfer->setBraintreePayPalExpress($brainTreePaymentTransfer);
123
124
        $quoteTransfer->setPayment($paymentTransfer);
125
    }
126
}
127