Completed
Push — master ( 235157...2175ac )
by Tim
10s
created

ProductBundleProcessor   C

Complexity

Total Complexity 32

Size/Duplication

Total Lines 453
Duplicated Lines 0 %

Coupling/Cohesion

Components 10
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 32
lcom 10
cbo 5
dl 0
loc 453
ccs 0
cts 128
cp 0
rs 6.3999
c 0
b 0
f 0

32 Methods

Rating   Name   Duplication   Size   Complexity  
A setConnection() 0 4 1
A getConnection() 0 4 1
A beginTransaction() 0 4 1
A commit() 0 4 1
A rollBack() 0 4 1
A setEavAttributeOptionValueRepository() 0 4 1
A getEavAttributeOptionValueRepository() 0 4 1
A setProductBundleOptionAction() 0 4 1
A getProductBundleOptionAction() 0 4 1
A setProductBundleOptionValueAction() 0 4 1
A getProductBundleOptionValueAction() 0 4 1
A setProductBundleSelectionAction() 0 4 1
A getProductBundleSelectionAction() 0 4 1
A setProductBundleSelectionPriceAction() 0 4 1
A getProductBundleSelectionPriceAction() 0 4 1
A setBundleOptionRepository() 0 4 1
A getBundleOptionRepository() 0 4 1
A setBundleOptionValueRepository() 0 4 1
A getBundleOptionValueRepository() 0 4 1
A setBundleSelectionRepository() 0 4 1
A getBundleSelectionRepository() 0 4 1
A setBundleSelectionPriceRepository() 0 4 1
A getBundleSelectionPriceRepository() 0 4 1
A getEavAttributeOptionValueByOptionValueAndStoreId() 0 4 1
A loadBundleOption() 0 4 1
A loadBundleOptionValue() 0 4 1
A loadBundleSelection() 0 4 1
A loadBundleSelectionPrice() 0 4 1
A persistProductBundleOption() 0 4 1
A persistProductBundleOptionValue() 0 4 1
A persistProductBundleSelection() 0 4 1
A persistProductBundleSelectionPrice() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Bundle\Services\ProductProcessor
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-bundle
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Bundle\Services;
22
23
/**
24
 * A SLSB providing methods to load product data using a PDO connection.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import-product-bundle
30
 * @link      http://www.techdivision.com
31
 */
