Passed
Push — PAC-294-integration-strict-mod... ( 92929f )
by
unknown
08:34
created

MsiBunchProcessor::loadProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
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 \TechDivision\Import\Product\Repositories\ProductRepositoryInterface
38
     */
39
    protected $productRepository;
40
41
    /**
42
     * A PDO connection initialized with the values from the Doctrine EntityManager.
43
     *
44
     * @var \TechDivision\Import\Dbal\Connection\ConnectionInterface
45
     */
46
    protected $connection;
47
48
    /**
49
     * The repository to access inventory sources.
50
     *
51
     * @var \TechDivision\Import\Product\Msi\Repositories\InventorySourceRepositoryInterface
52
     */
53
    protected $inventorySourceRepository;
54
55
    /**
56
     * The repository to access inventory source items.
57
     *
58
     * @var \TechDivision\Import\Product\Msi\Repositories\InventorySourceItemRepositoryInterface
59
     */
60
    protected $inventorySourceItemRepository;
61
62
    /**
63
     * The action for product CRUD methods.
64
     *
65
     * @var \TechDivision\Import\Dbal\Actions\ActionInterface
66
     */
67
    protected $inventorySourceItemAction;
68
69
    /**
70
     * Initialize the processor with the necessary assembler and repository instances.
71
     *
72
     * @param \TechDivision\Import\Dbal\Connection\ConnectionInterface                             $connection                    The connection to use
73
     * @param \TechDivision\Import\Product\Msi\Repositories\InventorySourceRepositoryInterface     $inventorySourceRepository     The inventory source repository instance
74
     * @param \TechDivision\Import\Product\Msi\Repositories\InventorySourceItemRepositoryInterface $inventorySourceItemRepository The inventory source item repository instance
75
     * @param \TechDivision\Import\Dbal\Actions\ActionInterface                                    $inventorySourceItemAction     The inventory source item action instance
76
     */
77
    public function __construct(
78
        ConnectionInterface $connection,
79
        InventorySourceRepositoryInterface $inventorySourceRepository,
80
        InventorySourceItemRepositoryInterface $inventorySourceItemRepository,
81
        ActionInterface $inventorySourceItemAction,
82
        ProductRepositoryInterface $productRepository
83
    ) {
84
        $this->setConnection($connection);
85
        $this->setInventorySourceRepository($inventorySourceRepository);
86
        $this->setInventorySourceItemRepository($inventorySourceItemRepository);
87
        $this->setInventorySourceItemAction($inventorySourceItemAction);
88
        $this->setProductRepository($productRepository);
89
    }
90
91
    /**
92
     * Set's the passed connection.
93
     *
94
     * @param \TechDivision\Import\Dbal\Connection\ConnectionInterface $connection The connection to set
95
     *
96
     * @return void
97
     */
98
    public function setConnection(ConnectionInterface $connection)
99
    {
100
        $this->connection = $connection;
101
    }
102
103
    /**
104
     * Return's the connection.
105
     *
106
     * @return \TechDivision\Import\Dbal\Connection\ConnectionInterface The connection instance
107
     */
108
    public function getConnection()
109
    {
110
        return $this->connection;
111
    }
112
113
    /**
114
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
115
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
116
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
117
     * to autocommit mode.
118
     *
119
     * @return boolean Returns TRUE on success or FALSE on failure
120
     * @link http://php.net/manual/en/pdo.begintransaction.php
121
     */
122
    public function beginTransaction()
123
    {
124
        return $this->connection->beginTransaction();
125
    }
126
127
    /**
128
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
129
     * ProductProcessor::beginTransaction() starts a new transaction.
130
     *
131
     * @return boolean Returns TRUE on success or FALSE on failure
132
     * @link http://php.net/manual/en/pdo.commit.php
133
     */
134
    public function commit()
135
    {
136
        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...
137
    }
138
139
    /**
140
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
141
     *
142
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
143
     * rolled back the transaction.
144
     *
145
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
146
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
147
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
148
     *
149
     * @return boolean Returns TRUE on success or FALSE on failure
150
     * @link http://php.net/manual/en/pdo.rollback.php
151
     */
152
    public function rollBack()
153
    {
154
        return $this->connection->rollBack();
155
    }
156
157
    /**
158
     * Set's the repository to load the inventory sources with.
159
     *
160
     * @param \TechDivision\Import\Product\Msi\Repositories\InventorySourceRepositoryInterface $inventorySourceRepository The repository instance
161
     *
162
     * @return void
163
     */
164
    public function setInventorySourceRepository(InventorySourceRepositoryInterface $inventorySourceRepository)
