Completed
Push — master ( 40a934...77b4e9 )
by
unknown
02:28
created

getPrimaryKeyMemberName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
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\Dbal\Actions\ActionInterface;
24
use TechDivision\Import\Dbal\Connection\ConnectionInterface;
25
use TechDivision\Import\Product\Repositories\ProductRepositoryInterface;
26
use TechDivision\Import\Product\Repositories\ProductVarcharRepositoryInterface;
27
use TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepositoryInterface;
28
use TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepositoryInterface;
29
use TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface;
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\Dbal\Connection\ConnectionInterface
47
     */
48
    protected $connection;
49
50
    /**
51
     * The primary key util instance.
52
     *
53
     * @var \TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface
54
     */
55
    protected $primaryKeyUtil;
56
57
    /**
58
     * The action for URL rewrite CRUD methods.
59
     *
60
     * @var \TechDivision\Import\Dbal\Actions\ActionInterface
61
     */
62
    protected $urlRewriteAction;
63
64
    /**
65
     * The action for URL rewrite product category CRUD methods.
66
     *
67
     * @var \TechDivision\Import\Dbal\Actions\ActionInterface
68
     */
69
    protected $urlRewriteProductCategoryAction;
70
71
    /**
72
     * The repository to load the products with.
73
     *
74
     * @var \TechDivision\Import\Product\Repositories\ProductRepositoryInterface
75
     */
76
    protected $productRepository;
77
78
    /**
79
     * The repository to load the URL rewrites with.
80
     *
81
     * @var \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepositoryInterface
82
     */
83
    protected $urlRewriteRepository;
84
85
    /**
86
     * The repository to load the URL rewrite product category relations with.
87
     *
88
     * @var \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepositoryInterface
89
     */
90
    protected $urlRewriteProductCategoryRepository;
91
92
    /**
93
     * Initialize the processor with the necessary assembler and repository instances.
94
     *
95
     * @param \TechDivision\Import\Dbal\Connection\ConnectionInterface                                          $connection                          The connection to use
96
     * @param \TechDivision\Import\Product\Repositories\ProductRepositoryInterface                              $productRepository                   The product repository to use
97
     * @param \TechDivision\Import\Product\Repositories\ProductVarcharRepositoryInterface                       $productVarcharRepository            The product varchar repository to use
98
     * @param \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepositoryInterface                $urlRewriteRepository                The URL rewrite repository to use
99
     * @param \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepositoryInterface $urlRewriteProductCategoryRepository The URL rewrite product category repository to use
100
     * @param \TechDivision\Import\Dbal\Actions\ActionInterface                                                 $urlRewriteAction                    The URL rewrite action to use
101
     * @param \TechDivision\Import\Dbal\Actions\ActionInterface                                                 $urlRewriteProductCategoryAction     The URL rewrite product category action to use
102
     * @param \TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface                                           $primaryKeyUtil                      The primary key util
103
     */
104
    public function __construct(
105
        ConnectionInterface $connection,
106
        ProductRepositoryInterface $productRepository,
107
        ProductVarcharRepositoryInterface $productVarcharRepository,
108
        UrlRewriteRepositoryInterface $urlRewriteRepository,
109
        UrlRewriteProductCategoryRepositoryInterface $urlRewriteProductCategoryRepository,
110
        ActionInterface $urlRewriteAction,
111
        ActionInterface $urlRewriteProductCategoryAction,
112
        PrimaryKeyUtilInterface $primaryKeyUtil
113
    ) {
114
        $this->setConnection($connection);
115
        $this->setPrimaryKeyUtil($primaryKeyUtil);
116
        $this->setProductRepository($productRepository);
117
        $this->setProductVarcharRepository($productVarcharRepository);
118
        $this->setUrlRewriteRepository($urlRewriteRepository);
119
        $this->setUrlRewriteProductCategoryRepository($urlRewriteProductCategoryRepository);
120
        $this->setUrlRewriteAction($urlRewriteAction);
121
        $this->setUrlRewriteProductCategoryAction($urlRewriteProductCategoryAction);
122
    }
123
124
    /**
125
     * Set's the passed connection.
126
     *
127
     * @param \TechDivision\Import\Dbal\Connection\ConnectionInterface $connection The connection to set
128
     *
129
     * @return void
130
     */
131
    public function setConnection(ConnectionInterface $connection)
