Issues (104)

Model/Seller/SubSellerRepository.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\Api\Data;
12
use Getnet\SubSellerMagento\Api\Data\SubSellerInterface;
13
use Getnet\SubSellerMagento\Api\SubSellerRepositoryInterface;
14
use Getnet\SubSellerMagento\Model\ResourceModel\Seller\SubSeller\Collection;
15
use Magento\Directory\Model\CountryFactory;
0 ignored issues
show
The type Magento\Directory\Model\CountryFactory 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...
16
use Magento\Directory\Model\RegionFactory;
17
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
18
use Magento\Framework\Api\Search\FilterGroup;
19
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
20
use Magento\Framework\Api\SearchCriteriaInterface;
21
use Magento\Framework\App\ObjectManager;
22
use Magento\Framework\Exception\InputException;
23
use Magento\Framework\Exception\LocalizedException;
24
25
/**
26
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
 */
28
class SubSellerRepository implements SubSellerRepositoryInterface
29
{
30
    public const MESSAGE_SUB_SELLER_ID_IS_NOT_ALLOWED = 'id is not expected for this request.';
31
32
    /**
33
     * @var SubSellerRegistry
34
     */
35
    protected $subSellerRegistry;
36
37
    /**
38
     * @var SubSellerFactory
39
     */
40
    protected $subSellerFactory;
41
42
    /**
43
     * @var Data\SubSellerSearchResultsInterfaceFactory
0 ignored issues
show
The type Getnet\SubSellerMagento\...ResultsInterfaceFactory 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...
44
     */
45
    private $subSellerSearchResultsFactory;
46
47
    /**
48
     * @var \Magento\Directory\Model\CountryFactory
49
     */
50
    protected $countryFactory;
51
52
    /**
53
     * @var \Magento\Directory\Model\RegionFactory
54
     */
55
    protected $regionFactory;
56
57
    /**
58
     * @var \Getnet\SubSellerMagento\Model\ResourceModel\Seller\SubSeller
59
     */
60
    protected $resourceModel;
61
62
    /**
63
     * @var JoinProcessorInterface
64
     */
65
    protected $joinProcessor;
66
67
    /**
68
     * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
69
     */
70
    private $collectionProcessor;
71
72
    /**
73
     * @param SubSellerRegistry                                             $subSellerRegistry
74
     * @param Data\SubSellerSearchResultsInterfaceFactory                   $subSellerSearchResultsFactory
75
     * @param SubSellerFactory                                              $subSellerFactory
76
     * @param CountryFactory                                                $countryFactory
77
     * @param RegionFactory                                                 $regionFactory
78
     * @param \Getnet\SubSellerMagento\Model\ResourceModel\Seller\SubSeller $subSellerResource
79
     * @param JoinProcessorInterface                                        $joinProcessor
80
     * @param CollectionProcessorInterface|null                             $collectionProcessor
81
     */
82
    public function __construct(
83
        SubSellerRegistry $subSellerRegistry,
84
        Data\SubSellerSearchResultsInterfaceFactory $subSellerSearchResultsFactory,
85
        SubSellerFactory $subSellerFactory,
86
        CountryFactory $countryFactory,
87
        RegionFactory $regionFactory,
88
        \Getnet\SubSellerMagento\Model\ResourceModel\Seller\SubSeller $subSellerResource,
89
        JoinProcessorInterface $joinProcessor,
90
        CollectionProcessorInterface $collectionProcessor = null
91
    ) {
92
        $this->subSellerRegistry = $subSellerRegistry;
93
        $this->subSellerSearchResultsFactory = $subSellerSearchResultsFactory;
94
        $this->subSellerFactory = $subSellerFactory;
95
        $this->countryFactory = $countryFactory;
96
        $this->regionFactory = $regionFactory;
97
        $this->resourceModel = $subSellerResource;
98
        $this->joinProcessor = $joinProcessor;
99
        $this->collectionProcessor = $collectionProcessor
100
            ?? ObjectManager::getInstance()->get(
101
                // phpcs:ignore Magento2.PHP.LiteralNamespaces
102
                'Getnet\SubSellerMagento\Model\Api\SearchCriteria\SubSellerCollectionProcessor'
103
            );
104
    }
105
106
    /**
107
     * Save.
108
     *
109
     * @param \Getnet\SubSellerMagento\Api\Data\SubSellerInterface $subSeller
110
     *
111
     * @return Data\SubSellerInterface
112
     */
113
    public function save(\Getnet\SubSellerMagento\Api\Data\SubSellerInterface $subSeller)
114
    {
115
        if ($subSeller->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $subSeller->getId() 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...
116
            $this->subSellerRegistry->retrieveSubSeller($subSeller->getId());
117
        }
118
        $this->validate($subSeller);
119
120
        try {
121
            $this->resourceModel->save($subSeller);
122
        } catch (LocalizedException $e) {
123
            throw $e;
124
        }
125
        $this->subSellerRegistry->registerSubSeller($subSeller);
126
127
        return $subSeller;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $subSeller returns the type Magento\Framework\Model\AbstractModel which is incompatible with the documented return type Getnet\SubSellerMagento\...Data\SubSellerInterface.
Loading history...
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function get($subSellerId)
134
    {
135
        return $this->subSellerRegistry->retrieveSubSeller($subSellerId);
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public function delete(\Getnet\SubSellerMagento\Api\Data\SubSellerInterface $subSeller)
142
    {
143
        return $this->resourceModel->delete($subSeller);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->resourceModel->delete($subSeller) returns the type Getnet\SubSellerMagento\...eModel\Seller\SubSeller which is incompatible with the return type mandated by Getnet\SubSellerMagento\...toryInterface::delete() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149
    public function deleteById($subSellerId)
150
    {
151
        $subSellerModel = $this->subSellerRegistry->retrieveSubSeller($subSellerId);
152
        $this->delete($subSellerModel);
153
        $this->subSellerRegistry->removeSubSeller($subSellerId);
154
155
        return true;
156
    }
157
158
    /**
159
     * @inheritdoc
160
     */
161
    public function getList(SearchCriteriaInterface $searchCriteria)
162
    {
163
        /** @var \Getnet\SubSellerMagento\Model\ResourceModel\Seller\SubSeller\Collection $collection */
164
        $collection = $this->subSellerFactory->create()->getCollection();
0 ignored issues
show
Deprecated Code introduced by
The function Magento\Framework\Model\...tModel::getCollection() has been deprecated: 101.0.0 because collections should be used directly via factory ( Ignorable by Annotation )

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

164
        $collection = /** @scrutinizer ignore-deprecated */ $this->subSellerFactory->create()->getCollection();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
165
        // $this->joinProcessor->process($collection);
166
        // $collection->joinRegionTable();
167
168
        $this->collectionProcessor->process($searchCriteria, $collection);
169
        $subSeller = [];
170
171
        /** @var \Getnet\SubSellerMagento\Model\Seller\SubSeller $subSellerModel */
172
        foreach ($collection as $subSellerModel) {
173
            $subSeller[] = $subSellerModel;
174
        }
175
176
        return $this->subSellerSearchResultsFactory->create()
177
            ->setItems($subSeller)
178
            ->setTotalCount($collection->getSize())
179
            ->setSearchCriteria($searchCriteria);
180
    }
181
182
    /**
183
     * Helper function that adds a FilterGroup to the collection.
184
     *
185
     * @param FilterGroup $filterGroup
186
     * @param Collection  $collection
187
     *
188
     * @return void
189
     *
190
     * @deprecated 100.2.0
191
     */
192
    protected function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
193
    {
194
        $fields = [];
195
        $conditions = [];
196
        foreach ($filterGroup->getFilters() as $filter) {
197
            $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
198
            $fields[] = $this->translateField($filter->getField());
0 ignored issues
show
Deprecated Code introduced by
The function Getnet\SubSellerMagento\...itory::translateField() has been deprecated: 100.2.0 ( Ignorable by Annotation )

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

198
            $fields[] = /** @scrutinizer ignore-deprecated */ $this->translateField($filter->getField());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
199
            $conditions[] = [$condition => $filter->getValue()];
200
        }
201
        if ($fields) {
202
            $collection->addFieldToFilter($fields, $conditions);
203
        }
204
    }
205
206
    /**
207
     * Translates a field name to a DB column name for use in collection queries.
208
     *
209
     * @deprecated 100.2.0
210
     *
211
     * @param string $field a field name that should be translated to a DB column name.
212
     *
213
     * @return string
214
     */
215
    protected function translateField($field)
216
    {
217
        switch ($field) {
218
            case SubSeller::KEY_REGION_NAME:
0 ignored issues
show
The constant Getnet\SubSellerMagento\...Seller::KEY_REGION_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
219
                return 'region_table.code';
220
            default:
221
                return 'main_table.'.$field;
222
        }
223
    }
224
225
    /**
226
     * Validate sub seller.
227
     *
228
     * @param SubSellerInterface $subSeller
229
     *
230
     * @throws InputException
231
     *
232
     * @return void
233
     *
234
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
235
     * @SuppressWarnings(PHPMD.NPathComplexity)
236
     */
237
    private function validate($subSeller)
238
    {
239
        $exception = new InputException();
240
241
        if ($subSeller->getId() === 0) {
242
            $subSeller->setId(null);
243
        }
244
245
        if (!\Zend_Validate::is($subSeller->getLegalDocumentNumber(), 'NotEmpty')) {
246
            $exception->addError(
247
                __(
248
                    '"%fieldName" is required. Enter and try again.',
249
                    [
250
                        'fieldName' => 'legal_documment_number',
251
                    ]
252
                )
253
            );
254
        }
255
        if ($subSeller->getLegalDocumentNumber()) {
256
            $legalDocumentNumber = preg_replace('/[^0-9]/', '', $subSeller->getLegalDocumentNumber());
257
258
            if (strlen($legalDocumentNumber) === 11) {
259
                $subSeller->setType(0);
260
            }
261
262
            if (strlen($legalDocumentNumber) === 14) {
263
                $subSeller->setType(1);
264
            }
265
        }
266
267
        if ($exception->wasErrorAdded()) {
268
            throw $exception;
269
        }
270
    }
271
}
272