AddressDataRequest::getValueForAddress()   C
last analyzed

Complexity

Conditions 12
Paths 40

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 23
c 1
b 0
f 0
nc 40
nop 2
dl 0
loc 34
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace Moip\Magento2\Gateway\Request;
10
11
use Magento\Payment\Gateway\Request\BuilderInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Request\BuilderInterface 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 Moip\Magento2\Gateway\Config\Config;
13
use Moip\Magento2\Gateway\Data\Order\OrderAdapterFactory;
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Da...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...
14
use Moip\Magento2\Gateway\SubjectReader;
15
16
/**
17
 * Class AddressDataRequest - Address structure.
18
 */
19
class AddressDataRequest implements BuilderInterface
20
{
21
    /**
22
     * BillingAddress block name.
23
     */
24
    const BILLING_ADDRESS = 'billingAddresses';
25
26
    /**
27
     * BillingAddress block name.
28
     */
29
    const SHIPPING_ADDRESS = 'shippingAddress';
30
31
    /**
32
     * The street address. Maximum 255 characters
33
     * Required.
34
     */
35
    const STREET = 'street';
36
37
    /**
38
     * The street number. 1 or 10 alphanumeric digits
39
     * Required.
40
     */
41
    const STREET_NUMBER = 'streetNumber';
42
43
    /**
44
     * The district address. Maximum 255 characters
45
     * Required.
46
     */
47
    const STREET_DISTRICT = 'district';
48
49
    /**
50
     * The complement address. Maximum 255 characters
51
     * Required.
52
     */
53
    const STREET_COMPLEMENT = 'complement';
54
55
    /**
56
     * The postal code.
57
     * Required.
58
     */
59
    const POSTAL_CODE = 'zipCode';
60
61
    /**
62
     * The ISO 3166-1 alpha-3.
63
     * Required.
64
     */
65
    const COUNTRY_CODE = 'country';
66
67
    /**
68
     * The locality/city. 255 character maximum.
69
     * Required.
70
     */
71
    const LOCALITY = 'city';
72
73
    /**
74
     * The state or province. The region must be a 2-letter abbreviation.
75
     * Required.
76
     */
77
    const STATE = 'state';
78
79
    /**
80
     * @var SubjectReader
81
     */
82
    private $subjectReader;
83
84
    /**
85
     * @var OrderAdapterFactory
86
     */
87
    private $orderAdapterFactory;
88
89
    /**
90
     * @var Config
91
     */
92
    private $config;
93
94
    /**
95
     * @param SubjectReader       $subjectReader
96
     * @param OrderAdapterFactory $orderAdapterFactory
97
     * @param Config              $config
98
     */
99
    public function __construct(
100
        SubjectReader $subjectReader,
101
        OrderAdapterFactory $orderAdapterFactory,
102
        Config $config
103
    ) {
104
        $this->subjectReader = $subjectReader;
105
        $this->orderAdapterFactory = $orderAdapterFactory;
106
        $this->config = $config;
107
    }
108
109
    /**
110
     * Value For Field Address.
111
     *
112
     * @param $adress
113
     * @param $field
114
     *
115
     * @return string|null
116
     */
117
    public function getValueForAddress($adress, $field)
118
    {
119
        $value = (int) $this->config->getAddtionalValue($field);
120
121
        if ($field === self::STREET) {
122
            $limitSend = 57;
123
        } elseif ($field === self::STREET_NUMBER) {
124
            $limitSend = 6;
125
        } elseif ($field === self::STREET_DISTRICT) {
126
            $limitSend = 60;
127
        } elseif ($field === self::STREET_COMPLEMENT) {
128
            $limitSend = 30;
129
        }
130
131
        if ($value === 0) {
132
            return substr($adress->getStreetLine1(), 0, $limitSend);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $limitSend does not seem to be defined for all execution paths leading up to this point.
Loading history...
133
        } elseif ($value === 1) {
134
            return substr($adress->getStreetLine2(), 0, $limitSend);
135
        } elseif ($value === 2) {
136
            return substr($adress->getStreetLine3(), 0, $limitSend);
137
        } elseif ($value === 3) {
138
            /** contigência para sempre haver o bairro */
139
            if ($adress->getStreetLine4()) {
140
                return substr($adress->getStreetLine4(), 0, $limitSend);
141
            }
142
            if ($adress->getStreetLine3()) {
143
                return substr($adress->getStreetLine3(), 0, $limitSend);
144
            }
145
            if ($adress->getStreetLine1()) {
146
                return substr($adress->getStreetLine1(), 0, $limitSend);
147
            }
148
        }
149
150
        return substr($adress->getStreetLine1(), 0, $limitSend);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function build(array $buildSubject)
157
    {
158
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
159
        $payment = $paymentDO->getPayment();
160
161
        $result = [];
162
163
        $orderAdapter = $this->orderAdapterFactory->create(
164
            ['order' => $payment->getOrder()]
165
        );
166
167
        $billingAddress = $orderAdapter->getBillingAddress();
168
        if ($billingAddress) {
169
            $result[CustomerDataRequest::CUSTOMER][self::BILLING_ADDRESS] = [
170
                self::POSTAL_CODE       => $billingAddress->getPostcode(),
171
                self::STREET            => $this->getValueForAddress($billingAddress, self::STREET),
172
                self::STREET_NUMBER     => $this->getValueForAddress($billingAddress, self::STREET_NUMBER),
173
                self::STREET_DISTRICT   => $this->getValueForAddress($billingAddress, self::STREET_DISTRICT),
174
                self::STREET_COMPLEMENT => $this->getValueForAddress($billingAddress, self::STREET_COMPLEMENT),
175
                self::LOCALITY          => $billingAddress->getCity(),
176
                self::STATE             => $billingAddress->getRegionCode(),
177
                self::COUNTRY_CODE      => 'BRA',
178
            ];
179
        }
180
181
        $shippingAddress = $orderAdapter->getShippingAddress();
182
        if ($shippingAddress) {
183
            $result[CustomerDataRequest::CUSTOMER][self::SHIPPING_ADDRESS] = [
184
                self::POSTAL_CODE       => $shippingAddress->getPostcode(),
185
                self::STREET            => $this->getValueForAddress($shippingAddress, self::STREET),
186
                self::STREET_NUMBER     => $this->getValueForAddress($shippingAddress, self::STREET_NUMBER),
187
                self::STREET_DISTRICT   => $this->getValueForAddress($shippingAddress, self::STREET_DISTRICT),
188
                self::STREET_COMPLEMENT => $this->getValueForAddress($shippingAddress, self::STREET_COMPLEMENT),
189
                self::LOCALITY          => $shippingAddress->getCity(),
190
                self::STATE             => $shippingAddress->getRegionCode(),
191
                self::COUNTRY_CODE      => 'BRA',
192
            ];
193
        }
194
195
        return $result;
196
    }
197
}
198