Completed
Push — master ( 5bcafe...c5dea9 )
by Tim
13s
created

getProductVarcharRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessor
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    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product-url-rewrite
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\UrlRewrite\Services;
22
23
use TechDivision\Import\Actions\UrlRewriteAction;
24
use TechDivision\Import\Connection\ConnectionInterface;
25
use TechDivision\Import\Product\Repositories\ProductRepository;
26
use TechDivision\Import\Product\Repositories\ProductVarcharRepository;
27
use TechDivision\Import\Product\UrlRewrite\Actions\UrlRewriteProductCategoryAction;
28
use TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepository;
29
use TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepository;
30
31
/**
32
 * The product URL rewrite processor implementation.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2016 TechDivision GmbH <[email protected]>
36
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link      https://github.com/techdivision/import-product-url-rewrite
38
 * @link      http://www.techdivision.com
39
 */
40
class ProductUrlRewriteProcessor implements ProductUrlRewriteProcessorInterface
41
{
42
43
    /**
44
     * A PDO connection initialized with the values from the Doctrine EntityManager.
45
     *
46
     * @var \TechDivision\Import\Connection\ConnectionInterface
47
     */
48
    protected $connection;
49
50
    /**
51
     * The action for URL rewrite CRUD methods.
52
     *
53
     * @var \TechDivision\Import\Actions\UrlRewriteAction
54
     */
55
    protected $urlRewriteAction;
56
57
    /**
58
     * The action for URL rewrite product category CRUD methods.
59
     *
60
     * @var \TechDivision\Import\Product\Actions\UrlRewriteProductCategoryAction
61
     */
62
    protected $urlRewriteProductCategoryAction;
63
64
    /**
65
     * The repository to load the products with.
66
     *
67
     * @var \TechDivision\Import\Product\Repositories\ProductRepository
68
     */
69
    protected $productRepository;
70
71
    /**
72
     * The repository to load the URL rewrites with.
73
     *
74
     * @var \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepository
75
     */
76
    protected $urlRewriteRepository;
77
78
    /**
79
     * The repository to load the URL rewrite product category relations with.
80
     *
81
     * @var \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepository
82
     */
83
    protected $urlRewriteProductCategoryRepository;
84
85
    /**
86
     * Initialize the processor with the necessary assembler and repository instances.
87
     *
88
     * @param \TechDivision\Import\Connection\ConnectionInterface                                      $connection                          The connection to use
89
     * @param \TechDivision\Import\Product\Repositories\ProductRepository                              $productRepository                   The product repository to use
90
     * @param \TechDivision\Import\Product\Repositories\ProductVarcharRepository                       $productVarcharRepository            The product varchar repository to use
91
     * @param \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepository                $urlRewriteRepository                The URL rewrite repository to use
92
     * @param \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepository $urlRewriteProductCategoryRepository The URL rewrite product category repository to use
93
     * @param \TechDivision\Import\Actions\UrlRewriteAction                                            $urlRewriteAction                    The URL rewrite action to use
94
     * @param \TechDivision\Import\Product\Actions\UrlRewriteProductCategoryAction                     $urlRewriteProductCategoryAction     The URL rewrite product category action to use
95
     */
96
    public function __construct(
97
        ConnectionInterface $connection,
98
        ProductRepository $productRepository,
99
        ProductVarcharRepository $productVarcharRepository,
100
        UrlRewriteRepository $urlRewriteRepository,
101
        UrlRewriteProductCategoryRepository $urlRewriteProductCategoryRepository,
102
        UrlRewriteAction $urlRewriteAction,
103
        UrlRewriteProductCategoryAction $urlRewriteProductCategoryAction
104
    ) {
105
        $this->setConnection($connection);
106
        $this->setProductRepository($productRepository);
107
        $this->setProductVarcharRepository($productVarcharRepository);
108
        $this->setUrlRewriteRepository($urlRewriteRepository);
109
        $this->setUrlRewriteProductCategoryRepository($urlRewriteProductCategoryRepository);
110
        $this->setUrlRewriteAction($urlRewriteAction);
111
        $this->setUrlRewriteProductCategoryAction($urlRewriteProductCategoryAction);
0 ignored issues
show
Documentation introduced by
$urlRewriteProductCategoryAction is of type object<TechDivision\Impo...eProductCategoryAction>, but the function expects a object<TechDivision\Impo...eProductCategoryAction>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
    }
113
114
    /**
115
     * Set's the passed connection.
116
     *
117
     * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection to set
118
     *
119
     * @return void
120
     */
121
    public function setConnection(ConnectionInterface $connection)
122
    {
123
        $this->connection = $connection;
124
    }
125
126
    /**
127
     * Return's the connection.
128
     *
129
     * @return \TechDivision\Import\Connection\ConnectionInterface The connection instance
130
     */
131
    public function getConnection()
132
    {
133
        return $this->connection;
134
    }
135
136
    /**
137
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
138
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
139
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
140
     * to autocommit mode.
141
     *
142
     * @return boolean Returns TRUE on success or FALSE on failure
143
     * @link http://php.net/manual/en/pdo.begintransaction.php
144
     */
145
    public function beginTransaction()
146
    {
147
        return $this->connection->beginTransaction();
148
    }
149
150
    /**
151
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
152
     * ProductProcessor::beginTransaction() starts a new transaction.
153
     *
154
     * @return boolean Returns TRUE on success or FALSE on failure
155
     * @link http://php.net/manual/en/pdo.commit.php
156
     */
157
    public function commit()
158
    {
159
        return $this->connection->commit();
160
    }
161
162
    /**
163
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
164
     *
165
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
166
     * rolled back the transaction.
167
     *
168
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
169
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
170
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
171
     *
172
     * @return boolean Returns TRUE on success or FALSE on failure
173
     * @link http://php.net/manual/en/pdo.rollback.php
174
     */
175
    public function rollBack()
176
    {
177
        return $this->connection->rollBack();
178
    }
179
180
    /**
181
     * Set's the action with the URL rewrite CRUD methods.
182
     *
183
     * @param \TechDivision\Import\Actions\UrlRewriteAction $urlRewriteAction The action with the URL rewrite CRUD methods
184
     *
185
     * @return void
186
     */
187
    public function setUrlRewriteAction($urlRewriteAction)
188
    {
189
        $this->urlRewriteAction = $urlRewriteAction;
190
    }
191
192
    /**
193
     * Return's the action with the URL rewrite CRUD methods.
194
     *
195
     * @return \TechDivision\Import\Actions\UrlRewriteAction The action instance
196
     */
197
    public function getUrlRewriteAction()
198
    {
199
        return $this->urlRewriteAction;
200
    }
201
202
    /**
203
     * Set's the action with the URL rewrite product category CRUD methods.
204
     *
205
     * @param \TechDivision\Import\Product\Actions\UrlRewriteProductCategoryAction $urlRewriteProductCategoryAction The action with the URL rewrite CRUD methods
206
     *
207
     * @return void
208
     */
209
    public function setUrlRewriteProductCategoryAction($urlRewriteProductCategoryAction)
210
    {
211
        $this->urlRewriteProductCategoryAction = $urlRewriteProductCategoryAction;
212
    }
213
214
    /**
215
     * Return's the action with the URL rewrite product category CRUD methods.
216
     *
217
     * @return \TechDivision\Import\Product\Actions\UrlRewriteProductCategoryAction The action instance
218
     */
219
    public function getUrlRewriteProductCategoryAction()
220
    {
221
        return $this->urlRewriteProductCategoryAction;
222
    }
223
224
    /**
225
     * Set's the repository to load the product varchar attribute with.
226
     *
227
     * @param \TechDivision\Import\Product\Repositories\ProductVarcharRepository $productVarcharRepository The repository instance
228
     *
229
     * @return void
230
     */
231
    public function setProductVarcharRepository($productVarcharRepository)
232
    {
233
        $this->productVarcharRepository = $productVarcharRepository;
0 ignored issues
show
Bug introduced by
The property productVarcharRepository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
234
    }
235
236
    /**
237
     * Return's the repository to load the product varchar attribute with.
238
     *
239
     * @return \TechDivision\Import\Product\Repositories\ProductVarcharRepository The repository instance
240
     */
241
    public function getProductVarcharRepository()
242
    {
243
        return $this->productVarcharRepository;
244
    }
245
246
    /**
247
     * Set's the repository to load the URL rewrites with.
248
     *
249
     * @param \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepository $urlRewriteRepository The repository instance
250
     *
251
     * @return void
252
     */
253
    public function setUrlRewriteRepository($urlRewriteRepository)
254
    {
255
        $this->urlRewriteRepository = $urlRewriteRepository;
256
    }
257
258
    /**
259
     * Return's the repository to load the URL rewrites with.
260
     *
261
     * @return \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepository The repository instance
262
     */
263
    public function getUrlRewriteRepository()
264
    {
265
        return $this->urlRewriteRepository;
266
    }
267
268
    /**
269
     * Set's the repository to load the URL rewrite product category relations with.
270
     *
271
     * @param \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepository $urlRewriteProductCategoryRepository The repository instance
272
     *
273
     * @return void
274
     */
275
    public function setUrlRewriteProductCategoryRepository($urlRewriteProductCategoryRepository)
276
    {
277
        $this->urlRewriteProductCategoryRepository = $urlRewriteProductCategoryRepository;
278
    }
279
280
    /**
281
     * Return's the repository to load the URL rewrite product category relations with.
282
     *
283
     * @return \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepository The repository instance
284
     */
285
    public function getUrlRewriteProductCategoryRepository()
286
    {
287
        return $this->urlRewriteProductCategoryRepository;
288
    }
289
290
    /**
291
     * Set's the repository to load the products with.
292
     *
293
     * @param \TechDivision\Import\Product\Repositories\ProductRepository $productRepository The repository instance
294
     *
295
     * @return void
296
     */
297
    public function setProductRepository($productRepository)
298
    {
299
        $this->productRepository = $productRepository;
300
    }
301
302
    /**
303
     * Return's the repository to load the products with.
304
     *
305
     * @return \TechDivision\Import\Product\Repositories\ProductRepository The repository instance
306
     */
307
    public function getProductRepository()
308
    {
309
        return $this->productRepository;
310
    }
311
312
    /**
313
     * Return's the URL rewrites for the passed URL entity type and ID.
314
     *
315
     * @param string  $entityType The entity type to load the URL rewrites for
316
     * @param integer $entityId   The entity ID to laod the rewrites for
317
     *
318
     * @return array The URL rewrites
319
     */
320
    public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId)
