Passed
Pull Request — master (#9)
by Volodymyr
04:52
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
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\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...
7
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...
8
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...
9
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...
10
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...
11
use SprykerEco\Shared\Braintree\BraintreeConfig;
12
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToPaymentClientInterface;
13
14
class PaypalResponseMapper implements PaypalResponseMapperInterface
15
{
16
    protected const KEY_NONCE = 'nonce';
17
    protected const KEY_DETAILS = 'details';
18
    protected const KEY_EMAIL = 'email';
19
    protected const KEY_FIRST_NAME = 'firstName';
20
    protected const KEY_LAST_NAME = 'lastName';
21
    protected const KEY_PAYER_ID = 'payerId';
22
    protected const KEY_SHIPPING_ADDRESS = 'shippingAddress';
23
    protected const KEY_RECIPIENT_NAME = 'recipientName';
24
    protected const KEY_LINE1 = 'line1';
25
    protected const KEY_CITY = 'city';
26
    protected const KEY_STATE = 'state';
27
    protected const KEY_POSTAL_CODE = 'postalCode';
28
    protected const KEY_COUNTRY_CODE = 'countryCode';
29
30
    /**
31
     * @var BraintreeToPaymentClientInterface
32
     */
33
    protected $paymentClient;
34
35
    /**
36
     * @param BraintreeToPaymentClientInterface $paymentClient
37
     */
38
    public function __construct(
39
        BraintreeToPaymentClientInterface $paymentClient
40
    ) {
41
        $this->paymentClient = $paymentClient;
42
    }
43
44
    /**
45
     * @param array $payload
46
     *
47
     * @return PaypalExpressSuccessResponseTransfer
48
     */
49
    public function mapSuccessResponseToPaypalExpressSuccessResponseTransfer(array $payload): PaypalExpressSuccessResponseTransfer
50
    {
51
        $transfer = new PaypalExpressSuccessResponseTransfer();
52
53
        $transfer->setNonce($payload[static::KEY_NONCE] ?? null);
54
        $transfer->setEmail($payload[static::KEY_DETAILS][static::KEY_EMAIL] ?? null);
55
        $transfer->setFirstName($payload[static::KEY_DETAILS][static::KEY_FIRST_NAME] ?? null);
56
        $transfer->setLastName($payload[static::KEY_DETAILS][static::KEY_LAST_NAME] ?? null);
57
        $transfer->setPayerId($payload[static::KEY_DETAILS][static::KEY_PAYER_ID] ?? null);
58
        $transfer->setRecipientName($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_RECIPIENT_NAME] ?? null);
59
        $transfer->setLine1($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_LINE1] ?? null);
60
        $transfer->setCity($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_CITY] ?? null);
61
        $transfer->setState($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_STATE] ?? null);
62
        $transfer->setPostalCode($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_POSTAL_CODE] ?? null);
63
        $transfer->setCountryCode($payload[static::KEY_SHIPPING_ADDRESS][static::KEY_COUNTRY_CODE] ?? null);
64
65
        return $transfer;
66
    }
67
68
    /**
69
     * @param PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer
70
     * @param QuoteTransfer $quoteTransfer
71
     *
72
     * @return QuoteTransfer
73
     */
74
    public function mapPaypalExpressSuccessResponseTransferToQuoteTransfer(
75
        PaypalExpressSuccessResponseTransfer $paypalExpressSuccessResponseTransfer,
76
        QuoteTransfer $quoteTransfer
77
    ): QuoteTransfer {
78
        $customerTransfer = $quoteTransfer->getCustomer() ?? new CustomerTransfer();
79
        $shippingAddressTransfer = $quoteTransfer->getShippingAddress() ?? new AddressTransfer();
80
81
        $quoteTransfer->setCustomer($customerTransfer);
82
        $quoteTransfer->setShippingAddress($shippingAddressTransfer);
83
84
        $quoteTransfer->getCustomer()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
85
        $quoteTransfer->getCustomer()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
86
        $quoteTransfer->getCustomer()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
87
        $quoteTransfer->getShippingAddress()->setFirstName($paypalExpressSuccessResponseTransfer->getFirstName());
88
        $quoteTransfer->getShippingAddress()->setLastName($paypalExpressSuccessResponseTransfer->getLastName());
89
        $quoteTransfer->getShippingAddress()->setEmail($paypalExpressSuccessResponseTransfer->getEmail());
90
        $quoteTransfer->getShippingAddress()->setAddress1($paypalExpressSuccessResponseTransfer->getLine1());
91
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
92
        $quoteTransfer->getShippingAddress()->setCity($paypalExpressSuccessResponseTransfer->getCity());
93
        $quoteTransfer->getShippingAddress()->setState($paypalExpressSuccessResponseTransfer->getState());
94
        $quoteTransfer->getShippingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
95
        $quoteTransfer->getShippingAddress()->setZipCode($paypalExpressSuccessResponseTransfer->getPostalCode());
96
        $quoteTransfer->setBillingSameAsShipping(true);
97
98
        $quoteTransfer->setPayment();
99
100
        //TODO: Get country code
101
        //TODO: Nonce, payerId
102
103
104
        return $quoteTransfer;
105
    }
106
107
    /**
108
     * @param QuoteTransfer $quoteTransfer
109
     * @return PaymentTransfer
110
     */
111
    protected function getPaymentTransfer(QuoteTransfer $quoteTransfer): PaymentTransfer
112
    {
113
        $paymentTransfer = new PaymentTransfer();
114
        $paymentTransfer->setPaymentProvider(BraintreeConfig::PROVIDER_NAME);
115
116
        $brainTreePaymentTransfer = new BraintreePaymentTransfer();
117
        $paymentTransfer->setBraintreePayPalExpress($brainTreePaymentTransfer);
118
119
        $quoteTransfer->setPayment($paymentTransfer);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Generated\Shared\Transfer\PaymentTransfer. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
120
    }
121
}