Completed
Push — master ( fd00f3...3479b2 )
by Tim
12s
created

VariantSubject::persistProductSuperAttribute()   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\Variant\Subjects\VariantSubject
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-variant
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Variant\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-variant
33
 * @link      http://www.techdivision.com
34
 */
35
class VariantSubject extends AbstractProductSubject
36
{
37
38
    /**
39
     * The ID of the parent product to relate the variant with.
40
     *
41
     * @var integer
42
     */
43
    protected $parentId;
44
45
    /**
46
     * The mapping for the SKUs to the created entity IDs.
47
     *
48
     * @var array
49
     */
50
    protected $skuEntityIdMapping = array();
51
52
    /**
53
     * Intializes the previously loaded global data for exactly one variants.
54
     *
55
     * @param string $serial The serial of the actual import
56
     *
57
     * @return void
58
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
59
     */
60
    public function setUp($serial)
61
    {
62
63
        // invoke parent method
64
        parent::setUp($serial);
65
66
        // load the entity manager and the registry processor
67
        $registryProcessor = $this->getRegistryProcessor();
68
69
        // load the status of the actual import process
70
        $status = $registryProcessor->getAttribute($serial);
71
72
        // load the attribute set we've prepared intially
73
        $this->skuEntityIdMapping = $status[RegistryKeys::SKU_ENTITY_ID_MAPPING];
74
    }
75
76
    /**
77
     * Set's the ID of the parent product to relate the variant with.
78
     *
79
     * @param integer $parentId The ID of the parent product
80
     *
81
     * @return void
82
     */
83
    public function setParentId($parentId)
84
    {
85
        $this->parentId = $parentId;
86
    }
87
88
    /**
89
     * Return's the ID of the parent product to relate the variant with.
90
     *
91
     * @return integer The ID of the parent product
92
     */
93
    public function getParentId()
94
    {
95
        return $this->parentId;
96
    }
97
98
    /**
99
     * Return the entity ID for the passed SKU.
100
     *
101
     * @param string $sku The SKU to return the entity ID for
102
     *
103
     * @return integer The mapped entity ID
104
     * @throws \Exception Is thrown if the SKU is not mapped yet
105
     */
106
    public function mapSkuToEntityId($sku)
107
    {
108
109
        // query weather or not the SKU has been mapped
110
        if (isset($this->skuEntityIdMapping[$sku])) {
111
            return $this->skuEntityIdMapping[$sku];
112
        }
113
114
        // throw an exception if the SKU has not been mapped yet
115
        throw new \Exception(
116
            $this->appendExceptionSuffix(
117
                sprintf('Found not mapped entity ID for SKU %s', $sku)
118
            )
119
        );
120
    }
121
122
    /**
123
     * Return's the store for the passed store code.
124
     *
125
     * @param string $storeCode The store code to return the store for
126
     *
127
     * @return array The requested store
128
     * @throws \Exception Is thrown, if the requested store is not available
129
     */
130
    public function getStoreByStoreCode($storeCode)
131
    {
132
133
        // query whether or not the store with the passed store code exists
134
        if (isset($this->stores[$storeCode])) {
135
            return $this->stores[$storeCode];
136
        }
137
138
        // throw an exception, if not
139
        throw new \Exception(
140
            $this->appendExceptionSuffix(
141
                sprintf('Found invalid store code %s', $storeCode)
142
            )
143
        );
144
    }
145
}
146