Passed
Pull Request — master (#4)
by Tim
04:21
created

AttributeSetBunchProcessor   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 433
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 30
eloc 44
dl 0
loc 433
ccs 0
cts 132
cp 0
rs 10
c 0
b 0
f 0

30 Methods

Rating   Name   Duplication   Size   Complexity  
A getEavAttributeSetAction() 0 3 1
A persistEntityAttribute() 0 3 1
A loadAttributeSetByEntityTypeIdAndAttributeSetName() 0 3 1
A loadAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName() 0 3 1
A setEntityAttributeAction() 0 3 1
A setEavAttributeSetAction() 0 3 1
A persistAttributeSet() 0 3 1
A setEavAttributeGroupAction() 0 3 1
A setConnection() 0 3 1
A __construct() 0 16 1
A beginTransaction() 0 3 1
A setEavAttributeSetRepository() 0 3 1
A loadAttributeGroupByAttributeSetIdAndAttributeGroupCode() 0 3 1
A loadEntityAttributesByAttributeGroupId() 0 3 1
A commit() 0 3 1
A loadAttributeSetByEntityTypeCodeAndAttributeSetName() 0 3 1
A getEavAttributeSetRepository() 0 3 1
A loadAttributeGroupsByAttributeSetId() 0 3 1
A loadEntityAttributesByEntityTypeIdAndAttributeSetName() 0 3 1
A deleteAttributeSet() 0 3 1
A rollBack() 0 3 1
A getEavAttributeGroupAction() 0 3 1
A setEavAttributeGroupRepository() 0 3 1
A getEntityAttributeRepository() 0 3 1
A getEntityAttributeAction() 0 3 1
A getConnection() 0 3 1
A loadEntityAttributeByAttributeIdAndAttributeSetId() 0 3 1
A setEntityAttributeRepository() 0 3 1
A persistAttributeGroup() 0 3 1
A getEavAttributeGroupRepository() 0 3 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Services\AttributeSetBunchProcessor
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 2019 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-attribute-set
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Set\Services;
22
23
use TechDivision\Import\Connection\ConnectionInterface;
24
use TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface;
25
use TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface;
26
use TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface;
27
use TechDivision\Import\Attribute\Set\Repositories\EntityAttributeRepositoryInterface;
28
use TechDivision\Import\Attribute\Set\Repositories\EavAttributeGroupRepositoryInterface;
29
use TechDivision\Import\Attribute\Actions\EntityAttributeActionInterface;
30
31
/**
32
 * The attribute set bunch processor implementation.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2019 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-attribute-set
38
 * @link      http://www.techdivision.com
39
 */
40
class AttributeSetBunchProcessor implements AttributeSetBunchProcessorInterface
41
{
42
43
    /**
44
     * A connection to use.
45
     *
46
     * @var \TechDivision\Import\Connection\ConnectionInterface
47
     */
48
    protected $connection;
49
50
    /**
51
     * The EAV attribute set repository instance.
52
     *
53
     * @var \TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface
54
     */
55
    protected $eavAttributeSetRepository;
56
57
    /**
58
     * The EAV attribute set repository instance.
59
     *
60
     * @var \TechDivision\Import\Attribute\Set\Repositories\EavAttributeGroupRepositoryInterface
61
     */
62
    protected $eavAttributeGroupRepository;
63
64
    /**
65
     * The EAV entity attribute repository instance.
66
     *
67
     * @var \TechDivision\Import\Attribute\Set\Repositories\EntityAttributeRepositoryInterface
68
     */
69
    protected $attributeOptionRepository;
70
71
    /**
72
     * The attribute set action instance.
73
     *
74
     * @var \TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface
75
     */
76
    protected $eavAttributeSetAction;
77
78
    /**
79
     * The attribute group action instance.
80
     *
81
     * @var \TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface
82
     */
83
    protected $eavAttributeGroupAction;
84
85
    /**
86
     * The entity attribute action instance.
87
     *
88
     * @var \TechDivision\Import\Attribute\Actions\EntityAttributeActionInterface
89
     */
90
    protected $entityAttributeAction;
91
92
    /**
93
     * Initialize the processor with the necessary repository and action instances.
94
     *
95
     * @param \TechDivision\Import\Connection\ConnectionInterface                                  $connection                  The connection to use
96
     * @param \TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface                 $eavAttributeSetRepository   The EAV attribute set repository instance
97
     * @param \TechDivision\Import\Attribute\Set\Repositories\EavAttributeGroupRepositoryInterface $eavAttributeGroupRepository The EAV attribute group repository instance
98
     * @param \TechDivision\Import\Attribute\Set\Repositories\EntityAttributeRepositoryInterface   $entityAttributeRepository   The EAV attribute option repository instance
99
     * @param \TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface            $eavAttributeSetAction       The EAV attribute set action instance
100
     * @param \TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface          $eavAttributeGroupAction     The EAV attribute gropu action instance
101
     * @param \TechDivision\Import\Attribute\Actions\EntityAttributeActionInterface                $entityAttributeAction       The entity attribute action instance
102
     */
103
    public function __construct(
104
        ConnectionInterface $connection,
105
        EavAttributeSetRepositoryInterface $eavAttributeSetRepository,
106
        EavAttributeGroupRepositoryInterface $eavAttributeGroupRepository,
107
        EntityAttributeRepositoryInterface $entityAttributeRepository,
108
        EavAttributeSetActionInterface $eavAttributeSetAction,
109
        EavAttributeGroupActionInterface $eavAttributeGroupAction,
110
        EntityAttributeActionInterface $entityAttributeAction
111
    ) {
112
        $this->setConnection($connection);
113
        $this->setEavAttributeSetRepository($eavAttributeSetRepository);
114
        $this->setEavAttributeGroupRepository($eavAttributeGroupRepository);
115
        $this->setEntityAttributeRepository($entityAttributeRepository);
116
        $this->setEavAttributeSetAction($eavAttributeSetAction);
117
        $this->setEavAttributeGroupAction($eavAttributeGroupAction);
118
        $this->setEntityAttributeAction($entityAttributeAction);
119
    }
120
121
    /**
122
     * Set's the passed connection.
123
     *
124
     * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection to set
125
     *
126
     * @return void
127
     */
128
    public function setConnection(ConnectionInterface $connection)
129
    {
130
        $this->connection = $connection;
131
    }
132
133
    /**
134
     * Return's the connection.
135
     *
136
     * @return \TechDivision\Import\Connection\ConnectionInterface The connection instance
137
     */
138
    public function getConnection()
139
    {
140
        return $this->connection;
141
    }
142
143
    /**
144
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
145
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
146
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
147
     * to autocommit mode.
148
     *
149
     * @return boolean Returns TRUE on success or FALSE on failure
150
     * @link http://php.net/manual/en/pdo.begintransaction.php
151
     */
152
    public function beginTransaction()
153
    {
154
        return $this->connection->beginTransaction();
155
    }
156
157
    /**
158
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
159
     * ProductProcessor::beginTransaction() starts a new transaction.
160
     *
161
     * @return boolean Returns TRUE on success or FALSE on failure
162
     * @link http://php.net/manual/en/pdo.commit.php
163
     */
164
    public function commit()
165
    {
166
        return $this->connection->commit();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->connection->commit() returns the type void which is incompatible with the documented return type boolean.
Loading history...
Bug introduced by
Are you sure the usage of $this->connection->commit() targeting TechDivision\Import\Conn...tionInterface::commit() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
167
    }
168
169
    /**
170
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
171
     *
172
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
173
     * rolled back the transaction.
174
     *
175
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
176
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
177
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
178
     *
179
     * @return boolean Returns TRUE on success or FALSE on failure
180
     * @link http://php.net/manual/en/pdo.rollback.php
181
     */
182
    public function rollBack()
183
    {
184
        return $this->connection->rollBack();
185
    }
186
187
    /**
188
     * Set's the attribute set repository instance.
189
     *
190
     * @param \TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface $eavAttributeSetRepository The attribute set repository instance
191
     *
192
     * @return void
193
     */
194
    public function setEavAttributeSetRepository(EavAttributeSetRepositoryInterface $eavAttributeSetRepository)
195
    {
196
        $this->eavAttributeSetRepository = $eavAttributeSetRepository;
197
    }
198
199
    /**
200
     * Return's the attribute set repository instance.
201
     *
202
     * @return \TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface The attribute set repository instance
203
     */
204
    public function getEavAttributeSetRepository()
205
    {
206
        return $this->eavAttributeSetRepository;
207
    }
208
209
    /**
210
     * Set's the attribute group repository instance.
211
     *
212
     * @param \TechDivision\Import\Attribute\Set\Repositories\EavAttributeGroupRepositoryInterface $eavAttributeGroupRepository The attribute group repository instance
213
     *
214
     * @return void
215
     */
216
    public function setEavAttributeGroupRepository(EavAttributeGroupRepositoryInterface $eavAttributeGroupRepository)
217
    {
218
        $this->eavAttributeGroupRepository = $eavAttributeGroupRepository;
219
    }
220
221
    /**
222
     * Return's the attribute group repository instance.
223
     *
224
     * @return \TechDivision\Import\Attribute\Set\Repositories\EavAttributeGroupRepositoryInterface The attribute group repository instance
225
     */
226
    public function getEavAttributeGroupRepository()
227
    {
228
        return $this->eavAttributeGroupRepository;
229
    }
230
231
    /**
232
     * Set's the entity attribute repository instance.
233
     *
234
     * @param \TechDivision\Import\Attribute\Set\Repositories\EntityAttributeRepositoryInterface $entityAttributeRepository The entity attribute repository instance
235
     *
236
     * @return void
237
     */
238
    public function setEntityAttributeRepository(EntityAttributeRepositoryInterface $entityAttributeRepository)
239
    {
240
        $this->entityAttributeRepository = $entityAttributeRepository;
0 ignored issues
show
Bug Best Practice introduced by
The property entityAttributeRepository does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
241
    }
242
243
    /**
244
     * Return's the entity attribute repository instance.
245
     *
246
     * @return \TechDivision\Import\Attribute\Set\Repositories\EntityAttributeRepositoryInterface The entity attribute repository instance
247
     */
248
    public function getEntityAttributeRepository()
249
    {
250
        return $this->entityAttributeRepository;
251
    }
252
253
    /**
254
     * Set's the EAV attribute set action instance.
255
     *
256
     * @param \TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface $eavAttributeSetAction The attribute set action instance
257
     *
258
     * @return void
259
     */
260
    public function setEavAttributeSetAction(EavAttributeSetActionInterface $eavAttributeSetAction)
261
    {
262
        $this->eavAttributeSetAction = $eavAttributeSetAction;
263
    }
264
265
    /**
266
     * Return's the attribute set action instance.
267
     *
268
     * @return \TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface The attribute set action instance
269
     */
270
    public function getEavAttributeSetAction()
271
    {
272
        return $this->eavAttributeSetAction;
273
    }
274
275
    /**
276
     * Set's the EAV attribute group action instance.
277
     *
278
     * @param \TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface $eavAttributeGroupAction The attribute gropu action instance
279
     *
280
     * @return void
281
     */
282
    public function setEavAttributeGroupAction(EavAttributeGroupActionInterface $eavAttributeGroupAction)
283
    {
284
        $this->eavAttributeGroupAction = $eavAttributeGroupAction;
285
    }
286
287
    /**
288
     * Return's the attribute group action instance.
289
     *
290
     * @return \TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface The attribute group action instance
291
     */
292
    public function getEavAttributeGroupAction()
293
    {
294
        return $this->eavAttributeGroupAction;
295
    }
296
297
    /**
298
     * Set's the entity attribute action instance.
299
     *
300
     * @param \TechDivision\Import\Attribute\Actions\EntityAttributeActionInterface $entityAttributeAction The entity attribute action instance
301
     *
302
     * @return void
303
     */
304
    public function setEntityAttributeAction(EntityAttributeActionInterface $entityAttributeAction)
305
    {
306
        $this->entityAttributeAction = $entityAttributeAction;
307
    }
308
309
    /**
310
     * Return's the entity attribute action instance.
311
     *
312
     * @return \TechDivision\Import\Attribute\Actions\EntityAttributeActionInterface The entity attribute action instance
313
     */
314
    public function getEntityAttributeAction()
315
    {
316
        return $this->entityAttributeAction;
317
    }
318
319
    /**
320
     * Load's and return's the EAV attribute set with the passed entity type code and attribute set name.
321
     *
322
     * @param string $entityTypeCode   The entity type code of the EAV attribute set to load
323
     * @param string $attributeSetName The attribute set name of the EAV attribute set to return
324
     *
325
     * @return array The EAV attribute set
326
     */
327
    public function loadAttributeSetByEntityTypeCodeAndAttributeSetName($entityTypeCode, $attributeSetName)
328
    {
329
        return $this->getEavAttributeSetRepository()->findOneByEntityTypeCodeAndAttributeSetName($entityTypeCode, $attributeSetName);
330
    }
331
332
    /**
333
     * Load's and return's the EAV attribute set with the passed entity type ID and attribute set name.
334
     *
335
     * @param string $entityTypeId     The entity type ID of the EAV attribute set to load
336
     * @param string $attributeSetName The attribute set name of the EAV attribute set to return
337
     *
338
     * @return array The EAV attribute set
339
     */
340
    public function loadAttributeSetByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName)
341
    {
342
        return $this->getEavAttributeSetRepository()->findOneByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName);
343
    }
