Completed
Push — 21.x ( bb9c8e )
by Tim
01:32
created

ProductMediaProcessor::setRawEntityLoader()   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 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Media\Services\ProductMediaProcessor
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-media
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Media\Services;
22
23
use TechDivision\Import\Loaders\LoaderInterface;
24
use TechDivision\Import\Actions\ActionInterface;
25
use TechDivision\Import\Connection\ConnectionInterface;
26
use TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryRepositoryInterface;
27
use TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueRepositoryInterface;
28
use TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueToEntityRepositoryInterface;
29
30
/**
31
 * The product media processor implementation.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-product-media
37
 * @link      http://www.techdivision.com
38
 */
39
class ProductMediaProcessor implements ProductMediaProcessorInterface
40
{
41
42
    /**
43
     * A PDO connection initialized with the values from the Doctrine EntityManager.
44
     *
45
     * @var \TechDivision\Import\Connection\ConnectionInterface
46
     */
47
    protected $connection;
48
49
    /**
50
     * The repository to load product media galleries.
51
     *
52
     * @var \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryRepositoryInterface
53
     */
54
    protected $productMediaGalleryRepository;
55
56
    /**
57
     * The repository to load product media gallery to entities.
58
     *
59
     * @var \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueToEntityRepositoryInterface
60
     */
61
    protected $productMediaGalleryValueToEntityRepository;
62
63
    /**
64
     * The repository to load product media gallery values.
65
     *
66
     * @var \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueRepositoryInterface
67
     */
68
    protected $productMediaGalleryValueRepository;
69
70
    /**
71
     * The action with the product media gallery CRUD methods.
72
     *
73
     * @var \TechDivision\Import\Actions\ActionInterface
74
     */
75
    protected $productMediaGalleryAction;
76
77
    /**
78
     * The action with the product media gallery value CRUD methods.
79
     *
80
     * @var \TechDivision\Import\Actions\ActionInterface
81
     */
82
    protected $productMediaGalleryValueAction;
83
84
    /**
85
     * The action with the product media gallery value to entity CRUD methods.
86
     *
87
     * @var \TechDivision\Import\Actions\ActionInterface
88
     */
89
    protected $productMediaGalleryValueToEntityAction;
90
91
    /**
92
     * The raw entity loader instance.
93
     *
94
     * @var \TechDivision\Import\Loaders\LoaderInterface
95
     */
96
    protected $rawEntityLoader;
97
98
    /**
99
     * Initialize the processor with the necessary assembler and repository instances.
100
     *
101
     * @param \TechDivision\Import\Connection\ConnectionInterface                                                 $connection                                 The connection to use
102
     * @param \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryRepositoryInterface              $productMediaGalleryRepository              The product media gallery repository to use
103
     * @param \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueRepositoryInterface         $productMediaGalleryValueRepository         The product media gallery value repository to use
104
     * @param \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueToEntityRepositoryInterface $productMediaGalleryValueToEntityRepository The product media gallery value to entity repository to use
105
     * @param \TechDivision\Import\Actions\ActionInterface                                                        $productMediaGalleryAction                  The product media gallery action to use
106
     * @param \TechDivision\Import\Actions\ActionInterface                                                        $productMediaGalleryValueAction             The product media gallery value action to use
107
     * @param \TechDivision\Import\Actions\ActionInterface                                                        $productMediaGalleryValueToEntityAction     The product media gallery value to entity action to use
108
     * @param \TechDivision\Import\Loaders\LoaderInterface                                                        $rawEntityLoader                            The raw entity loader instance
109
     */
110
    public function __construct(
111
        ConnectionInterface $connection,
112
        ProductMediaGalleryRepositoryInterface $productMediaGalleryRepository,
113
        ProductMediaGalleryValueRepositoryInterface $productMediaGalleryValueRepository,
114
        ProductMediaGalleryValueToEntityRepositoryInterface $productMediaGalleryValueToEntityRepository,
115
        ActionInterface $productMediaGalleryAction,
116
        ActionInterface $productMediaGalleryValueAction,
117
        ActionInterface $productMediaGalleryValueToEntityAction,
118
        LoaderInterface $rawEntityLoader
119
    ) {
120
        $this->setConnection($connection);
121
        $this->setProductMediaGalleryRepository($productMediaGalleryRepository);
122
        $this->setProductMediaGalleryValueRepository($productMediaGalleryValueRepository);
123
        $this->setProductMediaGalleryValueToEntityRepository($productMediaGalleryValueToEntityRepository);
124
        $this->setProductMediaGalleryAction($productMediaGalleryAction);
125
        $this->setProductMediaGalleryValueAction($productMediaGalleryValueAction);
126
        $this->setProductMediaGalleryValueToEntityAction($productMediaGalleryValueToEntityAction);
127
        $this->setRawEntityLoader($rawEntityLoader);
128
    }
129
130
    /**
131
     * Set's the raw entity loader instance.
132
     *
133
     * @param \TechDivision\Import\Loaders\LoaderInterface $rawEntityLoader The raw entity loader instance to set
134
     *
135
     * @return void
136
     */
137
    public function setRawEntityLoader(LoaderInterface $rawEntityLoader)
138
    {
139
        $this->rawEntityLoader = $rawEntityLoader;
140
    }
141
142
    /**
143
     * Return's the raw entity loader instance.
144
     *
145
     * @return \TechDivision\Import\Loaders\LoaderInterface The raw entity loader instance
146
     */
147
    public function getRawEntityLoader()
148
    {
149
        return $this->rawEntityLoader;
150
    }
151
152
    /**
153
     * Set's the passed connection.
154
     *
155
     * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection to set
156
     *
157
     * @return void
158
     */
159
    public function setConnection(ConnectionInterface $connection)
160
    {
161
        $this->connection = $connection;
162
    }
163
164
    /**
165
     * Return's the connection.
166
     *
167
     * @return \TechDivision\Import\Connection\ConnectionInterface The connection instance
168
     */
169
    public function getConnection()
170
    {
171
        return $this->connection;
172
    }
173
174
    /**
175
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
176
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
177
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
178
     * to autocommit mode.
179
     *
180
     * @return boolean Returns TRUE on success or FALSE on failure
181
     * @link http://php.net/manual/en/pdo.begintransaction.php
182
     */
183
    public function beginTransaction()
184
    {
185
        return $this->connection->beginTransaction();
186
    }
187
188
    /**
189
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
190
     * ProductProcessor::beginTransaction() starts a new transaction.
191
     *
192
     * @return boolean Returns TRUE on success or FALSE on failure
193
     * @link http://php.net/manual/en/pdo.commit.php
194
     */
195
    public function commit()
196
    {
197
        return $this->connection->commit();
198
    }
199
200
    /**
201
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
202
     *
203
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
204
     * rolled back the transaction.
205
     *
206
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
207
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
208
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
209
     *
210
     * @return boolean Returns TRUE on success or FALSE on failure
211
     * @link http://php.net/manual/en/pdo.rollback.php
212
     */
213
    public function rollBack()
214
    {
215
        return $this->connection->rollBack();
216
    }
217
218
    /**
219
     * Set's the repository to load product media gallery data.
220
     *
221
     * @param \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryRepositoryInterface $productMediaGalleryRepository The repository instance
222
     *
223
     * @return void
224
     */
225
    public function setProductMediaGalleryRepository(ProductMediaGalleryRepositoryInterface $productMediaGalleryRepository)
226
    {
227
        $this->productMediaGalleryRepository = $productMediaGalleryRepository;
228
    }
229
230
    /**
231
     * Return's the repository to load product media gallery data.
232
     *
233
     * @return \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryRepositoryInterface The repository instance
234
     */
235
    public function getProductMediaGalleryRepository()
236
    {
237
        return $this->productMediaGalleryRepository;
238
    }
239
240
    /**
241
     * Set's the repository to load product media gallery value to entity data.
242
     *
243
     * @param \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueToEntityRepositoryInterface $productMediaGalleryValueToEntityRepository The repository instance
244
     *
245
     * @return void
246
     */
247
    public function setProductMediaGalleryValueToEntityRepository(ProductMediaGalleryValueToEntityRepositoryInterface $productMediaGalleryValueToEntityRepository)
248
    {
249
        $this->productMediaGalleryValueToEntityRepository = $productMediaGalleryValueToEntityRepository;
250
    }
251
252
    /**
253
     * Return's the repository to load product media gallery value to entity data.
254
     *
255
     * @return \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueToEntityRepositoryInterface The repository instance
256
     */
257
    public function getProductMediaGalleryValueToEntityRepository()
258
    {
259
        return $this->productMediaGalleryValueToEntityRepository;
260
    }
261
262
    /**
263
     * Set's the repository to load product media gallery value data.
264
     *
265
     * @param \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueRepositoryInterface $productMediaGalleryValueRepository The repository instance
266
     *
267
     * @return void
268
     */
269
    public function setProductMediaGalleryValueRepository(ProductMediaGalleryValueRepositoryInterface $productMediaGalleryValueRepository)
270
    {
271
        $this->productMediaGalleryValueRepository = $productMediaGalleryValueRepository;
272
    }
273
274
    /**
275
     * Return's the repository to load product media gallery value data.
276
     *
277
     * @return \TechDivision\Import\Product\Media\Repositories\ProductMediaGalleryValueRepositoryInterface The repository instance
278
     */
279
    public function getProductMediaGalleryValueRepository()
280
    {
281
        return $this->productMediaGalleryValueRepository;
282
    }
283
284
    /**
285
     * Set's the action with the product media gallery CRUD methods.
286
     *
287
     * @param \TechDivision\Import\Actions\ActionInterface $productMediaGalleryAction The action with the product media gallery CRUD methods
288
     *
289
     * @return void
290
     */
291
    public function setProductMediaGalleryAction(ActionInterface $productMediaGalleryAction)
292
    {
293
        $this->productMediaGalleryAction = $productMediaGalleryAction;
294
    }
295
296
    /**
297
     * Return's the action with the product media gallery CRUD methods.
298
     *
299
     * @return \TechDivision\Import\Actions\ActionInterface The action with the product media gallery CRUD methods
300
     */
301
    public function getProductMediaGalleryAction()
302
    {
303
        return $this->productMediaGalleryAction;
304
    }
305
306
    /**
307
     * Set's the action with the product media gallery valueCRUD methods.
308
     *
309
     * @param \TechDivision\Import\Actions\ActionInterface $productMediaGalleryValueAction The action with the product media gallery value CRUD methods
310
     *
311
     * @return void
312
     */
313
    public function setProductMediaGalleryValueAction(ActionInterface $productMediaGalleryValueAction)
314
    {
315
        $this->productMediaGalleryValueAction = $productMediaGalleryValueAction;
316
    }
317
318
    /**
319
     * Return's the action with the product media gallery valueCRUD methods.
320
     *
321
     * @return \TechDivision\Import\Actions\ActionInterface The action with the product media gallery value CRUD methods
322
     */
323
    public function getProductMediaGalleryValueAction()
324
    {
325
        return $this->productMediaGalleryValueAction;
326
    }
327
328
    /**
329
     * Set's the action with the product media gallery value to entity CRUD methods.
330
     *
331
     * @param \TechDivision\Import\Actions\ActionInterface $productMediaGalleryValueToEntityAction The action with the product media gallery value to entity CRUD methods
332
     *
333
     * @return void
334
     */
335
    public function setProductMediaGalleryValueToEntityAction(ActionInterface $productMediaGalleryValueToEntityAction)
336
    {
337
        $this->productMediaGalleryValueToEntityAction = $productMediaGalleryValueToEntityAction;
338
    }
339
340
    /**
341
     * Return's the action with the product media gallery value to entity CRUD methods.
342
     *
343
     * @return \TechDivision\Import\Actions\ActionInterface $productMediaGalleryAction The action with the product media gallery value to entity CRUD methods
344
     */
345
    public function getProductMediaGalleryValueToEntityAction()
346
    {
347
        return $this->productMediaGalleryValueToEntityAction;
348
    }
349
350
    /**
351
     * Load's and return's a raw entity without primary key but the mandatory members only and nulled values.
352
     *
353
     * @param string $entityTypeCode The entity type code to return the raw entity for
354
     * @param array  $data           An array with data that will be used to initialize the raw entity with
355
     *
356
     * @return array The initialized entity
357
     */
358
    public function loadRawEntity($entityTypeCode, array $data = array())
359
    {
360
        return $this->getRawEntityLoader()->load($entityTypeCode, $data);
0 ignored issues
show
Unused Code introduced by
The call to LoaderInterface::load() has too many arguments starting with $entityTypeCode.

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...
Bug Best Practice introduced by
The return type of return $this->getRawEnti...entityTypeCode, $data); (ArrayAccess) is incompatible with the return type declared by the interface TechDivision\Import\Prod...nterface::loadRawEntity of type array.

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...
361
    }
