BunchSubject::getLastEntityId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\Subjects\BunchSubject
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2019 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-attribute-set
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Attribute\Set\Subjects;
16
17
use TechDivision\Import\Subjects\AbstractEavSubject;
18
use TechDivision\Import\Attribute\Utils\RegistryKeys;
19
use TechDivision\Import\Attribute\Set\Utils\MemberNames;
20
use TechDivision\Import\Attribute\Set\Utils\ConfigurationKeys;
21
use TechDivision\Import\Subjects\CleanUpColumnsSubjectInterface;
22
23
/**
24
 * The subject implementation that handles the business logic to persist attribute sets.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2019 TechDivision GmbH <[email protected]>
28
 * @license   https://opensource.org/licenses/MIT
29
 * @link      https://github.com/techdivision/import-attribute-set
30
 * @link      http://www.techdivision.com
31
 */
32
class BunchSubject extends AbstractEavSubject implements BunchSubjectInterface, CleanUpColumnsSubjectInterface
33
{
34
35
    /**
36
     * The attribute set that has been processed recently.
37
     *
38
     * @var array
39
     */
40
    protected $lastAttributeSet = array();
41
42
    /**
43
     * The array with the available entity types.
44
     *
45
     * @var array
46
     */
47
    protected $entityTypes = array();
48
49
    /**
50
     * The default entity type code.
51
     *
52
     * @var string
53
     */
54
    protected $defaultEntityTypeCode;
55
56
    /**
57
     * The array with the entity type code + attribute set name => ID mapping.
58
     *
59
     * @var array
60
     */
61
    protected $entityTypeCodeAndAttributeSetNameIdMapping = array();
62
63
    /**
64
     * Intializes the previously loaded global data for exactly one bunch.
65
     *
66
     * @param string $serial The serial of the actual import
67
     *
68
     * @return void
69
     */
70
    public function setUp($serial)
71
    {
72
73
        // load the status of the actual import
74
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
75
76
        // load the global data we've prepared initially
77
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
78
79
        // initialize the default entity type code with the value from the configuration
80
        $this->defaultEntityTypeCode = $this->getEntityTypeCode();
81
82
        // prepare the callbacks
83
        parent::setUp($serial);
84
    }
85
86
    /**
87
     * Returns the default entity type code.
88
     *
89
     * @return string The default entity type code
90
     */
91
    public function getDefaultEntityTypeCode()
92
    {
93
        return $this->defaultEntityTypeCode;
94
    }
95
96
    /**
97
     * Returns the entity type with the passed ID.
98
     *
99
     * @param integer $entityTypeId The ID of the entity type to return
100
     *
101
     * @return array|null The entity type
102
     */
103
    public function getEntityTypeByEntityTypeId($entityTypeId)
104
    {
105
106
        // try to find the entity type with the passed ID and return it, if available
107
        foreach ($this->entityTypes as $entityType) {
108
            if ($entityType[MemberNames::ENTITY_TYPE_ID] === $entityTypeId) {
109
                return $entityType;
110
            }
111
        }
112
    }
113
114
    /**
115
     * Return's the entity type code to be used.
116
     *
117
     * @return string The entity type code to be used
118
     */
119
    public function getEntityTypeCode()
120
    {
121
122
        // initialize the entity type
123
        $entityType = null;
124
125
        // query wether or not we already have an attribute set
126
        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...
127
            $entityType = $this->getEntityTypeByEntityTypeId($this->lastAttributeSet[MemberNames::ENTITY_TYPE_ID]);
128
        }
129
130
        // load the entity type code from the configuration
131
        return  $entityType ? $entityType[MemberNames::ENTITY_TYPE_CODE] : parent::getEntityTypeCode();
132
    }
133
134
    /**
135
     * Return's the entity type for the passed code, of if no entity type code has
136
     * been passed, the default one from the configuration will be used.
137
     *
138
     * @param string|null $entityTypeCode The entity type code
139
     *
140
     * @return array The requested entity type
141
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
142
     */
