Passed
Pull Request — master (#9)
by Volodymyr
04:30
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
namespace SprykerEco\Yves\Braintree\Model\Mapper\PaypalResponse;
4
5
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...
6
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...
7
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...
8
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...
9
use Generated\Shared\Transfer\ShipmentCarrierTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentCarrierTransfer 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...
10
use Generated\Shared\Transfer\ShipmentMethodTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentMethodTransfer 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\ShipmentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentTransfer 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 SprykerEco\Yves\Braintree\BraintreeConfig;
13
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface;
14
15
class PaypalResponseMapper implements PaypalResponseMapperInterface
16
{
17
    protected const KEY_NONCE = 'nonce';
18
    protected const KEY_DETAILS = 'details';
19
    protected const KEY_EMAIL = 'email';
20
    protected const KEY_FIRST_NAME = 'firstName';
21
    protected const KEY_LAST_NAME = 'lastName';
22
    protected const KEY_PAYER_ID = 'payerId';
23
    protected const KEY_SHIPPING_ADDRESS = 'shippingAddress';
24
    protected const KEY_RECIPIENT_NAME = 'recipientName';
25
    protected const KEY_LINE1 = 'line1';
26
    protected const KEY_CITY = 'city';
27
    protected const KEY_STATE = 'state';
28
    protected const KEY_POSTAL_CODE = 'postalCode';
29
    protected const KEY_COUNTRY_CODE = 'countryCode';
30
31
    /**
32
     * @var BraintreeToShipmentClientInterface
33
     */
34
    protected $shipmentClient;
35
36
    /**
37
     * @var BraintreeConfig
38
     */
39
    protected $braintreeConfig;
40
41
    /**
42
     * @param BraintreeToShipmentClientInterface $shipmentClient
43
     * @param BraintreeConfig $braintreeConfig
44
     */
45
    public function __construct(
46
        BraintreeToShipmentClientInterface $shipmentClient,
47
        BraintreeConfig $braintreeConfig
48
    ) {
49
        $this->shipmentClient = $shipmentClient;
50
        $this->braintreeConfig = $braintreeConfig;
51
    }
52
53
    /**
54
     * @param array $payload
55
     *
56
     * @return PaypalExpressSuccessResponseTransfer
57
     */
58
    public function mapSuccessResponseToPaypalExpressSuccessResponseTransfer(array $payload): PaypalExpressSuccessResponseTransfer
59
    {
60
        $transfer = new PaypalExpressSuccessResponseTransfer();
61
62
        $transfer->setNonce($payload[static::KEY_NONCE] ?? null);
63
        $transfer->setEmail($payload[static::KEY_DETAILS][static::KEY_EMAIL] ?? null);
64
        $transfer->setFirstName($payload[static::KEY_DETAILS][static::KEY_FIRST_NAME] ?? null);
65
        $transfer->setLastName($payload[static::KEY_DETAILS][static::KEY_LAST_NAME] ?? null);
66
        $transfer->setPayerId($payload[static::KEY_DETAILS][static::KEY_PAYER_ID] ?? null);
67
        $transfer->setRecipientName($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_RECIPIENT_NAME] ?? null);
68
        $transfer->setLine1($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_LINE1] ?? null);
69
        $transfer->setCity($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_CITY] ?? null);
70
        $transfer->setState($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_STATE] ?? null);
71
        $transfer->setPostalCode($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_POSTAL_CODE] ?? null);
72
        $transfer->setCountryCode($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_COUNTRY_CODE] ?? null);
73
74
        return $transfer;
75
    }
76
77
    /**
78
     * @param PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
79
     * @param QuoteTransfer $quoteTransfer
80
     *
81
     * @return QuoteTransfer
82
     */
83
    public function mapPaypalExpressSuccessResponseTransferToQuoteTransfer(
84
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer,
85
        QuoteTransfer $quoteTransfer
86
    ): QuoteTransfer {
87
        $customerTransfer = $quoteTransfer->getCustomer() ?? new CustomerTransfer();
88
        $shippingAddressTransfer = $quoteTransfer->getShippingAddress() ?? new AddressTransfer();
89
90
        $quoteTransfer->setCustomer($customerTransfer);
91
        $quoteTransfer->setShippingAddress($shippingAddressTransfer);
92
93
        $quoteTransfer->getCustomer()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
94
        $quoteTransfer->getCustomer()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
95
        $quoteTransfer->getCustomer()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
96
        $quoteTransfer->getShippingAddress()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
97
        $quoteTransfer->getShippingAddress()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
98
        $quoteTransfer->getShippingAddress()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
99
        $quoteTransfer->getShippingAddress()->setAddress1($paypalExpressSuccessResponseTransfer->getLine1());
100
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
101
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
102
        $quoteTransfer->getShippingAddress()->setState($paypalExpressSuccessResponseTransfer->getState());
103
        $quoteTransfer->getShippingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
104
        $quoteTransfer->getShippingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
105
        $quoteTransfer->setBillingSameAsShipping(true);
106
        $quoteTransfer->setShipment($this->getShipmentTransfer($quoteTransfer));
107
108
        //TODO: Get country code
109
        //TODO: Nonce, payerId
110
111
112
        return $quoteTransfer;
113
    }
114
115
    /**
116
     * @param QuoteTransfer $quoteTransfer
117
     *
118
     * @return ShipmentTransfer
119
     */
120
    protected function getShipmentTransfer(QuoteTransfer $quoteTransfer): ShipmentTransfer
121
    {
122
        $shipmentMethodsTransfer = $this->shipmentClient->getAvailableMethods($quoteTransfer);
123
        $shipmentMethodTransferSelected = null;
124
125
        foreach ($shipmentMethodsTransfer->getMethods() as $shipmentMethodTransfer) {
126
            if ($this->braintreeConfig->getDefaultPaypalExpressShipmentMethodId() === $shipmentMethodTransfer->getIdShipmentMethod()) {
127
                $shipmentMethodTransferSelected = $shipmentMethodTransfer;
128
            }
129
        }
130
131
        $shipmentTransfer = new ShipmentTransfer();
132
        $shipmentTransfer->setMethod($shipmentMethodTransferSelected);
133
        $shipmentTransfer->setCarrier($this->createShipmentCarrierTransfer($shipmentMethodTransferSelected));
134
135
        return $shipmentTransfer;
136
    }
137
138
139
    /**
140
     * @param ShipmentMethodTransfer $shipmentMethodTransfer
141
     *
142
     * @return ShipmentCarrierTransfer
143
     */
144
    protected function createShipmentCarrierTransfer(ShipmentMethodTransfer $shipmentMethodTransfer): ShipmentCarrierTransfer
145
    {
146
        $shipmentCarrierTransfer = new ShipmentCarrierTransfer();
147
148
        $shipmentCarrierTransfer->setName($shipmentMethodTransfer->getCarrierName() ?? null);
149
        $shipmentCarrierTransfer->setIdShipmentCarrier($shipmentMethodTransfer->getFkShipmentCarrier() ?? null);
150
        $shipmentCarrierTransfer->setIsActive(true);
151
152
        return $shipmentCarrierTransfer;
153
    }
154
}