362
363
    /**
364
     * Load's the product media gallery with the passed attribute ID + value.
365
     *
366
     * @param integer $attributeId The attribute ID of the product media gallery to load
367
     * @param string  $value       The value of the product media gallery to load
368
     *
369
     * @return array The product media gallery
370
     */
371
    public function loadProductMediaGallery($attributeId, $value)
372
    {
373
        return $this->getProductMediaGalleryRepository()->findOneByAttributeIdAndValue($attributeId, $value);
374
    }
375
376
    /**
377
     * Load's the product media gallery with the passed value/entity ID.
378
     *
379
     * @param integer $valueId  The value ID of the product media gallery value to entity to load
380
     * @param integer $entityId The entity ID of the product media gallery value to entity to load
381
     *
382
     * @return array The product media gallery
383
     */
384
    public function loadProductMediaGalleryValueToEntity($valueId, $entityId)
385
    {
386
        return $this->getProductMediaGalleryValueToEntityRepository()->findOneByValueIdAndEntityId($valueId, $entityId);
387
    }
388
389
    /**
390
     * Load's the product media gallery value with the passed value/store/parent ID.
391
     *
392
     * @param integer $valueId  The value ID of the product media gallery value to load
393
     * @param string  $storeId  The store ID of the product media gallery value to load
394
     * @param string  $entityId The entity ID of the parent product of the product media gallery value to load
395
     *
396
     * @return array The product media gallery value
397
     */
