Passed
Pull Request — master (#1)
by Tim
03:10
created

BunchSubject::setLastAttributeSetId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\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 2019 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-set
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Set\Subjects;
22
23
use TechDivision\Import\Utils\EntityTypeCodes;
24
use TechDivision\Import\Subjects\AbstractEavSubject;
25
use TechDivision\Import\Attribute\Utils\RegistryKeys;
26
use TechDivision\Import\Attribute\Set\Utils\MemberNames;
27
28
/**
29
 * The subject implementation that handles the business logic to persist attribute sets.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2019 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-set
35
 * @link      http://www.techdivision.com
36
 */
37
class BunchSubject extends AbstractEavSubject implements BunchSubjectInterface
38
{
39
40
    /**
41
     * The attribute set that has been processed recently.
42
     *
43
     * @var array
44
     */
45
    protected $lastAttributeSet = array();
46
47
    /**
48
     * The array with the available entity types.
49
     *
50
     * @var array
51
     */
52
    protected $entityTypes = array();
53
54
    /**
55
     * The default entity type code.
56
     *
57
     * @var string
58
     */
59
    protected $defaultEntityTypeCode;
60
61
    /**
62
     * The array with the entity type code + attribute set name => ID mapping.
63
     *
64
     * @var array
65
     */
66
    protected $entityTypeCodeAndAttributeSetNameIdMapping = array();
67
68
    /**
69
     * Mapping for the virtual entity type code to the real Magento 2 EAV entity type code.
70
     *
71
     * @var array
72
     */
73
    protected $entityTypeCodeMappings = array(
74
        EntityTypeCodes::EAV_ATTRIBUTE             => EntityTypeCodes::CATALOG_PRODUCT,
75
        EntityTypeCodes::EAV_ATTRIBUTE_SET         => EntityTypeCodes::CATALOG_PRODUCT,
76
        EntityTypeCodes::CATALOG_PRODUCT           => EntityTypeCodes::CATALOG_PRODUCT,
77
        EntityTypeCodes::CATALOG_CATEGORY          => EntityTypeCodes::CATALOG_CATEGORY,
78
        EntityTypeCodes::CATALOG_PRODUCT_PRICE     => EntityTypeCodes::CATALOG_PRODUCT,
79
        EntityTypeCodes::CATALOG_PRODUCT_INVENTORY => EntityTypeCodes::CATALOG_PRODUCT
80
    );
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
     */
89
    public function setUp($serial)
90
    {
91
92
        // load the status of the actual import
93
        $status = $this->getRegistryProcessor()->getAttribute($serial);
94
95
        // load the global data we've prepared initially
96
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
97
98
        // initialize the default entity type code with the value from the configuration
99
        $this->defaultEntityTypeCode = $this->entityTypeCodeMappings[$this->getConfiguration()->getConfiguration()->getEntityTypeCode()];
100
101
        // prepare the callbacks
102
        parent::setUp($serial);
103
    }
104
105
    /**
106
     * Returns the default entity type code.
107
     *
108
     * @return string The default entity type code
109
     */
110
    public function getDefaultEntityTypeCode()
111
    {
112
        return $this->defaultEntityTypeCode;
113
    }
114
115
    /**
116
     * Returns the entity type with the passed ID.
117
     *
118
     * @param integer $entityTypeId The ID of the entity type to return
119
     *
120
     * @return array|null The entity type
121
     */
122
    public function getEntityTypeByEntityTypeId($entityTypeId)
123
    {
124
125
        // try to find the entity type with the passed ID and return it, if available
126
        foreach ($this->entityTypes as $entityType) {
127
            if ($entityType[MemberNames::ENTITY_TYPE_ID] === $entityTypeId) {
128
                return $entityType;
129
            }
130
        }
131
    }
132
133
    /**
134
     * Return's the entity type code to be used.
135
     *
136
     * @return string The entity type code to be used
137
     */
138
    public function getEntityTypeCode()
139
    {
140
141
        // initialize the entity type
142
        $entityType = null;
143
144
        // query wether or not we already have an attribute set
145
        if ($this->lastAttributeSet) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->lastAttributeSet of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
146
            $entityType = $this->getEntityTypeByEntityTypeId($this->lastAttributeSet[MemberNames::ENTITY_TYPE_ID]);
147
        }
148
149
        // load the entity type code from the configuration
150
        $entityTypeCode =  $entityType ? $entityType[MemberNames::ENTITY_TYPE_CODE] : $this->getConfiguration()->getConfiguration()->getEntityTypeCode();
151
152
        // try to map the entity type code
153
        if (isset($this->entityTypeCodeToAttributeSetMappings[$entityTypeCode])) {
154
            $entityTypeCode = $this->entityTypeCodeToAttributeSetMappings[$entityTypeCode];
155
        }
156
157
        // return the (mapped) entity type code
158
        return $entityTypeCode;
159
    }
160
161
    /**
162
     * Return's the entity type for the passed code, of if no entity type code has
163
     * been passed, the default one from the configuration will be used.
164
     *
165
     * @param string|null $entityTypeCode The entity type code
166
     *
167
     * @return array The requested entity type
168
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
169
     */
170
    public function getEntityType($entityTypeCode = null)
171
    {
172
173
        // set the default entity type code, if non has been passed
174
        if ($entityTypeCode === null) {
175
            $entityTypeCode = $this->getDefaultEntityTypeCode();
176
        }
177
178
        // query whether or not, the entity type with the passed code is available
179
        if (isset($this->entityTypes[$entityTypeCode])) {
180
            return $this->entityTypes[$entityTypeCode];
181
        }
182
183
        // throw an exception, if not
184
        throw new \Exception(
185
            sprintf(
186
                'Can\'t find entity type with code %s in file %s on line %d',
187
                $entityTypeCode,
188
                $this->getFilename(),
189
                $this->getLineNumber()
190
            )
191
        );
192
    }
193
194
    /**
195
     * Queries whether or not the attribute set with the passed entity type code and attribute set
196
     * name has already been processed.
197
     *
198
     * @param string $entityTypeCode   The entity type code to check
199
     * @param string $attributeSetName The attribute set name to check
200
     *
201
     * @return boolean TRUE if the attribute set has been processed, else FALSE
202
     */
203
    public function hasBeenProcessed($entityTypeCode, $attributeSetName)
204
    {
205
        return isset($this->entityTypeCodeAndAttributeSetNameIdMapping[$entityTypeCode][$attributeSetName]);
206
    }
207
208
    /**
209
     * Map's the passed entity type code and attribute set name to the attribute set ID that has been created recently.
210
     *
211
     * @param string $entityTypeCode   The entity type code to map
212
     * @param string $attributeSetName The attribute set name to map
213
     *
214
     * @return void
215
     */
216
    public function addEntityTypeCodeAndAttributeSetNameIdMapping($entityTypeCode, $attributeSetName)
217
    {
218
        $this->entityTypeCodeAndAttributeSetNameIdMapping[$entityTypeCode][$attributeSetName] = $this->getLastEntityId();
219
    }
220
221
    /**
222
     * Return's the ID of the attribute set that has been created recently.
223
     *
224
     * @return integer The attribute set ID
225
     * @see \TechDivision\Import\Attribute\Set\Subjects\BunchSubject::getLastAttributeSetId()
226
     */
227
    public function getLastEntityId()
228
    {
229
        return $this->getLastAttributeSetId();
230
    }
231
232
    /**
233
     * Return's the ID of the attribute set that has been created recently.
234
     *
235
     * @return integer The attribute set ID
236
     */
237
    public function getLastAttributeSetId()
238
    {
239
        return $this->lastAttributeSet[MemberNames::ATTRIBUTE_SET_ID];
240
    }
241
242
    /**
243
     * Set's the attribute set that has been created recently.
244
     *
245
     * @param array $lastAttributeSet The attribute set
246
     *
247
     * @return void
248
     */
249
    public function setLastAttributeSet(array $lastAttributeSet)
250
    {
251
        $this->lastAttributeSet = $lastAttributeSet;
252
    }
253
254
    /**
255
     * Return's the attribute set that has been created recently.
256
     *
257
     * @return array The attribute set
258
     */
259
    public function getLastAttributeSet()
260
    {
261
        return $this->lastAttributeSet;
262
    }
263
}
264