Completed
Pull Request — master (#15)
by Tim
01:50
created

BunchSubject::loadAttributeByAttributeCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Subjects\BunchSubject
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-attribute
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Subjects;
22
23
use TechDivision\Import\Subjects\ExportableTrait;
24
use TechDivision\Import\Attribute\Utils\MemberNames;
25
use TechDivision\Import\Attribute\Utils\RegistryKeys;
26
use TechDivision\Import\Subjects\ExportableSubjectInterface;
27
28
/**
29
 * The subject implementation that handles the business logic to persist attributes.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-attribute
35
 * @link      http://www.techdivision.com
36
 */
37
class BunchSubject extends AbstractAttributeSubject implements ExportableSubjectInterface
38
{
39
40
    /**
41
     * The trait that implements the export functionality.
42
     *
43
     * @var \TechDivision\Import\Subjects\ExportableTrait;
44
     */
45
    use ExportableTrait;
46
47
    /**
48
     * The ID of the attribute that has been created recently.
49
     *
50
     * @var integer
51
     */
52
    protected $lastAttributeId;
53
54
    /**
55
     * The array with the available attribute sets.
56
     *
57
     * @var array
58
     */
59
    protected $attributeSets = array();
60
61
    /**
62
     * The array with the available attribute groups.
63
     *
64
     * @var array
65
     */
66
    protected $attributeGroups = array();
67
68
    /**
69
     * The array with the pre-loaded attribute IDs.
70
     *
71
     * @var array
72
     */
73
    protected $preLoadedAttributeIds = array();
74
75
    /**
76
     * The attribute code => attribute ID mapping.
77
     *
78
     * @var array
79
     */
80
    protected $attributeCodeIdMapping = array();
81
82
    /**
83
     * Intializes the previously loaded global data for exactly one bunch.
84
     *
85
     * @param string $serial The serial of the actual import
86
     *
87
     * @return void
88
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
89
     */
90
    public function setUp($serial)
91
    {
92
93
        // load the status of the actual import
94
        $status = $this->getRegistryProcessor()->getAttribute($serial);
95
96
        // load the global data we've prepared initially
97
        $this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS];
98
        $this->attributeGroups = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_GROUPS];
99
100
        // prepare the callbacks
101
        parent::setUp($serial);
102
    }
103
104
    /**
105
     * Clean up the global data after importing the bunch.
106
     *
107
     * @param string $serial The serial of the actual import
108
     *
109
     * @return void
110
     */
111
    public function tearDown($serial)
112
    {
113
114
        // invoke the parent method
115
        parent::tearDown($serial);
116
117
        // load the registry processor
118
        $registryProcessor = $this->getRegistryProcessor();
119
120
        // update the status
121
        $registryProcessor->mergeAttributesRecursive(
122
            $serial,
123
            array(
124
                RegistryKeys::PRE_LOADED_ATTRIBUTE_IDS => $this->preLoadedAttributeIds,
125
            )
126
        );
127
    }
128
129
    /**
130
     * Return's the attribute set with the passed attribute set name.
131
     *
132
     * @param string $attributeSetName The name of the requested attribute set
133
     * @param string $entityTypeCode   The entity type code of the requested attribute set
134
     *
135
     * @return array The EAV attribute set
136
     * @throws \Exception Is thrown, if the attribute set with the passed name is not available
137
     */
138
    public function getAttributeSetByAttributeSetNameAndEntityTypeCode($attributeSetName, $entityTypeCode)
139
    {
140
141
        // query whether or not attribute sets for the actual entity type code are available
142
        if (isset($this->attributeSets[$entityTypeCode])) {
143
            // load the attribute sets for the actualy entity type code
144
            $attributSets = $this->attributeSets[$entityTypeCode];
145
146
            // query whether or not, the requested attribute set is available
147
            if (isset($attributSets[$attributeSetName])) {
148
                return $attributSets[$attributeSetName];
149
            }
150
        }
151
152
        // throw an exception, if not
153
        throw new \Exception(
154
            sprintf(
155
                'Found invalid attribute set name %s in file %s on line %d',
156
                $attributeSetName,
157
                $this->getFilename(),
158
                $this->getLineNumber()
159
            )
160
        );
161
    }
162
163
    /**
164
     * Return's the attribute group with the passed attribute set/group name.
165
     *
166
     * @param string $entityTypeCode     The entity type code of the requested attribute group
167
     * @param string $attributeSetName   The name of the requested attribute group's attribute set
168
     * @param string $attributeGroupName The name of the requested attribute group
169
     *
170
     * @return array The EAV attribute group
171
     * @throws \Exception Is thrown, if the attribute group with the passed attribute set/group name is not available
172
     */
173
    public function getAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName)
174
    {
175
176
        // query whether or not attribute groups for the actual entity type code are available
177
        if (isset($this->attributeGroups[$entityTypeCode])) {
178
            // query whether or not, the attribute group with the passed set/group name is available
179
            if (isset($this->attributeGroups[$entityTypeCode][$attributeSetName][$attributeGroupName])) {
180
                return $this->attributeGroups[$entityTypeCode][$attributeSetName][$attributeGroupName];
181
            }
182
        }
183
184
        // throw an exception, if not
185
        throw new \Exception(
186
            sprintf(
187
                'Found invalid attribute group with entity type code %s attribute set/group name %s/%s in file %s on line %d',
188
                $entityTypeCode,
189
                $attributeSetName,
190
                $attributeGroupName,
191
                $this->getFilename(),
192
                $this->getLineNumber()
193
            )
194
        );
195
    }
196
197
    /**
198
     * Pre-load and temporary persist the attribute ID for the passed EAV attribute.
199
     *
200
     * @param array $attribute The EAV attribute to pre-load
201
     *
202
     * @return void
203
     */
204
    public function preLoadAttributeId(array $attribute)
205
    {
206
        $this->preLoadedAttributeIds[$attribute[MemberNames::ATTRIBUTE_CODE]]= $attribute[MemberNames::ATTRIBUTE_ID];
207
    }
208
209
    /**
210
     * Return's the ID of the attribute that has been created recently.
211
     *
212
     * @return integer The attribute ID
213
     */
214
    public function getLastEntityId()
215
    {
216
        return $this->getLastAttributeId();
217
    }
218
219
    /**
220
     * Map's the passed attribute code to the attribute ID that has been created recently.
221
     *
222
     * @param string $attributeCode The attribute code that has to be mapped
223
     *
224
     * @return void
225
     */
226
    public function addAttributeCodeIdMapping($attributeCode)
227
    {
228
        $this->attributeCodeIdMapping[$attributeCode] = $this->getLastEntityId();
229
    }
230
231
    /**
232
     * Queries whether or not the attribute with the passed code has already been processed.
233
     *
234
     * @param string $attributeCode The attribute code to check
235
     *
236
     * @return boolean TRUE if the path has been processed, else FALSE
237
     */
238
    public function hasBeenProcessed($attributeCode)
239
    {
240
        return isset($this->attributeCodeIdMapping[$attributeCode]);
241
    }
242
243
    /**
244
     * Set's the ID of the attribute that has been created recently.
245
     *
246
     * @param integer $lastAttributeId The attribute ID
247
     *
248
     * @return void
249
     */
250
    public function setLastAttributeId($lastAttributeId)
251
    {
252
        $this->lastAttributeId = $lastAttributeId;
253
    }
254
255
    /**
256
     * Return's the ID of the attribute that has been created recently.
257
     *
258
     * @return integer The attribute ID
259
     */
260
    public function getLastAttributeId()
261
    {
262
        return $this->lastAttributeId;
263
    }
264
}
265