Completed
Push — 19.x ( a94fdb...51db9b )
by Tim
04:27
created

MediaGalleryUpdateObserver::mergeEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Media\Observers\MediaGalleryUpdateObserver
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\Observers;
22
23
use TechDivision\Import\Product\Media\Utils\MemberNames;
24
25
/**
26
 * Observer that creates/updates the product's media gallery information.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 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-product-media
32
 * @link      http://www.techdivision.com
33
 */
34
class MediaGalleryUpdateObserver extends MediaGalleryObserver
35
{
36
37
    /**
38
     * Merge's and return's the entity with the passed attributes and set's the
39
     * passed status.
40
     *
41
     * @param array       $entity        The entity to merge the attributes into
42
     * @param array       $attr          The attributes to be merged
43
     * @param string|null $changeSetName The change set name to use
44
     *
45
     * @return array The merged entity
46
     */
47
    protected function mergeEntity(array $entity, array $attr, $changeSetName = null)
48
    {
49
50
        // temporary persist the parent value ID
51
        $this->setParentValueId($entity[MemberNames::VALUE_ID]);
52
53
        // merge and return the entity
54
        return parent::mergeEntity($entity, $attr, $changeSetName);
55
    }
56
57
    /**
58
     * Initialize the product media gallery with the passed attributes and returns an instance.
59
     *
60
     * @param array $attr The product media gallery attributes
61
     *
62
     * @return array The initialized product media gallery
63
     */
64
    protected function initializeProductMediaGallery(array $attr)
65
    {
66
67
        // load the value and the attribute ID
68
        $value = $attr[MemberNames::VALUE];
69
        $attributeId = $attr[MemberNames::ATTRIBUTE_ID];
70
71
        // query whether the product media gallery entity already exists or not
72
        if ($entity = $this->loadProductMediaGallery($attributeId, $value)) {
73
            return $this->mergeEntity($entity, $attr);
74
        }
75
76
        // simply return the attributes
77
        return $attr;
78
    }
79
80
    /**
81
     * Initialize the product media gallery value to entity with the passed attributes and returns an instance.
82
     *
83
     * @param array $attr The product media gallery value to entity attributes
84
     *
85
     * @return array|null The initialized product media gallery value to entity, or NULL if the product media gallery value to entity already exists
86
     */
87
    protected function initializeProductMediaGalleryValueToEntity(array $attr)
88
    {
89
90
        // load the value/entity ID
91
        $valueId = $attr[MemberNames::VALUE_ID];
92
        $entityId = $attr[MemberNames::ENTITY_ID];
93
94
        // query whether the product media gallery value to entity entity already exists or not
95
        if ($this->loadProductMediaGalleryValueToEntity($valueId, $entityId)) {
96
            return;
97
        }
98
99
        // simply return the attributes
100
        return $attr;
101
    }
102
103
    /**
104
     * Load's the product media gallery with the passed attribute ID + value.
105
     *
106
     * @param integer $attributeId The attribute ID of the product media gallery to load
107
     * @param string  $value       The value of the product media gallery to load
108
     *
109
     * @return array The product media gallery
110
     */
111
    protected function loadProductMediaGallery($attributeId, $value)
112
    {
113
        return $this->getProductMediaProcessor()->loadProductMediaGallery($attributeId, $value);
114
    }
115
116
    /**
117
     * Load's the product media gallery with the passed value/entity ID.
118
     *
119
     * @param integer $valueId  The value ID of the product media gallery value to entity to load
120
     * @param integer $entityId The entity ID of the product media gallery value to entity to load
121
     *
122
     * @return array The product media gallery
123
     */
124
    protected function loadProductMediaGalleryValueToEntity($valueId, $entityId)
125
    {
126
        return $this->getProductMediaProcessor()->loadProductMediaGalleryValueToEntity($valueId, $entityId);
127
    }
128
}
129