AddressDataRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
c 1
b 0
f 0
dl 0
loc 142
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 47 5
A __construct() 0 8 1
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
namespace Getnet\PaymentMagento\Gateway\Request;
10
11
use Getnet\PaymentMagento\Gateway\Config\Config;
12
use Getnet\PaymentMagento\Gateway\Data\Order\OrderAdapterFactory;
0 ignored issues
show
Bug introduced by
The type Getnet\PaymentMagento\Ga...der\OrderAdapterFactory 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 Getnet\PaymentMagento\Gateway\SubjectReader;
14
use InvalidArgumentException;
15
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
16
use Magento\Payment\Gateway\Request\BuilderInterface;
17
18
/**
19
 * Class Address Data Request - Address structure.
20
 */
21
class AddressDataRequest implements BuilderInterface
22
{
23
    /**
24
     * Billing Address block name.
25
     */
26
    public const BILLING_ADDRESS = 'billing_address';
27
28
    /**
29
     * Shipping Address block name.
30
     */
31
    public const SHIPPING_ADDRESS = 'shipping_address';
32
33
    /**
34
     * The street address. Maximum 255 characters
35
     * Required.
36
     */
37
    public const STREET = 'street';
38
39
    /**
40
     * The number. 1 or 10 alphanumeric digits
41
     * Required.
42
     */
43
    public const NUMBER = 'number';
44
45
    /**
46
     * The complement address. Maximum 255 characters
47
     * Required.
48
     */
49
    public const COMPLEMENT = 'complement';
50
51
    /**
52
     * The district address. Maximum 255 characters
53
     * Required.
54
     */
55
    public const DISTRICT = 'district';
56
57
    /**
58
     * The locality/city. 255 character maximum.
59
     * Required.
60
     */
61
    public const LOCALITY = 'city';
62
63
    /**
64
     * The state or province. The region must be a 2-letter abbreviation.
65
     * Required.
66
     */
67
    public const STATE = 'state';
68
69
    /**
70
     * The ISO 3166-1 alpha-3.
71
     * Required.
72
     */
73
    public const COUNTRY_CODE = 'country';
74
75
    /**
76
     * The postal code.
77
     * Required.
78
     */
79
    public const POSTAL_CODE = 'postal_code';
80
81
    /**
82
     * @var SubjectReader
83
     */
84
    protected $subjectReader;
85
86
    /**
87
     * @var Config
88
     */
89
    protected $config;
90
91
    /**
92
     * @var OrderAdapterFactory;
93
     */
94
    protected $orderAdapterFactory;
95
96
    /**
97
     * @param SubjectReader       $subjectReader
98
     * @param Config              $config
99
     * @param OrderAdapterFactory $orderAdapterFactory
100
     */
101
    public function __construct(
102
        SubjectReader $subjectReader,
103
        Config $config,
104
        OrderAdapterFactory $orderAdapterFactory
105
    ) {
106
        $this->subjectReader = $subjectReader;
107
        $this->config = $config;
108
        $this->orderAdapterFactory = $orderAdapterFactory;
109
    }
110
111
    /**
112
     * Build.
113
     *
114
     * @param array $buildSubject
115
     */
116
    public function build(array $buildSubject): array
117
    {
118
        if (!isset($buildSubject['payment'])
119
            || !$buildSubject['payment'] instanceof PaymentDataObjectInterface
120
        ) {
121
            throw new InvalidArgumentException('Payment data object should be provided');
122
        }
123
124
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
125
        $payment = $paymentDO->getPayment();
126
127
        $result = [];
128
129
        /** @var OrderAdapterFactory $orderAdapter * */
130
        $orderAdapter = $this->orderAdapterFactory->create(
131
            ['order' => $payment->getOrder()]
132
        );
133
134
        $billingAddress = $orderAdapter->getBillingAddress();
135
        if ($billingAddress) {
136
            $result[CustomerDataRequest::CUSTOMER][self::BILLING_ADDRESS] = [
137
                self::POSTAL_CODE       => $billingAddress->getPostcode(),
138
                self::STREET            => $this->config->getValueForAddress($billingAddress, self::STREET),
139
                self::NUMBER            => $this->config->getValueForAddress($billingAddress, self::NUMBER),
140
                self::DISTRICT          => $this->config->getValueForAddress($billingAddress, self::DISTRICT),
141
                self::COMPLEMENT        => $this->config->getValueForAddress($billingAddress, self::COMPLEMENT),
142
                self::LOCALITY          => $billingAddress->getCity(),
143
                self::STATE             => $billingAddress->getRegionCode(),
144
                self::COUNTRY_CODE      => 'BRA',
145
            ];
146
        }
147
148
        $shippingAddress = $orderAdapter->getShippingAddress();
149
        if ($shippingAddress) {
150
            $result[CustomerDataRequest::CUSTOMER][self::SHIPPING_ADDRESS] = [
151
                self::POSTAL_CODE       => $shippingAddress->getPostcode(),
152
                self::STREET            => $this->config->getValueForAddress($shippingAddress, self::STREET),
153
                self::NUMBER            => $this->config->getValueForAddress($shippingAddress, self::NUMBER),
154
                self::DISTRICT          => $this->config->getValueForAddress($shippingAddress, self::DISTRICT),
155
                self::COMPLEMENT        => $this->config->getValueForAddress($shippingAddress, self::COMPLEMENT),
156
                self::LOCALITY          => $shippingAddress->getCity(),
157
                self::STATE             => $shippingAddress->getRegionCode(),
158
                self::COUNTRY_CODE      => 'BRA',
159
            ];
160
        }
161
162
        return $result;
163
    }
164
}
165