321
    {
322
        return $this->getUrlRewriteRepository()->findAllByEntityTypeAndEntityId($entityType, $entityId);
323
    }
324
325
    /**
326
     * Return's the URL rewrites for the passed URL entity type and ID.
327
     *
328
     * @param string  $entityType The entity type to load the URL rewrites for
329
     * @param integer $entityId   The entity ID to load the URL rewrites for
330
     * @param integer $storeId    The store ID to load the URL rewrites for
331
     *
332
     * @return array The URL rewrites
333
     */
334
    public function getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId)
335
    {
336
        return $this->getUrlRewriteRepository()->findAllByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId);
337
    }
338
339
    /**
340
     * Return's an array with the URL rewrites for the passed SKU.
341
     *
342
     * @param string $sku The SKU to load the URL rewrites for
343
     *
344
     * @return array The URL rewrites
345
     */
346
    public function getUrlRewritesBySku($sku)
347
    {
348
        return $this->getUrlRewriteRepository()->findAllBySku($sku);
349
    }
350
351
    /**
352
     * Return's an array with the URL rewrite product category relations for the passed SKU.
353
     *
354
     * @param string $sku The SKU to load the URL rewrite product category relations for
355
     *
356
     * @return array The URL rewrite product category relations
357
     */
358
    public function getUrlRewriteProductCategoriesBySku($sku)