165
    {
166
        $this->inventorySourceRepository = $inventorySourceRepository;
167
    }
168
169
    /**
170
     * Return's the repository to load the inventory sources with.
171
     *
172
     * @return \TechDivision\Import\Product\Msi\Repositories\InventorySourceItemRepositoryInterface The repository instance
173
     */
174
    public function getInventorySourceRepository()
175
    {
176
        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...
177
    }
178
179
    /**
180
     * Set's the repository to load the inventory source items with.
181
     *
182
     * @param \TechDivision\Import\Product\Msi\Repositories\InventorySourceItemRepositoryInterface $inventorySourceItemRepository The repository instance
183
     *
184
     * @return void
185
     */
186
    public function setInventorySourceItemRepository(InventorySourceItemRepositoryInterface $inventorySourceItemRepository)
187
    {
188
        $this->inventorySourceItemRepository = $inventorySourceItemRepository;
189
    }
190
191
    /**
192
     * Return's the repository to load the inventory source items with.
193
     *
194
     * @return \TechDivision\Import\Product\Msi\Repositories\InventorySourceItemRepositoryInterface The repository instance
195
     */
196
    public function getInventorySourceItemRepository()
197
    {
198
        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...
199
    }
200
201
    /**
202
     * Set's the action with the inventory source item CRUD methods.
203
     *
204
     * @param \TechDivision\Import\Dbal\Actions\ActionInterface $inventorySourceItemAction The action instance
205
     *
206
     * @return void
207
     */
208
    public function setInventorySourceItemAction(ActionInterface $inventorySourceItemAction)
209
    {
210
        $this->inventorySourceItemAction = $inventorySourceItemAction;
211
    }
212
213
    /**
214
     * Return's the action with the inventory source item CRUD methods.
215
     *
216
     * @return \TechDivision\Import\Dbal\Actions\ActionInterface The action instance
217
     */
218
    public function getInventorySourceItemAction()
219
    {
220
        return $this->inventorySourceItemAction;
221
    }
222
223
    /**
224
     * Load's the inventory source item with the passed SKU and source code.
225
     *
226
     * @param string $sku        The SKU of the inventory source item to return
227
     * @param string $sourceCode The source code of the inventory source item to return
228
     *
229
     * @return array The inventory source item
230
     */
231
    public function loadInventorySourceItemBySkuAndSourceCode($sku, $sourceCode)
232
    {
233
        return $this->getInventorySourceItemRepository()->findOneBySkuAndSourceCode($sku, $sourceCode);
234
    }
235
236
    /**
237
     * Load's the available inventory sources.
238
     *
239
     * @return array The available inventory sources
240
     */
241
    public function loadInventorySources()
242
    {
243
        return $this->getInventorySourceRepository()->findAll();
244
    }
245
246
    /**
247
     * Persist's the passed inventory source item data.
248
     *
249
     * @param array       $inventorySourceItem The inventory source item data to persist
250
     * @param string|null $name                The name of the prepared statement that has to be executed
251
     *
252
     * @return void
253
     */
254
    public function persistInventorySourceItem($inventorySourceItem, $name = null)
255
    {
256
        $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

256
        $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...
257
    }
258
259
    /**
260
     * Delete's the entity with the passed attributes.
261
     *
262
     * @param array       $row  The attributes of the entity to delete
263
     * @param string|null $name The name of the prepared statement that has to be executed
264
     *
265
     * @return void
266
     */
267
    public function deleteInventorySourceItem($row, $name = null)
268
    {
269
        $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

269
        $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...
270
    }
271
272
    /**
273
     * Clean-Up the repositories to free memory.
274
     *
275
     * @return void
276
     */
277
    public function cleanUp()
278
    {
279
        // not implemented yet
280
    }
281
282
    public function loadProduct($sku)
283
    {
284
        return $this->getProductRepository()->findOneBySku($sku);
285
    }
286
287
    /**
288
     * Set's the repository to load the products with.
289
     *
290
     * @param \TechDivision\Import\Product\Repositories\ProductRepositoryInterface $productRepository The repository instance
291
     *
292
     * @return void
293
     */
294
    public function setProductRepository(ProductRepositoryInterface $productRepository)
295
    {
296
        $this->productRepository = $productRepository;
297
    }
298
299
    /**
300
     * Return's the repository to load the products with.
301
     *
302
     * @return \TechDivision\Import\Product\Repositories\ProductRepositoryInterface The repository instance
303
     */
304
    public function getProductRepository()
305
    {
306
        return $this->productRepository;
307
    }
308
}
309