Completed
Pull Request — master (#5)
by Tim
04:37
created

MediaGalleryObserver::persistProductMediaGallery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Media\Observers\MediaGalleryObserver
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\ColumnKeys;
24
use TechDivision\Import\Product\Media\Utils\MemberNames;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * Observer that creates/updates the product's media gallery information.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product-media
34
 * @link      http://www.techdivision.com
35
 */
36
class MediaGalleryObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * The ID of the parent product the media is related to.
41
     *
42
     * @var integer
43
     */
44
    protected $parentId;
45
46
    /**
47
     * The ID of the persisted media gallery entity.
48
     *
49
     * @var integer
50
     */
51
    protected $valueId;
52
53
    /**
54
     * Process the observer's business logic.
55
     *
56
     * @return array The processed row
57
     */
58
    protected function process()
59
    {
60
61
        // query whether or not, the image changed
62
        if ($this->isParentImage($this->getValue(ColumnKeys::IMAGE_PATH))) {
63
            return;
64
        }
65
66
        // load the product SKU and map it the entity ID
67
        $this->parentId = $this->getValue(ColumnKeys::IMAGE_PARENT_SKU, null, array($this, 'mapParentSku'));
68
69
        // reset the position counter for the product media gallery value
70
        $this->resetPositionCounter();
71
72
        // initialize and persist the product media gallery
73
        $productMediaGallery = $this->initializeProductMediaGallery($this->prepareProductMediaGalleryAttributes());
74
        $this->valueId = $this->persistProductMediaGallery($productMediaGallery);
0 ignored issues
show
Documentation Bug introduced by
The property $valueId was declared of type integer, but $this->persistProductMed...y($productMediaGallery) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
75
76
        // persist the product media gallery to entity data
77
        if ($productMediaGalleryValueToEntity = $this->initializeProductMediaGalleryValueToEntity($this->prepareProductMediaGalleryValueToEntityAttributes())) {
78
            $this->persistProductMediaGalleryValueToEntity($productMediaGalleryValueToEntity);
79
        }
80
81
        // temporarily persist parent/value ID
82
        $this->setParentId($this->parentId);
83
        $this->setParentValueId($this->valueId);
84
    }
85
86
    /**
87
     * Prepare the product media gallery that has to be persisted.
88
     *
89
     * @return array The prepared product media gallery attributes
90
     */
91
    protected function prepareProductMediaGalleryAttributes()
92
    {
93
94
        // initialize the gallery data
95
        $disabled = 0;
96
        $attributeId = 90;
97
        $mediaType = 'image';
98
        $image = $this->getValue(ColumnKeys::IMAGE_PATH_NEW);
99
100
        // initialize and return the entity
101
        return $this->initializeEntity(
102
            array(
103
                MemberNames::ATTRIBUTE_ID => $attributeId,
104
                MemberNames::VALUE        => $image,
105
                MemberNames::MEDIA_TYPE   => $mediaType,
106
                MemberNames::DISABLED     => $disabled
107
            )
108
        );
109
    }
110
111
    /**
112
     * Prepare the product media gallery value to entity that has to be persisted.
113
     *
114
     * @return array The prepared product media gallery value to entity attributes
115
     */
116
    protected function prepareProductMediaGalleryValueToEntityAttributes()
117
    {
118
119
        // initialize and return the entity
120
        return $this->initializeEntity(
121
            array(
122
                MemberNames::VALUE_ID  => $this->valueId,
123
                MemberNames::ENTITY_ID => $this->parentId
124
            )
125
        );
126
    }
127
128
    /**
129
     * Initialize the product media gallery with the passed attributes and returns an instance.
130
     *
131
     * @param array $attr The product media gallery attributes
132
     *
133
     * @return array The initialized product media gallery
134
     */
135
    protected function initializeProductMediaGallery(array $attr)
136
    {
137
        return $attr;
138
    }
139
140
    /**
141
     * Initialize the product media gallery value to entity with the passed attributes and returns an instance.
142
     *
143
     * @param array $attr The product media gallery value to entity attributes
144
     *
145
     * @return array|null The initialized product media gallery value to entity, or NULL if the product media gallery value to entity already exists
146
     */
147
    protected function initializeProductMediaGalleryValueToEntity(array $attr)
148
    {
149
        return $attr;
150
    }
151
152
    /**
153
     * Map's the passed SKU of the parent product to it's PK.
154
     *
155
     * @param string $parentSku The SKU of the parent product
156
     *
157
     * @return integer The primary key used to create relations
158
     */
159
    protected function mapParentSku($parentSku)
160
    {
161
        return $this->mapSkuToEntityId($parentSku);
162
    }
163
164
    /**
165
     * Return's the name of the created image.
166
     *
167
     * @return string The name of the created image
168
     */
169
    protected function getParentImage()
170
    {
171
        return $this->getSubject()->getParentImage();
172
    }
173
174
    /**
175
     * Return's TRUE if the passed image is the parent one.
176
     *
177
     * @param string $image The imageD to check
178
     *
179
     * @return boolean TRUE if the passed image is the parent one
180
     */
181
    protected function isParentImage($image)
182
    {
183
        return $this->getParentImage() === $image;
184
    }
185
186
    /**
187
     * Set's the value ID of the created media gallery entry.
188
     *
189
     * @param integer $parentValueId The ID of the created media gallery entry
190
     *
191
     * @return void
192
     */
193
    protected function setParentValueId($parentValueId)
194
    {
195
        $this->getSubject()->setParentValueId($parentValueId);
196
    }
197
198
    /**
199
     * Return the entity ID for the passed SKU.
200
     *
201
     * @param string $sku The SKU to return the entity ID for
202
     *
203
     * @return integer The mapped entity ID
204
     * @throws \Exception Is thrown if the SKU is not mapped yet
205
     */
206
    protected function mapSkuToEntityId($sku)
207
    {
208
        return $this->getSubject()->mapSkuToEntityId($sku);
209
    }
210
211
    /**
212
     * Set's the ID of the parent product to relate the variant with.
213
     *
214
     * @param integer $parentId The ID of the parent product
215
     *
216
     * @return void
217
     */
218
    protected function setParentId($parentId)
219
    {
220
        $this->getSubject()->setParentId($parentId);
221
    }
222
223
    /**
224
     * Reset the position counter to 1.
225
     *
226
     * @return void
227
     */
228
    protected function resetPositionCounter()
229
    {
230
        $this->getSubject()->resetPositionCounter();
231
    }
232
233
    /**
234
     * Persist's the passed product media gallery data and return's the ID.
235
     *
236
     * @param array $productMediaGallery The product media gallery data to persist
237
     *
238
     * @return string The ID of the persisted entity
239
     */
240
    protected function persistProductMediaGallery($productMediaGallery)
241
    {
242
        return $this->getSubject()->persistProductMediaGallery($productMediaGallery);
243
    }
244
245
    /**
246
     * Persist's the passed product media gallery value to entity data.
247
     *
248
     * @param array $productMediaGalleryValueToEntity The product media gallery value to entity data to persist
249
     *
250
     * @return void
251
     */
252
    protected function persistProductMediaGalleryValueToEntity($productMediaGalleryValueToEntity)
253
    {
254
        $this->getSubject()->persistProductMediaGalleryValueToEntity($productMediaGalleryValueToEntity);
255
    }
256
}
257