132
    {
133
        $this->connection = $connection;
134
    }
135
136
    /**
137
     * Return's the connection.
138
     *
139
     * @return \TechDivision\Import\Dbal\Connection\ConnectionInterface The connection instance
140
     */
141
    public function getConnection()
142
    {
143
        return $this->connection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->connection; (TechDivision\Import\Dbal...ion\ConnectionInterface) is incompatible with the return type declared by the interface TechDivision\Import\Prod...nterface::getConnection of type TechDivision\Import\Connection\ConnectionInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
144
    }
145
146
    /**
147
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
148
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
149
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
150
     * to autocommit mode.
151
     *
152
     * @return boolean Returns TRUE on success or FALSE on failure
153
     * @link http://php.net/manual/en/pdo.begintransaction.php
154
     */
155
    public function beginTransaction()
156
    {
157
        return $this->connection->beginTransaction();
158
    }
159
160
    /**
161
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
162
     * ProductProcessor::beginTransaction() starts a new transaction.
163
     *
164
     * @return boolean Returns TRUE on success or FALSE on failure
165
     * @link http://php.net/manual/en/pdo.commit.php
166
     */
167
    public function commit()
168
    {
169
        return $this->connection->commit();
170
    }
171
172
    /**
173
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
174
     *
175
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
176
     * rolled back the transaction.
177
     *
178
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
179
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
180
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
181
     *
182
     * @return boolean Returns TRUE on success or FALSE on failure
183
     * @link http://php.net/manual/en/pdo.rollback.php
184
     */
185
    public function rollBack()
186
    {
187
        return $this->connection->rollBack();
188
    }
189
190
    /**
191
     * Set's the action with the URL rewrite CRUD methods.
192
     *
193
     * @param \TechDivision\Import\Dbal\Actions\ActionInterface $urlRewriteAction The action with the URL rewrite CRUD methods
194
     *
195
     * @return void
196
     */
197
    public function setUrlRewriteAction(ActionInterface $urlRewriteAction)
198
    {
199
        $this->urlRewriteAction = $urlRewriteAction;
200
    }
201
202
    /**
203
     * Return's the action with the URL rewrite CRUD methods.
204
     *
205
     * @return \TechDivision\Import\Dbal\Actions\ActionInterface The action instance
206
     */
207
    public function getUrlRewriteAction()
208
    {
209
        return $this->urlRewriteAction;
210
    }
211
212
    /**
213
     * Set's the action with the URL rewrite product category CRUD methods.
214
     *
215
     * @param \TechDivision\Import\Dbal\Actions\ActionInterface $urlRewriteProductCategoryAction The action with the URL rewrite CRUD methods
216
     *
217
     * @return void
218
     */
219
    public function setUrlRewriteProductCategoryAction(ActionInterface $urlRewriteProductCategoryAction)
220
    {
221
        $this->urlRewriteProductCategoryAction = $urlRewriteProductCategoryAction;
222
    }
223
224
    /**
225
     * Return's the action with the URL rewrite product category CRUD methods.
226
     *
227
     * @return \TechDivision\Import\Dbal\Actions\ActionInterface The action instance
228
     */
229
    public function getUrlRewriteProductCategoryAction()
230
    {
231
        return $this->urlRewriteProductCategoryAction;
232
    }
233
234
    /**
235
     * Set's the repository to load the product varchar attribute with.
236
     *
237
     * @param \TechDivision\Import\Product\Repositories\ProductVarcharRepositoryInterface $productVarcharRepository The repository instance
238
     *
239
     * @return void
240
     */
241
    public function setProductVarcharRepository(ProductVarcharRepositoryInterface $productVarcharRepository)
242
    {
243
        $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...
244
    }
245
246
    /**
247
     * Return's the repository to load the product varchar attribute with.
248
     *
249
     * @return \TechDivision\Import\Product\Repositories\ProductVarcharRepositoryInterface The repository instance
250
     */
251
    public function getProductVarcharRepository()
252
    {
253
        return $this->productVarcharRepository;
254
    }
255
256
    /**
257
     * Set's the repository to load the URL rewrites with.
258
     *
259
     * @param \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepositoryInterface $urlRewriteRepository The repository instance
260
     *
261
     * @return void
262
     */
263
    public function setUrlRewriteRepository(UrlRewriteRepositoryInterface $urlRewriteRepository)
