Passed
Push — master ( b8b3d1...a7897a )
by
unknown
21:24 queued 18:55
created

MsiBunchProcessor::setProductRepository()   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 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 1
f 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Msi\Services\MsiBunchProcessor
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2018 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-product-msi
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Product\Msi\Services;
16
17
use TechDivision\Import\Dbal\Actions\ActionInterface;
18
use TechDivision\Import\Dbal\Connection\ConnectionInterface;
19
use TechDivision\Import\Product\Msi\Repositories\InventorySourceRepositoryInterface;
20
use TechDivision\Import\Product\Msi\Repositories\InventorySourceItemRepositoryInterface;
21
use TechDivision\Import\Product\Repositories\ProductRepositoryInterface;
22
23
/**
24
 * The inventory source item bunch processor implementation.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2018 TechDivision GmbH <[email protected]>
28
 * @license   https://opensource.org/licenses/MIT
29
 * @link      https://github.com/techdivision/import-product-msi
30
 * @link      http://www.techdivision.com
31
 */
32
class MsiBunchProcessor implements MsiBunchProcessorInterface
33
{
34
    /**
35
     * The repository to load the products with.
36
     *
37
     * @var ProductRepositoryInterface
38
     */
39
    protected $productRepository;
40
41
    /**
42
     * A PDO connection initialized with the values from the Doctrine EntityManager.
43
     *
44
     * @var ConnectionInterface
45
     */
46
    protected $connection;
47
48
    /**
49
     * The repository to access inventory sources.
50
     *
51
     * @var InventorySourceRepositoryInterface
52
     */
53
    protected $inventorySourceRepository;
54
55
    /**
56
     * The repository to access inventory source items.
57
     *
58
     * @var InventorySourceItemRepositoryInterface
59
     */
60
    protected $inventorySourceItemRepository;
61
62
    /**
63
     * The action for product CRUD methods.
64
     *
65
     * @var ActionInterface
66
     */
67
    protected $inventorySourceItemAction;
68
69
    /**
70
     * Initialize the processor with the necessary assembler and repository instances.
71
     *
72
     * @param ConnectionInterface                    $connection                    The connection to use
73
     * @param InventorySourceRepositoryInterface     $inventorySourceRepository     The inventory source repository instance
74
     * @param InventorySourceItemRepositoryInterface $inventorySourceItemRepository The inventory source item repository instance
75
     * @param ActionInterface                        $inventorySourceItemAction     The inventory source item action instance
76
     * @param ProductRepositoryInterface             $productRepository             The product source repository instance
77
     */
78
    public function __construct(
79
        ConnectionInterface $connection,
80
        InventorySourceRepositoryInterface $inventorySourceRepository,
81
        InventorySourceItemRepositoryInterface $inventorySourceItemRepository,
82
        ActionInterface $inventorySourceItemAction,
83
        ProductRepositoryInterface $productRepository
84
    ) {
85
        $this->setConnection($connection);
86
        $this->setInventorySourceRepository($inventorySourceRepository);
87
        $this->setInventorySourceItemRepository($inventorySourceItemRepository);
88
        $this->setInventorySourceItemAction($inventorySourceItemAction);
89
        $this->setProductRepository($productRepository);
90
    }
91
92
    /**
93
     * Set's the passed connection.
94
     *
95
     * @param ConnectionInterface $connection The connection to set
96
     *
97
     * @return void
98
     */
99
    public function setConnection(ConnectionInterface $connection)
100
    {
101
        $this->connection = $connection;
102
    }
103
104
    /**
105
     * Return's the connection.
106
     *
107
     * @return ConnectionInterface The connection instance
108
     */
109
    public function getConnection()
110
    {
111
        return $this->connection;
112
    }
113
114
    /**
115
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
116
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
117
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
118
     * to autocommit mode.
119
     *
120
     * @return boolean Returns TRUE on success or FALSE on failure
121
     * @link http://php.net/manual/en/pdo.begintransaction.php
122
     */
123
    public function beginTransaction()
124
    {
125
        return $this->connection->beginTransaction();
126
    }
127
128
    /**
129
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
130
     * ProductProcessor::beginTransaction() starts a new transaction.
131
     *
132
     * @return boolean Returns TRUE on success or FALSE on failure
133
     * @link http://php.net/manual/en/pdo.commit.php
134
     */
135
    public function commit()
136
    {
137
        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\Dbal...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...
138
    }
139
140
    /**
141
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
142
     *
143
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
144
     * rolled back the transaction.
145
     *
146
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
147
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
148
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
149
     *
150
     * @return boolean Returns TRUE on success or FALSE on failure
151
     * @link http://php.net/manual/en/pdo.rollback.php
152
     */
153
    public function rollBack()
154
    {
155
        return $this->connection->rollBack();
156
    }
157
158
    /**
159
     * Set's the repository to load the inventory sources with.
160
     *
161
     * @param InventorySourceRepositoryInterface $inventorySourceRepository The repository instance
162
     *
163
     * @return void
164
     */
165
    public function setInventorySourceRepository(InventorySourceRepositoryInterface $inventorySourceRepository)
166
    {
167
        $this->inventorySourceRepository = $inventorySourceRepository;
168
    }
169
170
    /**
171
     * Return's the repository to load the inventory sources with.
172
     *
173
     * @return InventorySourceItemRepositoryInterface The repository instance
174
     */
175
    public function getInventorySourceRepository()
176
    {
177
        return $this->inventorySourceRepository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->inventorySourceRepository returns the type TechDivision\Import\Prod...urceRepositoryInterface which is incompatible with the documented return type TechDivision\Import\Prod...ItemRepositoryInterface.
Loading history...
178
    }
179
180
    /**
181
     * Set's the repository to load the inventory source items with.
182
     *
183
     * @param InventorySourceItemRepositoryInterface $inventorySourceItemRepository The repository instance
184
     *
185
     * @return void
186
     */
187
    public function setInventorySourceItemRepository(InventorySourceItemRepositoryInterface $inventorySourceItemRepository)
188
    {
189
        $this->inventorySourceItemRepository = $inventorySourceItemRepository;
190
    }
191
192
    /**
193
     * Return's the repository to load the inventory source items with.
194
     *
195
     * @return InventorySourceItemRepositoryInterface The repository instance
196
     */
197
    public function getInventorySourceItemRepository()
198
    {
199
        return $this->inventorySourceItemRepository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->inventorySourceItemRepository returns the type TechDivision\Import\Prod...ItemRepositoryInterface which is incompatible with the return type mandated by TechDivision\Import\Prod...ySourceItemRepository() of TechDivision\Import\Prod...ItemRepositoryInterface.

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...
200
    }
201
202
    /**
203
     * Set's the action with the inventory source item CRUD methods.
204
     *
205
     * @param ActionInterface $inventorySourceItemAction The action instance
206
     *
207
     * @return void
208
     */
209
    public function setInventorySourceItemAction(ActionInterface $inventorySourceItemAction)
210
    {
211
        $this->inventorySourceItemAction = $inventorySourceItemAction;
212
    }
213
214
    /**
215
     * Return's the action with the inventory source item CRUD methods.
216
     *
217
     * @return ActionInterface The action instance
218
     */
219
    public function getInventorySourceItemAction()
220
    {
221
        return $this->inventorySourceItemAction;
222
    }
223
224
    /**
225
     * Load's the inventory source item with the passed SKU and source code.
226
     *
227
     * @param string $sku        The SKU of the inventory source item to return
228
     * @param string $sourceCode The source code of the inventory source item to return
229
     *
230
     * @return array The inventory source item
231
     */
232
    public function loadInventorySourceItemBySkuAndSourceCode($sku, $sourceCode)
233
    {
234
        return $this->getInventorySourceItemRepository()->findOneBySkuAndSourceCode($sku, $sourceCode);
235
    }
236
237
    /**
238
     * Load's the available inventory sources.
239
     *
240
     * @return array The available inventory sources
241
     */
242
    public function loadInventorySources()
243
    {
244
        return $this->getInventorySourceRepository()->findAll();
245
    }
246
247
    /**
248
     * Persist's the passed inventory source item data.
249
     *
250
     * @param array       $inventorySourceItem The inventory source item data to persist
251
     * @param string|null $name                The name of the prepared statement that has to be executed
252
     *
253
     * @return void
254
     */
255
    public function persistInventorySourceItem($inventorySourceItem, $name = null)
256
    {
257
        $this->getInventorySourceItemAction()->persist($inventorySourceItem, $name);
0 ignored issues
show
Unused Code introduced by
The call to TechDivision\Import\Dbal...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

257
        $this->getInventorySourceItemAction()->/** @scrutinizer ignore-call */ persist($inventorySourceItem, $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...
258
    }
259
260
    /**
261
     * Delete's the entity with the passed attributes.
262
     *
263
     * @param array       $row  The attributes of the entity to delete
264
     * @param string|null $name The name of the prepared statement that has to be executed
265
     *
266
     * @return void
267
     */
268
    public function deleteInventorySourceItem($row, $name = null)
269
    {
270
        $this->getInventorySourceItemAction()->delete($row, $name);
0 ignored issues
show
Unused Code introduced by
The call to TechDivision\Import\Dbal...tionInterface::delete() 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

270
        $this->getInventorySourceItemAction()->/** @scrutinizer ignore-call */ delete($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...
271
    }
272
273
    /**
274
     * Clean-Up the repositories to free memory.
275
     *
276
     * @return void
277
     */
278
    public function cleanUp()
279
    {
280
        // not implemented yet
281
    }
282
283
    /**
284
     * Load's and return's the product with the passed SKU.
285
     *
286
     * @param string $sku The SKU of the product to load
287
     *
288
     * @return array The product
289
     */
290
    public function loadProduct($sku)
291
    {
292
        return $this->getProductRepository()->findOneBySku($sku);
293
    }
294
295
    /**
296
     * Set's the repository to load the products with.
297
     *
298
     * @param ProductRepositoryInterface $productRepository The repository instance
299
     *
300
     * @return void
301
     */
302
    public function setProductRepository(ProductRepositoryInterface $productRepository)
303
    {
304
        $this->productRepository = $productRepository;
305
    }
306
307
    /**
308
     * Return's the repository to load the products with.
309
     *
310
     * @return ProductRepositoryInterface The repository instance
311
     */
312
    public function getProductRepository()
313
    {
314
        return $this->productRepository;
315
    }
316
}
317