344
345
    /**
346
     * Return's the EAV attribute group with the passed entity type code, attribute set and attribute group name.
347
     *
348
     * @param string $entityTypeCode     The entity type code of the EAV attribute group to return
349
     * @param string $attributeSetName   The attribute set name of the EAV attribute group to return
350
     * @param string $attributeGroupName The attribute group name of the EAV attribute group to return
351
     *
352
     * @return array The EAV attribute group
353
     */
354
    public function loadAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName)
355
    {
356
        return $this->getEavAttributeGroupRepository()->findOneByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName);
357
    }
358
359
    /**
360
     * Returns the EAV entity attributes for the attribute group with the passed ID.
361
     *
362
     * @param integer $attributeGroupId The attribute group ID to load the EAV entity attributes for
363
     *
364
     * @return array|null The EAV attributes with for the passed attribute group ID
365
     */
366
    public function loadEntityAttributesByAttributeGroupId($attributeGroupId)
367
    {
368
        return $this->getEntityAttributeRepository()->findAllByAttributeGroupId($attributeGroupId);
369
    }
370
371
    /**
372
     * Returns the EAV entity attributes for the entity type ID and attribute set with the passed name.
373
     *
374
     * @param integer $entityTypeId     The entity type ID to load the EAV entity attributes for
375
     * @param string  $attributeSetName The attribute set name to return the EAV entity attributes for
376
     *
377
     * @return array|null The EAV entity attributes with for the passed entity type ID and attribute set name
378
     */
