Passed
Push — 14.x ( b3445b )
by Tim
04:11
created

BunchSubject::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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