398
    public function loadProductMediaGalleryValue($valueId, $storeId, $entityId)
399
    {
400
        return $this->getProductMediaGalleryValueRepository()->findOneByValueIdAndStoreIdAndEntityId($valueId, $storeId, $entityId);
401
    }
402
403
    /**
404
     * Load's the product media gallery entities with the passed SKU.
405
     *
406
     * @param string $sku The SKU to load the media gallery entities for
407
     *
408
     * @return array The product media gallery entities
409
     */
410
    public function getProductMediaGalleriesBySku($sku)
411
    {
412
        return $this->getProductMediaGalleryRepository()->findAllBySku($sku);
413
    }
414
415
    /**
416
     * Persist's the passed product media gallery data and return's the ID.
417
     *
418
     * @param array       $productMediaGallery The product media gallery data to persist
419
     * @param string|null $name                The name of the prepared statement that has to be executed
420
     *
421
     * @return string The ID of the persisted entity
422
     */
423
    public function persistProductMediaGallery($productMediaGallery, $name = null)
424
    {
425
        return $this->getProductMediaGalleryAction()->persist($productMediaGallery, $name);
426
    }
427
428
    /**
429
     * Persist's the passed product media gallery value data.
430
     *
431
     * @param array       $productMediaGalleryValue The product media gallery value data to persist
432
     * @param string|null $name                     The name of the prepared statement that has to be executed
433
     *
434
     * @return void
435
     */
436
    public function persistProductMediaGalleryValue($productMediaGalleryValue, $name = null)
437
    {
438
        $this->getProductMediaGalleryValueAction()->persist($productMediaGalleryValue, $name);
439
    }
440
441
    /**
442
     * Persist's the passed product media gallery value to entity data.
443
     *
444
     * @param array       $productMediaGalleryValuetoEntity The product media gallery value to entity data 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 persistProductMediaGalleryValueToEntity($productMediaGalleryValuetoEntity, $name = null)
450
    {
451
        $this->getProductMediaGalleryValueToEntityAction()->persist($productMediaGalleryValuetoEntity, $name);
452
    }
453
454
    /**
455
     * Delete's the passed product media gallery data.
456
     *
457
     * @param array       $row  The product media gallery data to be deleted
458
     * @param string|null $name The name of the prepared statement that has to be executed
459
     *
460
     * @return string The ID of the persisted entity
461
     */
462
    public function deleteProductMediaGallery(array $row, $name = null)
463
    {
464
        return $this->getProductMediaGalleryAction()->delete($row, $name);
465
    }
466
}
467