379
    public function loadEntityAttributesByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName)
380
    {
381
        return $this->getEntityAttributeRepository()->findAllByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName);
382
    }
383
384
    /**
385
     * Return's the EAV entity attribute with the passed attribute and attribute set ID.
386
     *
387
     * @param integer $attributeId    The ID of the EAV entity attribute's attribute to return
388
     * @param integer $attributeSetId The ID of the EAV entity attribute's attribute set to return
389
     *
390
     * @return array The EAV entity attribute
391
     */
392
    public function loadEntityAttributeByAttributeIdAndAttributeSetId($attributeId, $attributeSetId)
393
    {
394
        return $this->getEntityAttributeRepository()->findOneByAttributeIdAndAttributeSetId($attributeId, $attributeSetId);
395
    }
396
397
    /**
398
     * Return's the attribute groups for the passed attribute set ID, whereas the array
399
     * is prepared with the attribute group names as keys.
400
     *
401
     * @param mixed $attributeSetId The EAV attribute set ID to return the attribute groups for
402
     *
403
     * @return array|boolean The EAV attribute groups for the passed attribute ID
404
     */
405
    public function loadAttributeGroupsByAttributeSetId($attributeSetId)
406
    {
407
        return $this->getEavAttributeGroupRepository()->findAllByAttributeSetId($attributeSetId);
408
    }