32
class ProductBundleProcessor implements ProductBundleProcessorInterface
33
{
34
35
    /**
36
     * A PDO connection initialized with the values from the Doctrine EntityManager.
37
     *
38
     * @var \PDO
39
     */
40
    protected $connection;
41
42
    /**
43
     * The repository to access EAV attribute option values.
44
     *
45
     * @var \TechDivision\Import\Repositories\EavAttributeOptionValueRepository
46
     */
47
    protected $eavAttributeOptionValueRepository;
48
49
    /**
50
     * The action for product bundle option CRUD methods.
51
     *
52
     * @var \TechDivision\Import\Product\Bundle\Actions\ProductBundleOptionAction
53
     */
54
    protected $productBundleOptionAction;
55
56
    /**
57
     * The action for product bundle option value CRUD methods.
58
     *
59
     * @var \TechDivision\Import\Product\Bundle\Actions\ProductBundleOptionValueAction
60
     */
61
    protected $productBundleOptionValueAction;
62
63
    /**
64
     * The action for product bundle selection CRUD methods.
65
     *
66
     * @var \TechDivision\Import\Product\Bundle\Actions\ProductBundleSelectionAction
67
     */
68
    protected $productBundleSelectionAction;
69
70
    /**
71
     * The action for product bundle selection price CRUD methods.
72
     *
73
     * @var \TechDivision\Import\Product\Bundle\Actions\ProductBundleSelectionPriceAction
74
     */
75
    protected $productBundleSelectionPriceAction;
76
77
    /**
78
     * The repository to load bundle option data.
79
     *
80
     * @var \TechDivision\Import\Product\Bundle\Respository\BundleOptionRepository
81
     */
82
    protected $bundleOptionRespository;
83
84
    /**
85
     * The repository to load bundle option value data.
86
     *
87
     * @var \TechDivision\Import\Product\Bundle\Respository\BundleOptionValueRepository
88
     */
89
    protected $bundleOptionValueRespository;
90
91
    /**
92
     * The repository to load bundle selection data.
93
     *
94
     * @var \TechDivision\Import\Product\Bundle\Respository\BundleSelectionRepository
95
     */
96
    protected $bundleSelectionRespository;
97
98
    /**
99
     * The repository to load bundle selection price data.
100
     *
101
     * @var \TechDivision\Import\Product\Bundle\Respository\BundleSelectionPriceRepository
102
     */
103
    protected $bundleSelectionPriceRespository;
104
105
    /**
106
     * Set's the passed connection.
107
     *
108
     * @param \PDO $connection The connection to set
109
     *
110
     * @return void
111
     */
112
    public function setConnection(\PDO $connection)
113
    {
114
        $this->connection = $connection;
115
    }
116
117
    /**
118
     * Return's the connection.
119
     *
120
     * @return \PDO The connection instance
121
     */
122
    public function getConnection()
123
    {
124
        return $this->connection;
125
    }
126
127
    /**
128
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
129
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
130
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
131
     * to autocommit mode.
132
     *
133
     * @return boolean Returns TRUE on success or FALSE on failure
134
     * @link http://php.net/manual/en/pdo.begintransaction.php
135
     */
136
    public function beginTransaction()
137
    {
138
        return $this->connection->beginTransaction();
139
    }
140
141
    /**
142
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
143
     * ProductProcessor::beginTransaction() starts a new transaction.
144
     *
145
     * @return boolean Returns TRUE on success or FALSE on failure
146
     * @link http://php.net/manual/en/pdo.commit.php
147
     */
148
    public function commit()
149
    {
150
        return $this->connection->commit();
151
    }
152
153
    /**
154
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
155
     *
156
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
157
     * rolled back the transaction.
158
     *
159
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
160
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
161
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
162
     *
163
     * @return boolean Returns TRUE on success or FALSE on failure
164
     * @link http://php.net/manual/en/pdo.rollback.php
165
     */
166
    public function rollBack()
167
    {
168
        return $this->connection->rollBack();
169
    }
170
171
    /**
172
     * Set's the repository to access EAV attribute option values.
173
     *
174
     * @param \TechDivision\Import\Repositories\EavAttributeOptionValueRepository $eavAttributeOptionValueRepository The repository to access EAV attribute option values
175
     *
176
     * @return void
177
     */
178
    public function setEavAttributeOptionValueRepository($eavAttributeOptionValueRepository)
179
    {
180
        $this->eavAttributeOptionValueRepository = $eavAttributeOptionValueRepository;
181
    }
182
183
    /**
184
     * Return's the repository to access EAV attribute option values.
185
     *
186
     * @return \TechDivision\Import\Repositories\EavAttributeOptionValueRepository The repository instance
187
     */
188
    public function getEavAttributeOptionValueRepository()
189
    {
190
        return $this->eavAttributeOptionValueRepository;
191
    }
192
193
    /**
194
     * Set's the action with the product bundle option CRUD methods.
195
     *
196
     * @param \TechDivision\Import\Product\Bundle\Actions\ProductBundleOptionAction $productBundleOptionAction The action with the product bundle option CRUD methods
197
     *
198
     * @return void
199
     */
200
    public function setProductBundleOptionAction($productBundleOptionAction)
201
    {
202
        $this->productBundleOptionAction = $productBundleOptionAction;
203
    }
204
205
    /**
206
     * Return's the action with the product bundle option CRUD methods.
207
     *
208
     * @return \TechDivision\Import\Product\Bundle\Actions\ProductBundleOptionAction The action instance
209
     */
210
    public function getProductBundleOptionAction()
211
    {
212
        return $this->productBundleOptionAction;
213
    }
214
215
    /**
216
     * Set's the action with the product bundle option value CRUD methods.
217
     *
218
     * @param \TechDivision\Import\Product\Bundle\Actions\ProductBundleOptionValueAction $productBundleOptionValueAction The action with the product bundle option value CRUD methods
219
     *
220
     * @return void
221
     */
222
    public function setProductBundleOptionValueAction($productBundleOptionValueAction)
223
    {
224
        $this->productBundleOptionValueAction = $productBundleOptionValueAction;
225
    }
226
227
    /**
228
     * Return's the action with the product bundle option value CRUD methods.
229
     *
230
     * @return \TechDivision\Import\Product\Bundle\Actions\ProductBundleOptionValueAction The action instance
231
     */
232
    public function getProductBundleOptionValueAction()
233
    {
234
        return $this->productBundleOptionValueAction;
235
    }
236
237
    /**
238
     * Set's the action with the product bundle selection CRUD methods.
239
     *
240
     * @param \TechDivision\Import\Product\Bundle\Actions\ProductBundleSelectionAction $productBundleSelectionAction The action with the product bundle selection CRUD methods
241
     *
242
     * @return void
243
     */
244
    public function setProductBundleSelectionAction($productBundleSelectionAction)
245
    {
246
        $this->productBundleSelectionAction = $productBundleSelectionAction;
247
    }
248
249
    /**
250
     * Return's the action with the product bundle selection CRUD methods.
251
     *
252
     * @return \TechDivision\Import\Product\Bundle\Actions\ProductBundleSelectionAction The action instance
253
     */
254
    public function getProductBundleSelectionAction()
255
    {
256
        return $this->productBundleSelectionAction;
257
    }
258
259
    /**
260
     * Set's the action with the product bundle selection price CRUD methods.
261
     *
262
     * @param \TechDivision\Import\Product\Bundle\Actions\ProductBundleSelectionPriceAction $productBundleSelectionPriceAction The action with the product bundle selection price CRUD methods
263
     *
264
     * @return void
265
     */
266
    public function setProductBundleSelectionPriceAction($productBundleSelectionPriceAction)
267
    {
268
        $this->productBundleSelectionPriceAction = $productBundleSelectionPriceAction;
269
    }
270
271
    /**
272
     * Return's the action with the product bundle selection price CRUD methods.
273
     *
274
     * @return \TechDivision\Import\Product\Bundle\Actions\ProductBundleSelectionPriceAction The action instance
275
     */
276
    public function getProductBundleSelectionPriceAction()
277
    {
278
        return $this->productBundleSelectionPriceAction;
279
    }
280
281
    /**
282
     * Set's the repository to load bundle option data.
283
     *
284
     * @param \TechDivision\Import\Product\Bundle\Repository\BundleOptionRespository $bundleOptionRespository The repository instance
285
     *
286
     * @return void
287
     */
288
    public function setBundleOptionRepository($bundleOptionRespository)
289
    {
290
        $this->bundleOptionRespository = $bundleOptionRespository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $bundleOptionRespository of type object<TechDivision\Impo...undleOptionRespository> is incompatible with the declared type object<TechDivision\Impo...BundleOptionRepository> of property $bundleOptionRespository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
291
    }
292
293
    /**
294
     * Return's the respository to load bundle option data.
295
     *
296
     * @return \TechDivision\Import\Product\Bundle\Repository\BundleOptionRespository The repository instance
297
     */
298
    public function getBundleOptionRepository()
299
    {
300
        return $this->bundleOptionRespository;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->bundleOptionRespository; (TechDivision\Import\Prod...\BundleOptionRepository) is incompatible with the return type declared by the interface TechDivision\Import\Prod...tBundleOptionRepository of type TechDivision\Import\Prod...BundleOptionRespository.

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...
301
    }
302
303
    /**
304
     * Set's the repository to load bundle option value data.
305
     *
306
     * @param \TechDivision\Import\Product\Bundle\Repository\BundleOptionValueRespository $bundleOptionValueRespository The repository instance
307
     *
308
     * @return void
309
     */
310
    public function setBundleOptionValueRepository($bundleOptionValueRespository)
311
    {
312
        $this->bundleOptionValueRespository = $bundleOptionValueRespository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $bundleOptionValueRespository of type object<TechDivision\Impo...OptionValueRespository> is incompatible with the declared type object<TechDivision\Impo...eOptionValueRepository> of property $bundleOptionValueRespository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
313
    }
314
315
    /**
316
     * Return's the respository to load bundle option value data.
317
     *
318
     * @return \TechDivision\Import\Product\Bundle\Repository\BundleOptionValueRespository The repository instance
319
     */
320
    public function getBundleOptionValueRepository()
321
    {
322
        return $this->bundleOptionValueRespository;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->bundleOptionValueRespository; (TechDivision\Import\Prod...leOptionValueRepository) is incompatible with the return type declared by the interface TechDivision\Import\Prod...leOptionValueRepository of type TechDivision\Import\Prod...eOptionValueRespository.

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...
323
    }
324
325
    /**
326
     * Set's the repository to load bundle selection data.
327
     *
328
     * @param \TechDivision\Import\Product\Bundle\Repository\BundleSelectionRespository $bundleSelectionRespository The repository instance
329
     *
330
     * @return void
331
     */
332
    public function setBundleSelectionRepository($bundleSelectionRespository)
333
    {
334
        $this->bundleSelectionRespository = $bundleSelectionRespository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $bundleSelectionRespository of type object<TechDivision\Impo...leSelectionRespository> is incompatible with the declared type object<TechDivision\Impo...dleSelectionRepository> of property $bundleSelectionRespository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
335
    }
336
337
    /**
338
     * Return's the respository to load bundle selection data.
339
     *
340
     * @return \TechDivision\Import\Product\Bundle\Repository\BundleSelectionRespository The repository instance
341
     */
342
    public function getBundleSelectionRepository()
343
    {
344
        return $this->bundleSelectionRespository;
345
    }
346
347
    /**
348
     * Set's the repository to load bundle selection price data.
349
     *
350
     * @param \TechDivision\Import\Product\Bundle\Repository\BundleSelectionPriceRespository $bundleSelectionPriceRespository The repository instance
351
     *
352
     * @return void
353
     */
354
    public function setBundleSelectionPriceRepository($bundleSelectionPriceRespository)
355
    {
356
        $this->bundleSelectionPriceRespository = $bundleSelectionPriceRespository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $bundleSelectionPriceRespository of type object<TechDivision\Impo...ectionPriceRespository> is incompatible with the declared type object<TechDivision\Impo...lectionPriceRepository> of property $bundleSelectionPriceRespository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
357
    }
358
359
    /**
360
     * Return's the respository to load bundle selection price data.
361
     *
362
     * @return \TechDivision\Import\Product\Bundle\Repository\BundleSelectionPriceRespository The repository instance
363
     */
364
    public function getBundleSelectionPriceRepository()
365
    {
366
        return $this->bundleSelectionPriceRespository;
367
    }
368
369
    /**
370
     * Return's the attribute option value with the passed value and store ID.
371
     *
372
     * @param mixed   $value   The option value
373
     * @param integer $storeId The ID of the store
374
     *
375
     * @return array|boolean The attribute option value instance
376
     */
377
    public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId)