264
    {
265
        $this->urlRewriteRepository = $urlRewriteRepository;
266
    }
267
268
    /**
269
     * Return's the repository to load the URL rewrites with.
270
     *
271
     * @return \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteRepositoryInterface The repository instance
272
     */
273
    public function getUrlRewriteRepository()
274
    {
275
        return $this->urlRewriteRepository;
276
    }
277
278
    /**
279
     * Set's the repository to load the URL rewrite product category relations with.
280
     *
281
     * @param \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepositoryInterface $urlRewriteProductCategoryRepository The repository instance
282
     *
283
     * @return void
284
     */
285
    public function setUrlRewriteProductCategoryRepository(UrlRewriteProductCategoryRepositoryInterface $urlRewriteProductCategoryRepository)
286
    {
287
        $this->urlRewriteProductCategoryRepository = $urlRewriteProductCategoryRepository;
288
    }
289
290
    /**
291
     * Return's the repository to load the URL rewrite product category relations with.
292
     *
293
     * @return \TechDivision\Import\Product\UrlRewrite\Repositories\UrlRewriteProductCategoryRepositoryInterface The repository instance
294
     */
295
    public function getUrlRewriteProductCategoryRepository()
296
    {
297
        return $this->urlRewriteProductCategoryRepository;
298
    }
299
300
    /**
301
     * Set's the repository to load the products with.
302
     *
303
     * @param \TechDivision\Import\Product\Repositories\ProductRepositoryInterface $productRepository The repository instance
304
     *
305
     * @return void
306
     */
307
    public function setProductRepository(ProductRepositoryInterface $productRepository)
308
    {
309
        $this->productRepository = $productRepository;
310
    }
311
312
    /**
313
     * Return's the repository to load the products with.
314
     *
315
     * @return \TechDivision\Import\Product\Repositories\ProductRepositoryInterface The repository instance
316
     */
317
    public function getProductRepository()
318
    {
319
        return $this->productRepository;
320
    }
321
322
    /**
323
     * Return's the URL rewrites for the passed URL entity type and ID.
324
     *
325
     * @param string  $entityType The entity type to load the URL rewrites for
326
     * @param integer $entityId   The entity ID to laod the rewrites for
327
     *
328
     * @return array The URL rewrites
329
     */
330
    public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId)
331
    {
332
        return $this->getUrlRewriteRepository()->findAllByEntityTypeAndEntityId($entityType, $entityId);
333
    }
334
335
    /**
336
     * Return's the URL rewrites for the passed URL entity type and ID.
337
     *
338
     * @param string  $entityType The entity type to load the URL rewrites for
339
     * @param integer $entityId   The entity ID to load the URL rewrites for
340
     * @param integer $storeId    The store ID to load the URL rewrites for
341
     *
342
     * @return array The URL rewrites
343
     */
344
    public function getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId)
345
    {
346
        return $this->getUrlRewriteRepository()->findAllByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId);
347
    }
348
349
    /**
350
     * Return's an array with the URL rewrites for the passed SKU.
351
     *
352
     * @param string $sku The SKU to load the URL rewrites for
353
     *
354
     * @return array The URL rewrites
355
     */
356
    public function getUrlRewritesBySku($sku)
357
    {
358
        return $this->getUrlRewriteRepository()->findAllBySku($sku);
359
    }
360
361
    /**
362
     * Return's an array with the URL rewrite product category relations for the passed SKU.
363
     *
364
     * @param string $sku The SKU to load the URL rewrite product category relations for
365
     *
366
     * @return array The URL rewrite product category relations
367
     */
368
    public function getUrlRewriteProductCategoriesBySku($sku)
369
    {
370
        return $this->getUrlRewriteProductCategoryRepository()->findAllBySku($sku);
371
    }
372
373
    /**
374
     * Load's and return's the product with the passed SKU.
375
     *
376
     * @param string $sku The SKU of the product to load
377
     *
378
     * @return array The product
379
     */
380
    public function loadProduct($sku)
381
    {
382
        return $this->getProductRepository()->findOneBySku($sku);
383
    }
384
385
    /**
386
     * Load's and return's the varchar attribute with the passed params.
387
     *
388
     * @param integer $attributeCode The attribute code of the varchar attribute
389
     * @param integer $entityTypeId  The entity type ID of the varchar attribute
390
     * @param integer $storeId       The store ID of the varchar attribute
391
     * @param string  $pk            The primary key of the product
392
     *
393
     * @return array|null The varchar attribute
394
     */
