Completed
Push — master ( ee291f...d6b81e )
by Tim
11s
created

LinkSubject::loadProductLinkAttributeInt()   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 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Link\Subjects\LinkSubject
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-link
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Link\Subjects;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Product\Link\Utils\MemberNames;
25
use TechDivision\Import\Product\Subjects\AbstractProductSubject;
26
use TechDivision\Import\Product\Link\Exceptions\MapSkuToEntityIdException;
27
use TechDivision\Import\Product\Link\Exceptions\MapLinkTypeCodeToIdException;
28
29
/**
30
 * A subject implementation the process to import product links.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2016 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import-product-link
36
 * @link      http://www.techdivision.com
37
 */
38
class LinkSubject extends AbstractProductSubject
39
{
40
41
    /**
42
     * The temporary persisted last link ID.
43
     *
44
     * @var integer
45
     */
46
    protected $lastLinkId;
47
48
    /**
49
     * The available link types.
50
     *
51
     * @var array
52
     */
53
    protected $linkTypes = array();
54
55
    /**
56
     * The available link attributes.
57
     *
58
     * @var array
59
     */
60
    protected $linkAttributes = array();
61
62
    /**
63
     * The mapping for the SKUs to the created entity IDs.
64
     *
65
     * @var array
66
     */
67
    protected $skuEntityIdMapping = array();
68
69
    /**
70
     * Intializes the previously loaded global data for exactly one variants.
71
     *
72
     * @param string $serial The serial of the actual import
73
     *
74
     * @return void
75
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
76
     */
77
    public function setUp($serial)
78
    {
79
80
        // invoke the parent method
81
        parent::setUp($serial);
82
83
        // load the entity manager and the registry processor
84
        $registryProcessor = $this->getRegistryProcessor();
85
86
        // load the status of the actual import process
87
        $status = $registryProcessor->getAttribute($serial);
88
89
        // load the attribute set we've prepared intially
90
        $this->skuEntityIdMapping = $status[RegistryKeys::SKU_ENTITY_ID_MAPPING];
91
92
        // load the link types/attributes we've initialized before
93
        $this->linkTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::LINK_TYPES];
94
        $this->linkAttributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::LINK_ATTRIBUTES];
95
    }
96
97
    /**
98
     * Temporary persist the last link ID.
99
     *
100
     * @param integer $lastLinkId The last link ID
101
     *
102
     * @return void
103
     */
104
    public function setLastLinkId($lastLinkId)
105
    {
106
        $this->lastLinkId = $lastLinkId;
107
    }
108
109
    /**
110
     * Load the temporary persisted the last link ID.
111
     *
112
     * @return integer The last link ID
113
     */
114
    public function getLastLinkId()
115
    {
116
        return $this->lastLinkId;
117
    }
118
119
    /**
120
     * Return the entity ID for the passed SKU.
121
     *
122
     * @param string $sku The SKU to return the entity ID for
123
     *
124
     * @return integer The mapped entity ID
125
     * @throws \TechDivision\Import\Product\Link\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet
126
     */
127
    public function mapSkuToEntityId($sku)
128
    {
129
130
        // query weather or not the SKU has been mapped
131
        if (isset($this->skuEntityIdMapping[$sku])) {
132
            return $this->skuEntityIdMapping[$sku];
133
        }
134
135
        // throw an exception if the SKU has not been mapped yet
136
        throw new MapSkuToEntityIdException(
137
            $this->appendExceptionSuffix(
138
                sprintf('Found not mapped entity ID for SKU %s', $sku)
139
            )
140
        );
141
    }
142
143
    /**
144
     * Return's the link type ID for the passed link type code.
145
     *
146
     * @param string $linkTypeCode The link type code to return the link type ID for
147
     *
148
     * @return integer The mapped link type ID
149
     * @throws \TechDivision\Import\Product\Link\Exceptions\MapLinkTypeCodeToIdException Is thrown if the link type code is not mapped yet
150
     */
151
    public function mapLinkTypeCodeToLinkTypeId($linkTypeCode)
152
    {
153
154
        // query weather or not the link type code has been mapped
155
        if (isset($this->linkTypes[$linkTypeCode])) {
156
            return $this->linkTypes[$linkTypeCode][MemberNames::LINK_TYPE_ID];
157
        }
158
159
        // throw an exception if the link type code has not been mapped yet
160
        throw new MapLinkTypeCodeToIdException(
161
            $this->appendExceptionSuffix(
162
                sprintf('Found not mapped link type code %s', $linkTypeCode)
163
            )
164
        );
165
    }
166
167
    /**
168
     * Return's the link attribute for the passed link type ID and attribute code.
169
     *
170
     * @param integer $linkTypeId    The link type
171
     * @param string  $attributeCode The attribute code
172
     *
173
     * @return array The link attribute
174
     */
175
    public function getProductLinkAttribute($linkTypeId, $attributeCode)
176
    {
177
178
        // try to load the link attribute with the passed link type ID and attribute code
179
        foreach ($this->linkAttributes as $linkAttribute) {
180
            if ($linkAttribute[MemberNames::LINK_TYPE_ID] === $linkTypeId &&
181
                $linkAttribute[MemberNames::PRODUCT_LINK_ATTRIBUTE_CODE] === $attributeCode
182
            ) {
183
                // return the matching link attribute
184
                return $linkAttribute;
185
            }
186
        }
187
    }
188
}
189