BillingAddressDataRequest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 174
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A build() 0 42 4
C getValueForAddress() 0 34 12
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 BillingAddressDataBuilder - Address structure.
18
 */
19
class BillingAddressDataRequest implements BuilderInterface
20
{
21
    /**
22
     * BillingAddress block name.
23
     */
24
    const BILLING_ADDRESS = 'billingAddress';
25
26
    /**
27
     * The street address. Maximum 255 characters
28
     * Required.
29
     */
30
    const STREET = 'street';
31
32
    /**
33
     * The street number. 1 or 10 alphanumeric digits
34
     * Required.
35
     */
36
    const STREET_NUMBER = 'streetNumber';
37
38
    /**
39
     * The district address. Maximum 255 characters
40
     * Required.
41
     */
42
    const STREET_DISTRICT = 'district';
43
44
    /**
45
     * The complement address. Maximum 255 characters
46
     * Required.
47
     */
48
    const STREET_COMPLEMENT = 'complement';
49
50
    /**
51
     * The postal code.
52
     * Required.
53
     */
54
    const POSTAL_CODE = 'zipCode';
55
56
    /**
57
     * The ISO 3166-1 alpha-3.
58
     * Required.
59
     */
60
    const COUNTRY_CODE = 'country';
61
62
    /**
63
     * The locality/city. 255 character maximum.
64
     * Required.
65
     */
66
    const LOCALITY = 'city';
67
68
    /**
69
     * The state or province. The region must be a 2-letter abbreviation.
70
     * Required.
71
     */
72
    const STATE = 'state';
73
74
    /**
75
     * @var SubjectReader
76
     */
77
    private $subjectReader;
78
79
    /**
80
     * @var OrderAdapterFactory
81
     */
82
    private $orderAdapterFactory;
83
84
    /**
85
     * @var Config
86
     */
87
    private $config;
88
89
    /**
90
     * @param SubjectReader       $subjectReader
91
     * @param OrderAdapterFactory $orderAdapterFactory
92
     * @param Config              $config
93
     */
94
    public function __construct(
95
        SubjectReader $subjectReader,
96
        OrderAdapterFactory $orderAdapterFactory,
97
        Config $config
98
    ) {
99
        $this->subjectReader = $subjectReader;
100
        $this->orderAdapterFactory = $orderAdapterFactory;
101
        $this->config = $config;
102
    }
103
104
    /**
105
     * Value For Field Address.
106
     *
107
     * @param $adress
108
     * @param $field
109
     *
110
     * @return string value
111
     */
112
    public function getValueForAddress($adress, $field)
113
    {
114
        $value = (int) $this->config->getAddtionalValue($field);
115
116
        if ($field === self::STREET) {
117
            $limitSend = 57;
118
        } elseif ($field === self::STREET_NUMBER) {
119
            $limitSend = 6;
120
        } elseif ($field === self::STREET_DISTRICT) {
121
            $limitSend = 60;
122
        } elseif ($field === self::STREET_COMPLEMENT) {
123
            $limitSend = 30;
124
        }
125
126
        if ($value === 0) {
127
            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...
128
        } elseif ($value === 1) {
129
            return substr($adress->getStreetLine2(), 0, $limitSend);
130
        } elseif ($value === 2) {
131
            return substr($adress->getStreetLine3(), 0, $limitSend);
132
        } elseif ($value === 3) {
133
            /** contigência para sempre haver o bairro */
134
            if ($adress->getStreetLine4()) {
135
                return substr($adress->getStreetLine4(), 0, $limitSend);
136
            }
137
            if ($adress->getStreetLine3()) {
138
                return substr($adress->getStreetLine3(), 0, $limitSend);
139
            }
140
            if ($adress->getStreetLine1()) {
141
                return substr($adress->getStreetLine1(), 0, $limitSend);
142
            }
143
        }
144
145
        return substr($adress->getStreetLine1(), 0, $limitSend);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function build(array $buildSubject)
152
    {
153
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
154
        $payment = $paymentDO->getPayment();
155
156
        $result = [];
157
158
        $orderAdapter = $this->orderAdapterFactory->create(
159
            ['order' => $payment->getOrder()]
160
        );
161
162
        $billingAddress = $orderAdapter->getBillingAddress();
163
        if ($billingAddress) {
164
            if ($payment->getMethod() === 'moip_magento2_cc') {
165
                // phpcs:ignore Generic.Files.LineLength
166
                $result[PaymentDataRequest::PAYMENT_INSTRUMENT][PaymentDataRequest::FUNDING_INSTRUMENT][PaymentDataRequest::TYPE_CREDIT_CARD][PaymentDataRequest::CREDIT_HOLDER][self::BILLING_ADDRESS] = [
167
                    self::POSTAL_CODE       => preg_replace('/[^0-9]/', '', $billingAddress->getPostcode()),
168
                    self::STREET            => $this->getValueForAddress($billingAddress, self::STREET),
169
                    self::STREET_NUMBER     => $this->getValueForAddress($billingAddress, self::STREET_NUMBER),
170
                    self::STREET_DISTRICT   => $this->getValueForAddress($billingAddress, self::STREET_DISTRICT),
171
                    self::STREET_COMPLEMENT => $this->getValueForAddress($billingAddress, self::STREET_COMPLEMENT),
172
                    self::LOCALITY          => $billingAddress->getCity(),
173
                    self::STATE             => $billingAddress->getRegionCode(),
174
                    self::COUNTRY_CODE      => $billingAddress->getCountryId(),
175
                ];
176
            }
177
            if ($payment->getMethod() === 'moip_magento2_boleto') {
178
                // phpcs:ignore Generic.Files.LineLength
179
                $result[PaymentDataRequest::PAYMENT_INSTRUMENT][PaymentDataRequest::FUNDING_INSTRUMENT][PaymentDataRequest::TYPE_BOLETO][self::BILLING_ADDRESS] = [
180
                    self::POSTAL_CODE       => preg_replace('/[^0-9]/', '', $billingAddress->getPostcode()),
181
                    self::STREET            => $this->getValueForAddress($billingAddress, self::STREET),
182
                    self::STREET_NUMBER     => $this->getValueForAddress($billingAddress, self::STREET_NUMBER),
183
                    self::STREET_DISTRICT   => $this->getValueForAddress($billingAddress, self::STREET_DISTRICT),
184
                    self::STREET_COMPLEMENT => $this->getValueForAddress($billingAddress, self::STREET_COMPLEMENT),
185
                    self::LOCALITY          => $billingAddress->getCity(),
186
                    self::STATE             => $billingAddress->getRegionCode(),
187
                    self::COUNTRY_CODE      => $billingAddress->getCountryId(),
188
                ];
189
            }
190
        }
191
192
        return $result;
193
    }
194
}
195