143
    public function getEntityType($entityTypeCode = null)
144
    {
145
146
        // set the default entity type code, if non has been passed
147
        if ($entityTypeCode === null) {
148
            $entityTypeCode = $this->getDefaultEntityTypeCode();
149
        }
150
151
        // query whether or not, the entity type with the passed code is available
152
        if (isset($this->entityTypes[$entityTypeCode])) {
153
            return $this->entityTypes[$entityTypeCode];
154
        }
155
156
        // throw an exception, if not
157
        throw new \Exception(
158
            sprintf(
159
                'Can\'t find entity type with code %s in file %s on line %d',
160
                $entityTypeCode,
161
                $this->getFilename(),
162
                $this->getLineNumber()
163
            )
164
        );
165
    }
166
167
    /**
168
     * Queries whether or not the attribute set with the passed entity type code and attribute set
169
     * name has already been processed.
170
     *
171
     * @param string $entityTypeCode   The entity type code to check
172
     * @param string $attributeSetName The attribute set name to check
173
     *
174
     * @return boolean TRUE if the attribute set has been processed, else FALSE
175
     */
176
    public function hasBeenProcessed($entityTypeCode, $attributeSetName)
177
    {
178
        return isset($this->entityTypeCodeAndAttributeSetNameIdMapping[$entityTypeCode][$attributeSetName]);
179
    }
180
181
    /**
182
     * Map's the passed entity type code and attribute set name to the attribute set ID that has been created recently.
183
     *
184
     * @param string $entityTypeCode   The entity type code to map
185
     * @param string $attributeSetName The attribute set name to map
186
     *
187
     * @return void
188
     */
189
    public function addEntityTypeCodeAndAttributeSetNameIdMapping($entityTypeCode, $attributeSetName)
190
    {
191
        $this->entityTypeCodeAndAttributeSetNameIdMapping[$entityTypeCode][$attributeSetName] = $this->getLastEntityId();
192
    }
193
194
    /**
195
     * Return's the ID of the attribute set that has been created recently.
196
     *
197
     * @return integer The attribute set ID
198
     * @see \TechDivision\Import\Attribute\Set\Subjects\BunchSubject::getLastAttributeSetId()
199
     */
200
    public function getLastEntityId()
201
    {
202
        return $this->getLastAttributeSetId();
203
    }
204
205
    /**
206
     * Return's the ID of the attribute set that has been created recently.
207
     *
208
     * @return integer The attribute set ID
209
     */
210
    public function getLastAttributeSetId()
211
    {
212
        return $this->lastAttributeSet[MemberNames::ATTRIBUTE_SET_ID];
213
    }
214
215
    /**
216
     * Set's the attribute set that has been created recently.
217
     *
218
     * @param array $lastAttributeSet The attribute set
219
     *
220
     * @return void
221
     */
222
    public function setLastAttributeSet(array $lastAttributeSet)
223
    {
224
        $this->lastAttributeSet = $lastAttributeSet;
225
    }
226
227
    /**
228
     * Return's the attribute set that has been created recently.
229
     *
230
     * @return array The attribute set
231
     */
232
    public function getLastAttributeSet()
233
    {
234
        return $this->lastAttributeSet;
235
    }
236
237
    /**
238
     * Merge the columns from the configuration with all image type columns to define which
239
     * columns should be cleaned-up.
240
     *
241
     * @return array The columns that has to be cleaned-up
242
     */
243
    public function getCleanUpColumns()
244
    {
245
246
        // initialize the array for the columns that has to be cleaned-up
247
        $cleanUpColumns = array();
248
249
        // query whether or not an array has been specified in the configuration
250
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS)) {
251
            $cleanUpColumns = $this->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS);
252
        }
253
254
        // return the array with the column names
255
        return $cleanUpColumns;
256
    }
257
}
258