Completed
Push — 2.x ( 48ed54...fe246a )
by Tim
12s queued 10s
created

TierPriceProcessor::loadTierPrices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Services\TierPriceProcessor
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Klaas-Tido Rühl <[email protected]>
15
 * @author    Tim Wagner <[email protected]>
16
 * @copyright 2019 REFUSiON GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/techdivision/import-product-tier-price
19
 * @link      https://www.techdivision.com
20
 * @link      https://www.refusion.com
21
 */
22
23
namespace TechDivision\Import\Product\TierPrice\Services;
24
25
use TechDivision\Import\Actions\ActionInterface;
26
use TechDivision\Import\Utils\PrimaryKeyUtilInterface;
0 ignored issues
show
Bug introduced by
The type TechDivision\Import\Utils\PrimaryKeyUtilInterface 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...
27
use TechDivision\Import\Connection\ConnectionInterface;
28
use TechDivision\Import\Product\TierPrice\Utils\MemberNames;
29
use TechDivision\Import\Product\Repositories\ProductRepositoryInterface;
30
use TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface;
31
32
/**
33
 * Default implementation of tier price processor objects, giving access to CRUD methods.
34
 *
35
 * @author    Klaas-Tido Rühl <[email protected]>
36
 * @author    Tim Wagner <[email protected]>
37
 * @copyright 2019 REFUSiON GmbH <[email protected]>
38
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
39
 * @link      https://github.com/techdivision/import-product-tier-price
40
 * @link      https://www.techdivision.com
41
 * @link      https://www.refusion.com
42
 */
