Completed
Push — master ( 9c4441...0aa535 )
by Tim
34s
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 media gallery attribute code.
41
     *
42
     * @var string
43
     */
44
    const ATTRIBUTE_CODE = 'media_gallery';
45
46
    /**
47
     * The ID of the parent product the media is related to.
48
     *
49
     * @var integer
50
     */
51
    protected $parentId;
52
53
    /**
54
     * The ID of the persisted media gallery entity.
55
     *
56
     * @var integer
57
     */
58
    protected $valueId;
59
60
    /**
61
     * Process the observer's business logic.
62
     *
63
     * @return array The processed row
64
     */
65
    protected function process()
66
    {
67
68
        // query whether or not, the image changed
69
        if ($this->isParentImage($this->getValue(ColumnKeys::IMAGE_PATH))) {
70
            return;
71
        }
72
73
        try {
74
            // try to load the product SKU and map it the entity ID
75
            $this->parentId = $this->getValue(ColumnKeys::IMAGE_PARENT_SKU, null, array($this, 'mapParentSku'));
76
        } catch (\Exception $e) {
77
            throw $this->wrapException(array(ColumnKeys::IMAGE_PARENT_SKU), $e);
0 ignored issues
show
Documentation introduced by
array(\TechDivision\Impo...Keys::IMAGE_PARENT_SKU) is of type array<integer,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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