Completed
Push — 2.x ( 704b62 )
by Tim
04:55
created

TierPriceProcessor   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
eloc 27
dl 0
loc 234
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A deleteTierPrice() 0 3 1
A rollBack() 0 3 1
A cleanUpTierPrices() 0 17 4
A getTierPriceRepository() 0 3 1
A getTierPriceAction() 0 3 1
A loadTierPrices() 0 3 1
A beginTransaction() 0 3 1
A commit() 0 3 1
A setTierPriceRepository() 0 3 1
A setConnection() 0 3 1
A loadTierPriceByEntityIdAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId() 0 3 1
A persistTierPrice() 0 3 1
A setTierPriceAction() 0 3 1
A getConnection() 0 3 1
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\Connection\ConnectionInterface;
27
use TechDivision\Import\Product\TierPrice\Utils\MemberNames;
28
use TechDivision\Import\Product\Repositories\ProductRepositoryInterface;
29
use TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface;
30
31
/**
32
 * Default implementation of tier price processor objects, giving access to CRUD methods.
33
 *
34
 * @author    Klaas-Tido Rühl <[email protected]>
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2019 REFUSiON GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import-product-tier-price
39
 * @link      https://www.techdivision.com
40
 * @link      https://www.refusion.com
41
 */
42
class TierPriceProcessor implements TierPriceProcessorInterface
43
{
44
45
    /**
46
     * A \PDO connection used to load data and handle CRUD functionality.
47
     *
48
     * @var \TechDivision\Import\Connection\ConnectionInterface
49
     */
50
    protected $connection;
51
52
    /**
53
     * The action for tier price  CRUD methods.
54
     *
55
     * @var \TechDivision\Import\Actions\ActionInterface
56
     */
57
    protected $tierPriceAction;
58
59
    /**
60
     * The repository to load the tier prices with.
61
     *
62
     * @var \TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface
63
     */
64
    protected $tierPriceRepository;
65
66
    /**
67
     * Caches the existing tier price records referenced by their unique hash.
68
     *
69
     * @var array
70
     */
71
    protected $tierPricesByHash = null;
72
73
    /**
74
     * Initialize the processor with the necessary assembler and repository instances.
75
     *
76
     * @param \TechDivision\Import\Connection\ConnectionInterface                              $connection          The \PDO connnection instance
77
     * @param \TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface $tierPriceRepository The repository to load the tier prices with
78
     * @param \TechDivision\Import\Actions\ActionInterface                                     $tierPriceAction     The action for tier price  CRUD methods
79
     */
80
    public function __construct(
81
        ConnectionInterface $connection,
82
        TierPriceRepositoryInterface $tierPriceRepository,
83
        ActionInterface $tierPriceAction
84
    ) {
85
        $this->setConnection($connection);
86
        $this->setTierPriceRepository($tierPriceRepository);
87
        $this->setTierPriceAction($tierPriceAction);
88
    }
89
90
    /**
91
     * Set's the passed connection.
92
     *
93
     * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection to set
94
     *
95
     * @return void
96
     */
97
    public function setConnection(ConnectionInterface $connection)
98
    {
99
        $this->connection = $connection;
100
    }
101
102
    /**
103
     * Return's the connection.
104
     *
105
     * @return \TechDivision\Import\Connection\ConnectionInterface The connection instance
106
     */
107
    public function getConnection()
108
    {
109
        return $this->connection;
110
    }
111
112
    /**
113
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
114
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
115
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
116
     * to autocommit mode.
117
     *
118
     * @return boolean Returns TRUE on success or FALSE on failure
119
     * @link http://php.net/manual/en/pdo.begintransaction.php
120
     */
121
    public function beginTransaction()
122
    {
123
        return $this->connection->beginTransaction();
124
    }
125
126
    /**
127
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
128
     * ProductProcessor::beginTransaction() starts a new transaction.
129
     *
130
     * @return boolean Returns TRUE on success or FALSE on failure
131
     * @link http://php.net/manual/en/pdo.commit.php
132
     */
133
    public function commit()
134
    {
135
        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...
136
    }
137
138
    /**
139
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
140
     *
141
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
142
     * rolled back the transaction.
143
     *
144
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
145
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
146
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
147
     *
148
     * @return boolean Returns TRUE on success or FALSE on failure
149
     * @link http://php.net/manual/en/pdo.rollback.php
150
     */
151
    public function rollBack()
152
    {
153
        return $this->connection->rollBack();
154
    }
155
156
    /**
157
     * Sets the action with the tier price CRUD methods.
158
     *
159
     * @param \TechDivision\Import\Actions\ActionInterface $tierPriceAction The action with the tier price CRUD methods
160
     *
161
     * @return void
162
     */
163
    public function setTierPriceAction(ActionInterface $tierPriceAction)
164
    {
165
        $this->tierPriceAction = $tierPriceAction;
166
    }
167
168
    /**
169
     * Returns the action with the tier price CRUD methods.
170
     *
171
     * @return \TechDivision\Import\Actions\ActionInterface The action instance
172
     */
173
    public function getTierPriceAction()
174
    {
175
        return $this->tierPriceAction;
176
    }
177
178
    /**
179
     * Sets the repository to load the tier prices with.
180
     *
181
     * @param \TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface $tierPriceRepository The repository instance
182
     *
183
     * @return void
184
     */
185
    public function setTierPriceRepository(TierPriceRepositoryInterface $tierPriceRepository)
186
    {
187
        $this->tierPriceRepository = $tierPriceRepository;
188
    }
189
190
    /**
191
     * Returns the repository to load the tier prices with.
192
     *
193
     * @return \TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepositoryInterface The repository instance
194
     */
195
    public function getTierPriceRepository()
196
    {
197
        return $this->tierPriceRepository;
198
    }
199
200
    /**
201
     * Returns all tier prices.
202
     *
203
     * @return array The array with the tier prices
204
     */
205
    public function loadTierPrices()
206
    {
207
        return $this->getTierPriceRepository()->findAll();
208
    }
209
210
    /**
211
     * Returns the tier price with the given parameters.
212
     *
213
     * @param string  $entityId        The entity ID of the product relation
214
     * @param integer $allGroups       The flag if all groups are affected or not
215
     * @param integer $customerGroupId The customer group ID
216
     * @param integer $qty             The tier price quantity
217
     * @param integer $websiteId       The website ID the tier price is related to
218
     *
219
     * @return array The tier price
220
     */
221
    public function loadTierPriceByEntityIdAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($entityId, $allGroups, $customerGroupId, $qty, $websiteId)
222
    {
223
        return $this->getTierPriceRepository()->findOneByEntityIdAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($entityId, $allGroups, $customerGroupId, $qty, $websiteId);
224
    }
225
226
    /**
227
     * Persists the tier price with the passed data.
228
     *
229
     * @param array       $row  The tier price to persist
230
     * @param string|null $name The name of the prepared statement that has to be executed
231
     *
232
     * @return string The ID of the persisted entity
233
     */
234
    public function persistTierPrice($row, $name = null)
235
    {
236
        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

236
        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...
237
    }
238
239
    /**
240
     * Deletes the tierprice with the passed attributes.
241
     *
242
     * @param array       $row  The attributes of the entity to delete
243
     * @param string|null $name The name of the prepared statement that has to be executed
244
     *
245
     * @return void
246
     */
247
    public function deleteTierPrice($row, $name = null)
248
    {
249
        $this->getTierPriceAction()->delete($row, $name);
250
    }
251
252
    /**
253
     * Clean-Up the tier prices after an add-update operation.
254
     *
255
     * @param array $processedTierPrices The array with the IDs of the processed tier prices
256
     *
257
     * @return void
258
     */
259
    public function cleanUpTierPrices(array $processedTierPrices)
260
    {
261
262
        // load the available + the processed tier prices
263
        $availableTierPrices = $this->loadTierPrices();
264
265
        // iterate over the tier prices and delete the obsolete ones
266
        foreach ($availableTierPrices as $tierPrice) {
267
            // if imported, we continue
268
            if (isset($processedTierPrices[$tierPrice[MemberNames::VALUE_ID]])) {
269
                // if the product has been touched by the import, delete the tier price
270
                if (in_array($tierPrice[MemberNames::ENTITY_ID], $processedTierPrices[$tierPrice[MemberNames::VALUE_ID]])) {
271
                    continue;
272
                }
273
274
                // delete the tier price, because the entity is part of the import but the tier price is NOT available any more
275
                $this->deleteTierPrice([MemberNames::VALUE_ID => $tierPrice[MemberNames::VALUE_ID]]);
276
            }
277
        }
278
    }
279
}
280