43
class TierPriceProcessor implements TierPriceProcessorInterface
44
{
45
46
    /**
47
     * A \PDO connection used to load data and handle CRUD functionality.
48
     *
49
     * @var \TechDivision\Import\Connection\ConnectionInterface
50
     */
51
    protected $connection;
52
53
    /**
54
     * The primary key util instance.
55
     *
56
     * @var \TechDivision\Import\Utils\PrimaryKeyUtilInterface
57
     */
58
    protected $primaryKeyUtil;
59
60
    /**
61
     * The action for tier price  CRUD methods.
62
     *
63
     * @var \TechDivision\Import\Actions\ActionInterface
64
     */
65
    protected $tierPriceAction;
66
67
    /**
68
     * The repository to load the tier prices with.
69
     *
70
     * @var \TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface
71
     */
72
    protected $tierPriceRepository;
73
74
    /**
75
     * Caches the existing tier price records referenced by their unique hash.
76
     *
77
     * @var array
78
     */
79
    protected $tierPricesByHash = null;
80
81
    /**
82
     * Initialize the processor with the necessary assembler and repository instances.
83
     *
84
     * @param \TechDivision\Import\Connection\ConnectionInterface                              $connection          The \PDO connnection instance
85
     * @param \TechDivision\Import\Utils\PrimaryKeyUtilInterface                               $primaryKeyUtil      The primary key util
86
     * @param \TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface $tierPriceRepository The repository to load the tier prices with
87
     * @param \TechDivision\Import\Product\Repositories\ProductRepositoryInterface             $productRepository   The repository to load the products with
88
     * @param \TechDivision\Import\Actions\ActionInterface                                     $tierPriceAction     The action for tier price  CRUD methods
89
     */
90
    public function __construct(
91
        ConnectionInterface $connection,
92
        PrimaryKeyUtilInterface $primaryKeyUtil,
93
        TierPriceRepositoryInterface $tierPriceRepository,
94
        ProductRepositoryInterface $productRepository,
95
        ActionInterface $tierPriceAction
96
    ) {
97
        $this->setConnection($connection);
98
        $this->setPrimaryKeyUtil($primaryKeyUtil);
99
        $this->setTierPriceRepository($tierPriceRepository);
100
        $this->setProductRepository($productRepository);
101
        $this->setTierPriceAction($tierPriceAction);
102
    }
103
104
    /**
105
     * Set's the passed connection.
106
     *
107
     * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection to set
108
     *
109
     * @return void
110
     */
111
    public function setConnection(ConnectionInterface $connection)
112
    {
113
        $this->connection = $connection;
114
    }
115
116
    /**
117
     * Return's the connection.
118
     *
119
     * @return \TechDivision\Import\Connection\ConnectionInterface The connection instance
120
     */
121
    public function getConnection()
122
    {
123
        return $this->connection;
124
    }
125
126
    /**
127
     * Sets the passed primary key util instance.
128
     *
129
     * @param \TechDivision\Import\Utils\PrimaryKeyUtilInterface $primaryKeyUtil The primary key util instance
130
     *
131
     * @return void
132
     */
133
    public function setPrimaryKeyUtil(PrimaryKeyUtilInterface $primaryKeyUtil)
134
    {
135
        $this->primaryKeyUtil = $primaryKeyUtil;
136
    }
137
138
    /**
139
     * Returns the primary key util instance.
140
     *
141
     * @return \TechDivision\Import\Utils\PrimaryKeyUtilInterface The primary key util instance
142
     */
143
    public function getPrimaryKeyUtil()
144
    {
145
        return $this->primaryKeyUtil;
146
    }
147
148
    /**
149
     * Returns the primary key member name for the actual Magento edition.
150
     *
151
     * @return string The primary key member name
152
     * @see \TechDivision\Import\Utils\PrimaryKeyUtilInterface::getPrimaryKeyMemberName()
153
     */
154
    public function getPrimaryKeyMemberName()
155
    {
156
        return $this->getPrimaryKeyUtil()->getPrimaryKeyMemberName();
157
    }
158
159
    /**
160
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
161
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
162
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
163
     * to autocommit mode.
164
     *
165
     * @return boolean Returns TRUE on success or FALSE on failure
166
     * @link http://php.net/manual/en/pdo.begintransaction.php
167
     */
168
    public function beginTransaction()
169
    {
170
        return $this->connection->beginTransaction();
171
    }
172
173
    /**
174
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
175
     * ProductProcessor::beginTransaction() starts a new transaction.
176
     *
177
     * @return boolean Returns TRUE on success or FALSE on failure
178
     * @link http://php.net/manual/en/pdo.commit.php
179
     */
180
    public function commit()
181
    {
182
        return $this->connection->commit();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->connection->commit() returns the type void which is incompatible with the documented return type boolean.
Loading history...
Bug introduced by
Are you sure the usage of $this->connection->commit() targeting TechDivision\Import\Conn...tionInterface::commit() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
183
    }
184
185
    /**
186
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
187
     *
188
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
189
     * rolled back the transaction.
190
     *
191
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
192
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
193
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
194
     *
195
     * @return boolean Returns TRUE on success or FALSE on failure
196
     * @link http://php.net/manual/en/pdo.rollback.php
197
     */
198
    public function rollBack()
199
    {
200
        return $this->connection->rollBack();
201
    }
202
203
    /**
204
     * Sets the action with the tier price CRUD methods.
205
     *
206
     * @param \TechDivision\Import\Actions\ActionInterface $tierPriceAction The action with the tier price CRUD methods
207
     *
208
     * @return void
209
     */
210
    public function setTierPriceAction(ActionInterface $tierPriceAction)
211
    {
212
        $this->tierPriceAction = $tierPriceAction;
213
    }
214
215
    /**
216
     * Returns the action with the tier price CRUD methods.
217
     *
218
     * @return \TechDivision\Import\Actions\ActionInterface The action instance
219
     */
220
    public function getTierPriceAction()
221
    {
222
        return $this->tierPriceAction;
223
    }
224
225
    /**
226
     * Sets the repository to load the tier prices with.
227
     *
228
     * @param \TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface $tierPriceRepository The repository instance
229
     *
230
     * @return void
231
     */
232
    public function setTierPriceRepository(TierPriceRepositoryInterface $tierPriceRepository)
233
    {
234
        $this->tierPriceRepository = $tierPriceRepository;
235
    }
236
237
    /**
238
     * Returns the repository to load the tier prices with.
239
     *
240
     * @return \TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface The repository instance
241
     */
242
    public function getTierPriceRepository()
243
    {
244
        return $this->tierPriceRepository;
245
    }
246
247
    /**
248
     * Sets the repository to load the products with.
249
     *
250
     * @param \TechDivision\Import\Product\Repositories\ProductRepositoryInterface $productRepository The repository instance
251
     *
252
     * @return void
253
     */
254
    public function setProductRepository(ProductRepositoryInterface $productRepository)
255
    {
256
        $this->productRepository = $productRepository;
0 ignored issues
show
Bug Best Practice introduced by
The property productRepository does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
257
    }
258
259
    /**
260
     * Returns the repository to load the products with.
261
     *
262
     * @return \TechDivision\Import\Product\Repositories\ProductRepositoryInterface The repository instance
263
     */
264
    public function getProductRepository()
265
    {
266
        return $this->productRepository;
267
    }
268
269
    /**
270
     * Load's and return's the product with the passed SKU.
271
     *
272
     * @param string $sku The SKU of the product to load
273
     *
274
     * @return array The product
275
     */
276
    public function loadProduct($sku)
277
    {
278
        return $this->getProductRepository()->findOneBySku($sku);
279
    }
280
281
    /**
282
     * Returns all tier prices.
283
     *
284
     * @return array The array with the tier prices
285
     */
286
    public function loadTierPrices()
287
    {
288
        return $this->getTierPriceRepository()->findAll();
289
    }
290
291
    /**
292
     * Returns the tier price with the given parameters.
293
     *
294
     * @param string  $pk              The PK of the product relation
295
     * @param integer $allGroups       The flag if all groups are affected or not
296
     * @param integer $customerGroupId The customer group ID
297
     * @param integer $qty             The tier price quantity
298
     * @param integer $websiteId       The website ID the tier price is related to
299
     *
300
     * @return array The tier price
301
     */
302
    public function loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId)