409
410
    /**
411
     * Return's the attribute group for the passed attribute set ID and attribute group code.
412
     *
413
     * @param integer $attributeSetId     The EAV attribute set ID to return the attribute group for
414
     * @param string  $attributeGroupCode The EAV attribute group code to load the attribute group for
415
     *
416
     * @return array|boolean The EAV attribute group for the passed attribute set ID and attribute group code
417
     */
418
    public function loadAttributeGroupByAttributeSetIdAndAttributeGroupCode($attributeSetId, $attributeGroupCode)
419
    {
420
        return $this->getEavAttributeGroupRepository()->findOneByAttributeSetIdAndAttributeGroupCode($attributeSetId, $attributeGroupCode);
421
    }
422
423
    /**
424
     * Persist's the passed EAV attribute set data and return's the ID.
425
     *
426
     * @param array       $attributeSet The attribute set data to persist
427
     * @param string|null $name         The name of the prepared statement that has to be executed
428
     *
429
     * @return string The ID of the persisted attribute set
430
     */
431
    public function persistAttributeSet(array $attributeSet, $name = null)
432
    {
433
        return $this->getEavAttributeSetAction()->persist($attributeSet);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getEavAttributeSe...>persist($attributeSet) targeting TechDivision\Import\Acti...ionInterface::persist() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->getEavAttr...>persist($attributeSet) returns the type void which is incompatible with the documented return type string.
Loading history...
434
    }
