Completed
Push — 13.x ( 6ef6a1...0de302 )
by Tim
04:41
created

MediaSubject::getParentImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Media\Subjects\MediaSubject
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\Subjects;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Subjects\FileUploadTrait;
25
use TechDivision\Import\Subjects\FileUploadSubjectInterface;
26
use TechDivision\Import\Product\Subjects\AbstractProductSubject;
27
use TechDivision\Import\Product\Media\Utils\ConfigurationKeys;
28
use TechDivision\Import\Product\Media\Exceptions\MapSkuToEntityIdException;
29
30
/**
31
 * The subject implementation for the product media handling.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-product-media
37
 * @link      http://www.techdivision.com
38
 */
39
class MediaSubject extends AbstractProductSubject implements FileUploadSubjectInterface
40
{
41
42
    /**
43
     * The trait that provides file upload functionality.
44
     *
45
     * @var \TechDivision\Import\Subjects\FileUploadTrait
46
     */
47
    use FileUploadTrait;
48
49
    /**
50
     * The ID of the parent product to relate the variant with.
51
     *
52
     * @var integer
53
     */
54
    protected $parentId;
55
56
    /**
57
     * The value ID of the created media gallery entry.
58
     *
59
     * @var integer
60
     */
61
    protected $parentValueId;
62
63
    /**
64
     * The Magento installation directory.
65
     *
66
     * @var string
67
     */
68
    protected $installationDir;
69
70
    /**
71
     * The position counter, if no position for the product media gallery value has been specified.
72
     *
73
     * @var integer
74
     */
75
    protected $positionCounter = 1;
76
77
    /**
78
     * The available stores.
79
     *
80
     * @var array
81
     */
82
    protected $stores = array();
83
84
    /**
85
     * The mapping for the SKUs to the created entity IDs.
86
     *
87
     * @var array
88
     */
89
    protected $skuEntityIdMapping = array();
90
91
    /**
92
     * Intializes the previously loaded global data for exactly one variants.
93
     *
94
     * @param string $serial The serial of the actual import
95
     *
96
     * @return void
97
     */
98
    public function setUp($serial)
99
    {
100
101
        // invoke parent method
102
        parent::setUp($serial);
103
104
        // load the entity manager and the registry processor
105
        $registryProcessor = $this->getRegistryProcessor();
106
107
        // load the status of the actual import process
108
        $status = $registryProcessor->getAttribute(RegistryKeys::STATUS);
109
110
        // load the attribute set we've prepared intially
111
        $this->skuEntityIdMapping = $status[RegistryKeys::SKU_ENTITY_ID_MAPPING];
112
113
        // initialize media directory => can be absolute or relative
114
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) {
115
            $this->setMediaDir(
116
                $this->resolvePath(
117
                    $this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY)
118
                )
119
            );
120
        }
121
122
        // initialize images directory => can be absolute or relative
123
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) {
124
            $this->setImagesFileDir(
125
                $this->resolvePath(
126
                    $this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)
127
                )
128
            );
129
        }
130
    }
131
132
    /**
133
     * Set's the ID of the parent product to relate the variant with.
134
     *
135
     * @param integer $parentId The ID of the parent product
136
     *
137
     * @return void
138
     */
139
    public function setParentId($parentId)
140
    {
141
        $this->parentId = $parentId;
142
    }
143
144
    /**
145
     * Return's the ID of the parent product to relate the variant with.
146
     *
147
     * @return integer The ID of the parent product
148
     */
149
    public function getParentId()
150
    {
151
        return $this->parentId;
152
    }
153
154
    /**
155
     * Set's the value ID of the created media gallery entry.
156
     *
157
     * @param integer $parentValueId The ID of the created media gallery entry
158
     *
159
     * @return void
160
     */
161
    public function setParentValueId($parentValueId)
162
    {
163
        $this->parentValueId  = $parentValueId;
164
    }
165
166
    /**
167
     * Return's the value ID of the created media gallery entry.
168
     *
169
     * @return integer The ID of the created media gallery entry
170
     */
171
    public function getParentValueId()
172
    {
173
        return $this->parentValueId;
174
    }
175
176
    /**
177
     * Reset the position counter to 1.
178
     *
179
     * @return void
180
     */
181
    public function resetPositionCounter()
182
    {
183
        $this->positionCounter = 0;
184
    }
185
186
    /**
187
     * Returns the acutal value of the position counter and raise's it by one.
188
     *
189
     * @return integer The actual value of the position counter
190
     */
191
    public function raisePositionCounter()
192
    {
193
        return $this->positionCounter++;
194
    }
195
196
    /**
197
     * Return the entity ID for the passed SKU.
198
     *
199
     * @param string $sku The SKU to return the entity ID for
200
     *
201
     * @return integer The mapped entity ID
202
     * @throws \TechDivision\Import\Product\Media\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet
203
     */
204
    public function mapSkuToEntityId($sku)
205
    {
206
207
        // query weather or not the SKU has been mapped
208
        if (isset($this->skuEntityIdMapping[$sku])) {
209
            return $this->skuEntityIdMapping[$sku];
210
        }
211
212
        // throw an exception if the SKU has not been mapped yet
213
        throw new MapSkuToEntityIdException(sprintf('Found not mapped entity ID for SKU %s', $sku));
214
    }
215
216
    /**
217
     * Return's the store for the passed store code.
218
     *
219
     * @param string $storeCode The store code to return the store for
220
     *
221
     * @return array The requested store
222
     * @throws \Exception Is thrown, if the requested store is not available
223
     */
224
    public function getStoreByStoreCode($storeCode)
225
    {
226
227
        // query whether or not the store with the passed store code exists
228
        if (isset($this->stores[$storeCode])) {
229
            return $this->stores[$storeCode];
230
        }
231
232
        // throw an exception, if not
233
        throw new \Exception(sprintf('Found invalid store code %s', $storeCode));
234
    }
235
}
236