Issues (104)

Model/Seller/Converter.php (8 issues)

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\SubSellerMagento\Model\Seller;
10
11
use Getnet\SubSellerMagento\Model\Config\Source;
12
use Magento\Framework\App\ObjectManager;
13
use Magento\Framework\Locale\FormatInterface;
14
use Magento\Framework\Serialize\Serializer\Serialize;
15
use Magento\Framework\Stdlib\DateTime\DateTime;
16
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
17
18
/**
19
 * Sub Seller Model converter.
20
 *
21
 * Converts a Sub Seller Model to a Data Object or vice versa.
22
 *
23
 * @SuppressWarnings(PHPMD)
24
 */
25
class Converter
26
{
27
    /**
28
     * @var Serialize
29
     */
30
    protected $serialize;
31
32
    /**
33
     * @var \Getnet\SubSellerMagento\Api\Data\SubSellerInterfaceFactory
0 ignored issues
show
The type Getnet\SubSellerMagento\...bSellerInterfaceFactory 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...
34
     */
35
    protected $subSellerDataObjectFactory;
36
37
    /**
38
     * @var Source\Status
39
     */
40
    protected $status;
41
42
    /**
43
     * @var FormatInterface|null
44
     */
45
    private $format;
46
47
    /**
48
     * @var DateTime
49
     */
50
    protected $date;
51
52
    /**
53
     * @var TimezoneInterface
54
     */
55
    protected $timezone;
56
57
    /**
58
     * @param Serialize                                                   $serialize
59
     * @param \Getnet\SubSellerMagento\Api\Data\SubSellerInterfaceFactory $subSellerDataObjectFactory
60
     * @param Source\Status                                               $status
61
     * @param DateTime                                                    $date
62
     * @param TimezoneInterface                                           $timezone
63
     * @param FormatInterface|null                                        $format
64
     */
65
    public function __construct(
66
        Serialize $serialize,
67
        \Getnet\SubSellerMagento\Api\Data\SubSellerInterfaceFactory $subSellerDataObjectFactory,
68
        Source\Status $status,
69
        DateTime $date,
70
        TimezoneInterface $timezone,
71
        FormatInterface $format = null
72
    ) {
73
        $this->serialize = $serialize;
74
        $this->subSellerDataObjectFactory = $subSellerDataObjectFactory;
75
        $this->status = $status;
76
        $this->date = $date;
77
        $this->timezone = $timezone;
78
        $this->format = $format ?: ObjectManager::getInstance()->get(FormatInterface::class);
79
    }
80
    // phpcs:disable Generic.Files.LineLength
81
82
    /**
83
     * Extract sub seller data in a format which is.
84
     *
85
     * @param \Getnet\SubSellerMagento\Api\Data\SubSellerInterface $subSeller
86
     * @param bool                                                 $returnNumericLogic
87
     *
88
     * @return array
89
     * @SuppressWarnings(PHPMD.NPathComplexity)
90
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
91
     * @SuppressWarnings(PHPCS.Generic.Files.LineLength)
92
     */
93
    public function createArrayFromServiceObject(
94
        \Getnet\SubSellerMagento\Api\Data\SubSellerInterface $subSeller,
95
        $returnNumericLogic = false
96
    ) {
97
        $businessAddress = [];
98
        $bankAccountsUniqueAccount = [];
99
        $identificationDocument = [];
100
        $status = null;
101
        if ($subSeller->getStatus()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $subSeller->getStatus() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
102
            $statusOptions = $this->status->toOptionArray();
103
            $status = 1;
104
            if ($subSeller->getStatus() <= 3) {
105
                $status = $statusOptions[$subSeller->getStatus()];
106
                $status = $status->getText();
107
            }
108
        }
109
110
        if ($subSeller->getAddresses()) {
111
            unset($businessAddress);
112
            $addresses = $subSeller->getAddresses();
113
            $businessAddress = $addresses['addresses']['business_address'];
114
        }
115
116
        if ($subSeller->getBankAccounts()) {
117
            unset($bankAccountsUniqueAccount);
118
            $bankAccounts = $subSeller->getBankAccounts();
119
            $bankAccountsUniqueAccount = $bankAccounts['bank_accounts']['unique_account'];
120
        }
121
122
        if ($subSeller->getIdentificationDocument()) {
0 ignored issues
show
The method getIdentificationDocument() does not exist on Getnet\SubSellerMagento\...Data\SubSellerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Getnet\SubSellerMagento\...Data\SubSellerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
        if ($subSeller->/** @scrutinizer ignore-call */ getIdentificationDocument()) {
Loading history...
123
            unset($identificationDocument);
124
            $identificationDocument = $subSeller->getIdentificationDocument();
125
            $identificationDocument = $identificationDocument['identification_document'];
0 ignored issues
show
The assignment to $identificationDocument is dead and can be removed.
Loading history...
126
        }
127
128
        $subSellerFormData = [
129
            'id'                                 => $subSeller->getId(),
130
            'merchant_id'                        => $subSeller->getMerchantId(),
131
            'code'                               => $subSeller->getCode(),
132
            'id_ext'                             => $subSeller->getIdExt(),
133
            'email'                              => $subSeller->getEmail(),
134
            'legal_document_number'              => $subSeller->getLegalDocumentNumber(),
135
            'type'                               => $subSeller->getType(),
136
            'legal_name'                         => $subSeller->getLegalName(),
137
            'birth_date'                         => $subSeller->getBirthDate(),
138
            'address_street'                     => isset($businessAddress['address_street']) ? $businessAddress['address_street'] : null,
139
            'address_street_number'              => isset($businessAddress['address_street_number']) ? $businessAddress['address_street_number'] : null,
140
            'address_street_district'            => isset($businessAddress['address_street_district']) ? $businessAddress['address_street_district'] : null,
141
            'address_street_complement'          => isset($businessAddress['address_street_complement']) ? $businessAddress['address_street_complement'] : null,
142
            'address_city'                       => isset($businessAddress['address_city']) ? $businessAddress['address_city'] : null,
143
            'address_region'                     => isset($businessAddress['address_region']) ? $businessAddress['address_region'] : null,
144
            'address_region_id'                  => isset($businessAddress['address_region_id']) ? $businessAddress['address_region_id'] : null,
145
            'address_country_id'                 => isset($businessAddress['address_country_id']) ? $businessAddress['address_country_id'] : null,
146
            'address_postcode'                   => isset($businessAddress['address_postcode']) ? $businessAddress['address_postcode'] : null,
147
            'telephone'                          => $subSeller->getTelephone(),
148
            'account_type'                       => isset($bankAccountsUniqueAccount['account_type']) ? $bankAccountsUniqueAccount['account_type'] : null,
149
            'bank'                               => isset($bankAccountsUniqueAccount['bank']) ? $bankAccountsUniqueAccount['bank'] : null,
150
            'agency'                             => isset($bankAccountsUniqueAccount['agency']) ? $bankAccountsUniqueAccount['agency'] : null,
151
            'agency_digit'                       => isset($bankAccountsUniqueAccount['agency_digit']) ? $bankAccountsUniqueAccount['agency_digit'] : null,
152
            'account'                            => isset($bankAccountsUniqueAccount['account']) ? $bankAccountsUniqueAccount['account'] : null,
153
            'account_digit'                      => isset($bankAccountsUniqueAccount['account_digit']) ? $bankAccountsUniqueAccount['account_digit'] : null,
154
            'payment_plan'                       => $subSeller->getPaymentPlan(),
155
            'accepted_contract'                  => $subSeller->getAcceptedContract(),
156
            'marketplace_store'                  => $subSeller->getMarketplaceStore(),
157
            'monthly_gross_income'               => $subSeller->getMonthlyGrossIncome(),
158
            'gross_equity'                       => $subSeller->getGrossEquity(),
159
            'status'                             => $status,
160
            'status_comment'                     => $subSeller->getStatusComment(),
161
            'created_at'                         => $subSeller->getCreatedAt(),
0 ignored issues
show
The method getCreatedAt() does not exist on Getnet\SubSellerMagento\...Data\SubSellerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Getnet\SubSellerMagento\...Data\SubSellerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
            'created_at'                         => $subSeller->/** @scrutinizer ignore-call */ getCreatedAt(),
Loading history...
162
            'updated_at'                         => $subSeller->getUpdatedAt(),
0 ignored issues
show
The method getUpdatedAt() does not exist on Getnet\SubSellerMagento\...Data\SubSellerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Getnet\SubSellerMagento\...Data\SubSellerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
            'updated_at'                         => $subSeller->/** @scrutinizer ignore-call */ getUpdatedAt(),
Loading history...
163
        ];
164
165
        if ($subSellerFormData['address_region_id'] === '0') {
166
            $subSellerFormData['address_region_id'] = '';
167
        }
168
169
        if ($subSeller->getType()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $subSeller->getType() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
170
            $subSellerFormData['type'] = $returnNumericLogic ? 1 : true;
171
        }
172
173
        return $subSellerFormData;
174
    }
175
176
    /**
177
     * Convert an array to a sub seller data object.
178
     *
179
     * @param array $formData
180
     *
181
     * @return \Getnet\SubSellerMagento\Api\Data\SubSellerInterface
182
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
183
     * @SuppressWarnings(PHPMD.NPathComplexity)
184
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
185
     */
186
    public function populateSubSellerData($formData)
187
    {
188
        $birthDate = $this->extractFormData($formData, 'birth_date');
189
        if (!$birthDate) {
190
            $birthDate = '01/01/1970';
191
        }
192
193
        $birthDate = $this->convertDate($birthDate);
194
195
        $status = $this->extractFormData($formData, 'status');
196
        $statusComment = $this->extractFormData($formData, 'status_comment');
197
        if ($status === 1) {
0 ignored issues
show
The condition $status === 1 is always false.
Loading history...
198
            $statusComment = null;
199
        }
200
201
        $subSeller = $this->subSellerDataObjectFactory->create();
202
        $subSeller->setId($this->extractFormData($formData, 'id'))
203
            ->setMerchantId($this->extractFormData($formData, 'merchant_id'))
204
            ->setCode($this->extractFormData($formData, 'code'))
205
            ->setEmail($this->extractFormData($formData, 'email'))
206
            ->setLegalDocumentNumber($this->extractFormData($formData, 'legal_document_number'))
207
            ->setType($this->extractFormData($formData, 'type'))
208
            ->setLegalName($this->extractFormData($formData, 'legal_name'))
209
            ->setBirthDate($birthDate)
210
            ->setAddresses($this->extractFormData($formData, 'addresses'))
211
            ->setTelephone($this->extractFormData($formData, 'telephone'))
212
            ->setFax($this->extractFormData($formData, 'fax'))
213
            ->setBankAccounts($this->extractFormData($formData, 'bank_accounts'))
214
            ->setPaymentPlan($this->extractFormData($formData, 'payment_plan'))
215
            ->setAcceptedContract($this->extractFormData($formData, 'accepted_contract'))
216
            ->setMarketplaceStore($this->extractFormData($formData, 'marketplace_store'))
217
            ->setOccupation($this->extractFormData($formData, 'occupation'))
218
            ->setMonthlyGrossIncome($this->extractFormData($formData, 'monthly_gross_income'))
219
            ->setGrossEquity($this->extractFormData($formData, 'gross_equity'))
220
            ->setStatus($status)
221
            ->setStatusComment($statusComment);
222
223
        return $subSeller;
224
    }
225
226
    /**
227
     * Determines if an array value is set in the form data array and returns it.
228
     *
229
     * @param array  $formData  the form to get data from
230
     * @param string $fieldName the key
231
     *
232
     * @return null|string
233
     */
234
    protected function extractFormData($formData, $fieldName)
235
    {
236
        if (isset($formData[$fieldName])) {
237
            return $formData[$fieldName];
238
        }
239
240
        return null;
241
    }
242
243
    /**
244
     * Convert Date.
245
     *
246
     * @param string $date
247
     *
248
     * @return string
249
     */
250
    public function convertDate(string $date): string
251
    {
252
        $date = str_replace('/', '-', $date);
253
        $defaultTimezone = $this->timezone->getDefaultTimezone();
254
        $configTimezone = $this->timezone->getConfigTimezone();
255
        $date = new \DateTime($date, new \DateTimeZone($configTimezone));
256
        $date->setTimezone(new \DateTimeZone($defaultTimezone));
257
258
        return $date->format('Y-m-d');
259
    }
260
}
261