435
436
    /**
437
     * Persist the passed EAV attribute group.
438
     *
439
     * @param array       $attributeGroup The attribute group to persist
440
     * @param string|null $name           The name of the prepared statement that has to be executed
441
     *
442
     * @return string The ID of the persisted attribute group
443
     */
444
    public function persistAttributeGroup(array $attributeGroup, $name = null)
445
    {
446
        return $this->getEavAttributeGroupAction()->persist($attributeGroup);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getEavAttributeGr...ersist($attributeGroup) targeting TechDivision\Import\Acti...ionInterface::persist() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->getEavAttr...ersist($attributeGroup) returns the type void which is incompatible with the documented return type string.
Loading history...
447
    }
448
449
    /**
450
     * Persist's the passed EAV entity attribute data and return's the ID.
451
     *
452
     * @param array       $entityAttribute The entity attribute data to persist
453
     * @param string|null $name            The name of the prepared statement that has to be executed
454
     *
455
     * @return void
456
     */
457
    public function persistEntityAttribute(array $entityAttribute, $name = null)
458
    {
459
        $this->getEntityAttributeAction()->persist($entityAttribute, $name);
0 ignored issues
show
Unused Code introduced by
The call to TechDivision\Import\Acti...ionInterface::persist() has too many arguments starting with $name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

459
        $this->getEntityAttributeAction()->/** @scrutinizer ignore-call */ persist($entityAttribute, $name);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
460
    }
461
462
    /**
463
     * Delete's the EAV attribute set with the passed attributes.
464
     *
465
     * @param array       $row  The attributes of the EAV attribute group to delete
466
     * @param string|null $name The name of the prepared statement that has to be executed
467
     *
468
     * @return void
469
     */
470
    public function deleteAttributeSet($row, $name = null)
471
    {
472
        $this->getEavAttributeSetAction()->delete($row, $name);
473
    }
474
}
475