359
    {
360
        return $this->getUrlRewriteProductCategoryRepository()->findAllBySku($sku);
361
    }
362
363
    /**
364
     * Load's and return's the product with the passed SKU.
365
     *
366
     * @param string $sku The SKU of the product to load
367
     *
368
     * @return array The product
369
     */
370
    public function loadProduct($sku)
371
    {
372
        return $this->getProductRepository()->findOneBySku($sku);
373
    }
374
375
    /**
376
     * Load's and return's the varchar attribute with the passed params.
377
     *
378
     * @param integer $attributeCode The attribute code of the varchar attribute
379
     * @param integer $entityTypeId  The entity type ID of the varchar attribute
380
     * @param integer $storeId       The store ID of the varchar attribute
381
     * @param string  $value         The value of the varchar attribute
382
     *
383
     * @return array|null The varchar attribute
384
     */
385
    public function loadProductVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndValue($attributeCode, $entityTypeId, $storeId, $value)
386
    {
387
        return $this->getProductVarcharRepository()->findOneByAttributeCodeAndEntityTypeIdAndStoreIdAndValue($attributeCode, $entityTypeId, $storeId, $value);
388
    }
389
390
    /**
391
     * Return's the URL rewrite product category relation for the passed
392
     * URL rewrite ID.
393
     *
394
     * @param integer $urlRewriteId The URL rewrite ID to load the URL rewrite product category relation for
395
     *
396
     * @return array|false The URL rewrite product category relation
397
     */
398
    public function loadUrlRewriteProductCategory($urlRewriteId)
399
    {
400
        return $this->getUrlRewriteProductCategoryRepository()->load($urlRewriteId);
401
    }
402
403
    /**
404
     * Persist's the URL write with the passed data.
405
     *
406
     * @param array       $row  The URL rewrite to persist
407
     * @param string|null $name The name of the prepared statement that has to be executed
408
     *
409
     * @return string The ID of the persisted entity
410
     */
411
    public function persistUrlRewrite($row, $name = null)
412
    {
413
        return $this->getUrlRewriteAction()->persist($row, $name);
0 ignored issues
show
Unused Code introduced by
The call to UrlRewriteAction::persist() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
414
    }
415
416
    /**
417
     * Persist's the URL rewrite product => category relation with the passed data.
418
     *
419
     * @param array       $row  The URL rewrite product => category relation to persist
420
     * @param string|null $name The name of the prepared statement that has to be executed
421
     *
422
     * @return void
423
     */
424
    public function persistUrlRewriteProductCategory($row, $name = null)
425
    {
426
        $this->getUrlRewriteProductCategoryAction()->persist($row, $name);
0 ignored issues
show
Unused Code introduced by
The call to UrlRewriteProductCategoryAction::persist() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
427
    }
428
429
    /**
430
     * Delete's the URL rewrite with the passed attributes.
431
     *
432
     * @param array       $row  The attributes of the entity to delete
433
     * @param string|null $name The name of the prepared statement that has to be executed
434
     *
435
     * @return void
436
     */
437
    public function deleteUrlRewrite($row, $name = null)
438
    {
439
        $this->getUrlRewriteAction()->delete($row, $name);
440
    }
441
}
442