303
    {
304
        return $this->getTierPriceRepository()->findOneByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId);
305
    }
306
307
    /**
308
     * Persists the tier price with the passed data.
309
     *
310
     * @param array       $row  The tier price to persist
311
     * @param string|null $name The name of the prepared statement that has to be executed
312
     *
313
     * @return string The ID of the persisted entity
314
     */
315
    public function persistTierPrice($row, $name = null)
316
    {
317
        return $this->getTierPriceAction()->persist($row, $name);
0 ignored issues
show
Unused Code introduced by
The call to TechDivision\Import\Acti...ionInterface::persist() has too many arguments starting with $name. ( Ignorable by Annotation )

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

317
        return $this->getTierPriceAction()->/** @scrutinizer ignore-call */ persist($row, $name);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
Are you sure the usage of $this->getTierPriceAction()->persist($row, $name) targeting TechDivision\Import\Acti...ionInterface::persist() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->getTierPri...)->persist($row, $name) returns the type void which is incompatible with the documented return type string.
Loading history...
318
    }
319
320
    /**
321
     * Deletes the tierprice with the passed attributes.
322
     *
323
     * @param array       $row  The attributes of the entity to delete
324
     * @param string|null $name The name of the prepared statement that has to be executed
325
     *
326
     * @return void
327
     */
328
    public function deleteTierPrice($row, $name = null)
329
    {
330
        $this->getTierPriceAction()->delete($row, $name);
331
    }
332
333
    /**
334
     * Clean-Up the tier prices after an add-update operation.
335
     *
336
     * @param array $processedTierPrices The array with the IDs of the processed tier prices
337
     *
338
     * @return void
339
     */
340
    public function cleanUpTierPrices(array $processedTierPrices)
341
    {
342
343
        // load the available + the processed tier prices
344
        $availableTierPrices = $this->loadTierPrices();
345
346
        // iterate over the tier prices and delete the obsolete ones
347
        foreach ($availableTierPrices as $tierPrice) {
348
            // if imported, we continue
349
            if (isset($processedTierPrices[$tierPrice[MemberNames::VALUE_ID]])) {
350
                // if the product has been touched by the import, delete the tier price
351
                if (in_array($tierPrice[$this->getPrimaryKeyMemberName()], $processedTierPrices[$tierPrice[MemberNames::VALUE_ID]])) {
352
                    continue;
353
                }
354
355
                // delete the tier price, because the entity is part of the import but the tier price is NOT available any more
356
                $this->deleteTierPrice([MemberNames::VALUE_ID => $tierPrice[MemberNames::VALUE_ID]]);
357
            }
358
        }
359
    }
360
}
361