Completed
Push — master ( a3ab00...b679e8 )
by Tim
12s
created

BundleSubject::loadBundleOption()   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 3
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Bundle\Subjects\BundleSubject
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-bundle
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Bundle\Subjects;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Product\Subjects\AbstractProductSubject;
25
26
/**
27
 * A SLSB that handles the process to import product variants.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-product-bundle
33
 * @link      http://www.techdivision.com
34
 */
35
class BundleSubject extends AbstractProductSubject
36
{
37
38
    /**
39
     * The value for the price type 'fixed'.
40
     *
41
     * @var integer
42
     */
43
    const PRICE_TYPE_FIXED = 0;
44
45
    /**
46
     * The value for the price type 'percent'.
47
     *
48
     * @var integer
49
     */
50
    const PRICE_TYPE_PERCENT = 1;
51
52
    /**
53
     * The mapping for the SKUs to the created entity IDs.
54
     *
55
     * @var array
56
     */
57
    protected $skuEntityIdMapping = array();
58
59
    /**
60
     * The option name => option ID mapping.
61
     *
62
     * @var array
63
     */
64
    protected $nameOptionIdMapping = array();
65
66
    /**
67
     * The ID of the last created selection.
68
     *
69
     * @var integer
70
     */
71
    protected $childSkuSelectionIdMapping = array();
72
73
    /**
74
     * The position counter, if no position for the bundle selection has been specified.
75
     *
76
     * @var integer
77
     */
78
    protected $positionCounter = 1;
79
80
    /**
81
     * The mapping for the price type.
82
     *
83
     * @var array
84
     */
85
    protected $priceTypeMapping = array(
86
        'fixed'   => BundleSubject::PRICE_TYPE_FIXED,
87
        'percent' => BundleSubject::PRICE_TYPE_PERCENT
88
    );
89
90
    /**
91
     * Intializes the previously loaded global data for exactly one variants.
92
     *
93
     * @param string $serial The serial of the actual import
94
     *
95
     * @return void
96
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
97
     */
98
    public function setUp($serial)
99
    {
100
101
        // invoke the 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($serial);
109
110
        // load the attribute set we've prepared intially
111
        $this->skuEntityIdMapping = $status[RegistryKeys::SKU_ENTITY_ID_MAPPING];
112
    }
113
114
    /**
115
     * Reset the position counter to 1.
116
     *
117
     * @return void
118
     */
119
    public function resetPositionCounter()
120
    {
121
        $this->positionCounter = 1;
122
    }
123
124
    /**
125
     * Returns the acutal value of the position counter and raise's it by one.
126
     *
127
     * @return integer The actual value of the position counter
128
     */
129
    public function raisePositionCounter()
130
    {
131
        return $this->positionCounter++;
132
    }
133
134
    /**
135
     * Save's the mapping of the child SKU and the selection ID.
136
     *
137
     * @param string  $childSku    The child SKU of the selection
138
     * @param integer $selectionId The selection ID to save
139
     *
140
     * @return void
141
     */
142
    public function addChildSkuSelectionIdMapping($childSku, $selectionId)
143
    {
144
        $this->childSkuSelectionIdMapping[$childSku] = $selectionId;
145
    }
146
147
    /**
148
     * Return's the selection ID for the passed child SKU.
149
     *
150
     * @param string $childSku The child SKU to return the selection ID for
151
     *
152
     * @return integer The last created selection ID
153
     * @throws \Exception Is thrown if the SKU is not mapped yet
154
     */
155
    public function getChildSkuSelectionMapping($childSku)
156
    {
157
158
        // query whether or not a child SKU selection ID mapping is available
159
        if (isset($this->childSkuSelectionIdMapping[$childSku])) {
160
            return $this->childSkuSelectionIdMapping[$childSku];
161
        }
162
163
        // throw an exception if the SKU has not been mapped yet
164
        throw new \Exception(
165
            $this->appendExceptionSuffix(
166
                sprintf('Found not mapped selection ID mapping for SKU %s', $childSku)
167
            )
168
        );
169
    }
170
171
    /**
172
     * Return the entity ID for the passed SKU.
173
     *
174
     * @param string $sku The SKU to return the entity ID for
175
     *
176
     * @return integer The mapped entity ID
177
     * @throws \Exception Is thrown if the SKU is not mapped yet
178
     */
179
    public function mapSkuToEntityId($sku)
180
    {
181
182
        // query weather or not the SKU has been mapped
183
        if (isset($this->skuEntityIdMapping[$sku])) {
184
            return $this->skuEntityIdMapping[$sku];
185
        }
186
187
        // throw an exception if the SKU has not been mapped yet
188
        throw new \Exception(
189
            $this->appendExceptionSuffix(
190
                sprintf('Found not mapped entity ID mapping for SKU %s', $sku)
191
            )
192
        );
193
    }
194
195
    /**
196
     * Return's the mapping for the passed price type.
197
     *
198
     * @param string $priceType The price type to map
199
     *
200
     * @return integer The mapped price type
201
     * @throws \Exception Is thrown, if the passed price type can't be mapped
202
     */
203
    public function mapPriceType($priceType)
204
    {
205
206
        // query whether or not the passed price type is available
207
        if (isset($this->priceTypeMapping[$priceType])) {
208
            return $this->priceTypeMapping[$priceType];
209
        }
210
211
        // throw an exception, if not
212
        throw new \Exception(
213
            $this->appendExceptionSuffix(
214
                sprintf('Can\'t find mapping for price type %s', $priceType)
215
            )
216
        );
217
    }
218
219
    /**
220
     * Return's the store for the passed store code.
221
     *
222
     * @param string $storeCode The store code to return the store for
223
     *
224
     * @return array The requested store
225
     * @throws \Exception Is thrown, if the requested store is not available
226
     */
227
    public function getStoreByStoreCode($storeCode)
228
    {
229
230
        // query whether or not the store with the passed store code exists
231
        if (isset($this->stores[$storeCode])) {
232
            return $this->stores[$storeCode];
233
        }
234
235
        // throw an exception, if not
236
        throw new \Exception(
237
            $this->appendExceptionSuffix(
238
                sprintf('Found invalid store code %s', $storeCode)
239
            )
240
        );
241
    }
242
243
    /**
244
     * Return's the option ID for the passed name.
245
     *
246
     * @param string $name The name to return the option ID for
247
     *
248
     * @return integer The option ID for the passed name
249
     * @throws \Exception Is thrown, if no option ID for the passed name is available
250
     */
251
    public function getOptionIdForName($name)
252
    {
253
254
        // query whether or not an option ID for the passed name is available
255
        if (isset($this->nameOptionIdMapping[$name])) {
256
            return $this->nameOptionIdMapping[$name];
257
        }
258
259
        // throw an exception, if not
260
        throw new \Exception(
261
            $this->appendExceptionSuffix(
262
                sprintf('Can\'t find option ID for name %s', $name)
263
            )
264
        );
265
    }
266
267
    /**
268
     * Add's the mapping for the passed name => option ID.
269
     *
270
     * @param string  $name     The name of the option
271
     * @param integer $optionId The created option ID
272
     *
273
     * @return void
274
     */
275
    public function addNameOptionIdMapping($name, $optionId)
276
    {
277
        $this->nameOptionIdMapping[$name] = $optionId;
278
    }
279
280
    /**
281
     * Query whether or not the option with the passed name has already been created.
282
     *
283
     * @param string $name The option name to query for
284
     *
285
     * @return boolean TRUE if the option already exists, else FALSE
286
     */
287
    public function exists($name)
288
    {
289
        return isset($this->nameOptionIdMapping[$name]);
290
    }
291
292
    /**
293
     * Return's the last created option ID.
294
     *
295
     * @return integer $optionId The last created option ID
296
     */
297
    public function getLastOptionId()
298
    {
299
        return end($this->nameOptionIdMapping);
300
    }
301
}
302