Completed
Push — 17.x ( b10fe2...726eb0 )
by Tim
08:00 queued 06:27
created

GenericCompositeEntityMerger::merge()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 12
1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\EntityMergers\GenericCompositeEntityMerger
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 2020 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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Observers\EntityMergers;
22
23
use TechDivision\Import\Observers\ObserverInterface;
24
25
/**
26
 * A composite entity merger implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2020 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class GenericCompositeEntityMerger extends \ArrayObject implements EntityMergerInterface
35
{
36
37
    /**
38
     * Merge's and return's the entity with the passed attributes.
39
     *
40
     * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer instance to detect the state for
41
     * @param array                                            $entity   The entity loaded from the database
42
     * @param array                                            $attr     The entity data from the import file
43
     *
44
     * @return array The entity attributes that has to be merged
45
     * @throws \InvalidArgumentException Is thrown, if one of the elements does not implement the expected interface
46
     */
47
    public function merge(ObserverInterface $observer, array $entity, array $attr) : array
48
    {
49
50
        // invoke the entity mergers
51
        foreach ($this as $key => $entityMerger) {
52
            if ($entityMerger instanceof EntityMergerInterface) {
53
                $attr = $entityMerger->merge($observer, $entity, $attr);
54
            } else {
55
                throw new \InvalidArgumentException(
56
                    sprintf('Element #%d doesn\'t implement expected interface "TechDivision\Import\Observers\EntityMergers\EntityMergerInterface"', $key)
57
                );
58
            }
59
        }
60
61
        // return the entity attributes that has to be merged
62
        return $attr;
63
    }
64
}
65