395
    public function loadProductVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndPK($attributeCode, $entityTypeId, $storeId, $pk)
396
    {
397
        return $this->getProductVarcharRepository()->findOneByAttributeCodeAndEntityTypeIdAndStoreIdAndPk($attributeCode, $entityTypeId, $storeId, $pk);
0 ignored issues
show
Bug introduced by
The method findOneByAttributeCodeAn...TypeIdAndStoreIdAndPk() does not exist on TechDivision\Import\Prod...charRepositoryInterface. Did you maybe mean findOneByAttributeCodeAn...eIdAndStoreIdAndValue()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
398
    }
399
400
    /**
401
     * Load's and return's the varchar attribute with the passed params.
402
     *
403
     * @param integer $attributeCode The attribute code of the varchar attribute
404
     * @param integer $entityTypeId  The entity type ID of the varchar attribute
405
     * @param integer $storeId       The store ID of the varchar attribute
406
     * @param string  $value         The value of the varchar attribute
407
     *
408
     * @return array|null The varchar attribute
409
     */
410
    public function loadProductVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndValue($attributeCode, $entityTypeId, $storeId, $value)
411
    {
412
        return $this->getProductVarcharRepository()->findOneByAttributeCodeAndEntityTypeIdAndStoreIdAndValue($attributeCode, $entityTypeId, $storeId, $value);
413
    }
414
415
    /**
416
     * Return's the URL rewrite product category relation for the passed
417
     * URL rewrite ID.
418
     *
419
     * @param integer $urlRewriteId The URL rewrite ID to load the URL rewrite product category relation for
420
     *
421
     * @return array|false The URL rewrite product category relation
422
     */
423
    public function loadUrlRewriteProductCategory($urlRewriteId)
424
    {
425
        return $this->getUrlRewriteProductCategoryRepository()->load($urlRewriteId);
426
    }
427
428
    /**
429
     * Persist's the URL write with the passed data.
430
     *
431
     * @param array       $row  The URL rewrite to persist
432
     * @param string|null $name The name of the prepared statement that has to be executed
433
     *
434
     * @return string The ID of the persisted entity
435
     */
436
    public function persistUrlRewrite($row, $name = null)
437
    {
438
        return $this->getUrlRewriteAction()->persist($row, $name);
439
    }
440
441
    /**
442
     * Persist's the URL rewrite product => category relation with the passed data.
443
     *
444
     * @param array       $row  The URL rewrite product => category relation to persist
445
     * @param string|null $name The name of the prepared statement that has to be executed
446
     *
447
     * @return void
448
     */
449
    public function persistUrlRewriteProductCategory($row, $name = null)
450
    {
451
        $this->getUrlRewriteProductCategoryAction()->persist($row, $name);
452
    }
453
454
    /**
455
     * Delete's the URL rewrite with the passed attributes.
456
     *
457
     * @param array       $row  The attributes of the entity to delete
458
     * @param string|null $name The name of the prepared statement that has to be executed
459
     *
460
     * @return void
461
     */
462
    public function deleteUrlRewrite($row, $name = null)
463
    {
464
        $this->getUrlRewriteAction()->delete($row, $name);
465
    }
466
467
    /**
468
     * Sets the passed primary key util instance.
469
     *
470
     * @param \TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface $primaryKeyUtil The primary key util instance
471
     *
472
     * @return void
473
     */
474
    public function setPrimaryKeyUtil(PrimaryKeyUtilInterface $primaryKeyUtil)
475
    {
476
        $this->primaryKeyUtil = $primaryKeyUtil;
477
    }
478
479
    /**
480
     * Returns the primary key util instance.
481
     *
482
     * @return \TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface The primary key util instance
483
     */
484
    public function getPrimaryKeyUtil()
485
    {
486
        return $this->primaryKeyUtil;
487
    }
488
489
    /**
490
     * Returns the primary key member name for the actual Magento edition.
491
     *
492
     * @return string The primary key member name
493
     * @see \TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface::getPrimaryKeyMemberName()
494
     */
495
    public function getPrimaryKeyMemberName()
496
    {
497
        return $this->getPrimaryKeyUtil()->getPrimaryKeyMemberName();
498
    }
499
}
500