378
    {
379
        return $this->getEavAttributeOptionValueRepository()->findEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId);
380
    }
381
382
    /**
383
     * Load's the bundle option with the passed name, store + parent ID.
384
     *
385
     * @param string  $title    The title of the bundle option to be returned
386
     * @param integer $storeId  The store ID of the bundle option to be returned
387
     * @param integer $parentId The entity of the product the bundle option is related with
388
     *
389
     * @return array The bundle option
390
     */
391
    public function loadBundleOption($title, $storeId, $parentId)
392
    {
393
        return $this->getBundleOptionRepository()->findOneByNameAndStoreIdAndParentId($title, $storeId, $parentId);
394
    }
395
396
    /**
397
     * Load's the bundle option value with the passed name, store + parent ID.
398
     *
399
     * @param string  $title    The title of the bundle option value to be returned
400
     * @param integer $storeId  The store ID of the bundle option value to be returned
401
     * @param integer $parentId The entity of the product the bundle option value is related with
402
     *
403
     * @return array The bundle option
404
     */
405
    public function loadBundleOptionValue($title, $storeId, $parentId)
406
    {
407
        return $this->getBundleOptionValueRepository()->findOneByNameAndStoreIdAndParentId($title, $storeId, $parentId);
408
    }
409
410
    /**
411
     * Load's the bundle selection value with the passed option/product/parent product ID.
412
     *
413
     * @param integer $optionId        The option ID of the bundle selection to be returned
414
     * @param integer $productId       The product ID of the bundle selection to be returned
415
     * @param integer $parentProductId The parent product ID of the bundle selection to be returned
416
     *
417
     * @return array The bundle selection
418
     */
419
    public function loadBundleSelection($optionId, $productId, $parentProductId)
420
    {
421
        return $this->getBundleSelectionRepository()->findOneByOptionIdAndProductIdAndParentProductId($optionId, $productId, $parentProductId);
422
    }
423
424
    /**
425
     * Load's the bundle selection price with the passed selection/website ID.
426
     *
427
     * @param integer $selectionId The selection ID of the bundle selection price to be returned
428
     * @param integer $websiteId   The website ID of the bundle selection price to be returned
429
     *
430
     * @return array The bundle selection price
431
     */
432
    public function loadBundleSelectionPrice($selectionId, $websiteId)
433
    {
434
        return $this->getBundleSelectionPriceRepository()->findOneByOptionIdAndProductIdAndParentProductId($selectionId, $websiteId);
435
    }
436
437
    /**
438
     * Persist's the passed product bundle option data and return's the ID.
439
     *
440
     * @param array $productBundleOption The product bundle option data to persist
441
     *
442
     * @return string The ID of the persisted entity
443
     */
444
    public function persistProductBundleOption($productBundleOption)
445
    {
446
        return $this->getProductBundleOptionAction()->persist($productBundleOption);
447
    }
448
449
    /**
450
     * Persist's the passed product bundle option value data.
451
     *
452
     * @param array $productBundleOptionValue The product bundle option value data to persist
453
     *
454
     * @return void
455
     */
456
    public function persistProductBundleOptionValue($productBundleOptionValue)
457
    {
458
        $this->getProductBundleOptionValueAction()->persist($productBundleOptionValue);
459
    }
460
461
    /**
462
     * Persist's the passed product bundle selection data and return's the ID.
463
     *
464
     * @param array $productBundleSelection The product bundle selection data to persist
465
     *
466
     * @return string The ID of the persisted entity
467
     */
468
    public function persistProductBundleSelection($productBundleSelection)
469
    {
470
        return $this->getProductBundleSelectionAction()->persist($productBundleSelection);
471
    }
472
473
    /**
474
     * Persist's the passed product bundle selection price data and return's the ID.
475
     *
476
     * @param array $productBundleSelectionPrice The product bundle selection price data to persist
477
     *
478
     * @return void
479
     */
480
    public function persistProductBundleSelectionPrice($productBundleSelectionPrice)
481
    {
482
        return $this->getProductBundleSelectionPriceAction()->persist($productBundleSelectionPrice);
483
    }
484
}
485