Completed
Pull Request — master (#5)
by Tim
02:21
created

EntityAttributeObserver::prepareAttributes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\EntityAttributeObserver
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\Observers;
22
23
use TechDivision\Import\Attribute\Utils\ColumnKeys;
24
use TechDivision\Import\Attribute\Utils\MemberNames;
25
use TechDivision\Import\Subjects\SubjectInterface;
26
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface;
27
28
/**
29
 * Observer that create's the EAV entity attribute itself.
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 EntityAttributeObserver extends AbstractAttributeImportObserver
38
{
39
40
    /**
41
     * The attribute processor instance.
42
     *
43
     * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface
44
     */
45
    protected $attributeBunchProcessor;
46
47
    /**
48
     * Initializes the observer with the passed subject instance.
49
     *
50
     * @param \TechDivision\Import\Subjects\SubjectInterface                           $subject                 The observer's subject instance
51
     * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance
52
     */
53
    public function __construct(
54
        SubjectInterface $subject,
55
        AttributeBunchProcessorInterface $attributeBunchProcessor
56
    ) {
57
58
        // pass the subject through to the parend observer
59
        parent::__construct($subject);
60
61
        // initialize the attribute bunch processor
62
        $this->attributeBunchProcessor = $attributeBunchProcessor;
63
    }
64
65
    /**
66
     * Process the observer's business logic.
67
     *
68
     * @return void
69
     */
70
    protected function process()
71
    {
72
73
        // query whether or not, we've found a new attribute code => means we've found a new attribute
74
        if ($this->hasBeenProcessed($this->getValue(ColumnKeys::ATTRIBUTE_CODE))) {
75
            return;
76
        }
77
78
        // prepare the EAV entity attribue values
79
        $entityAttribute = $this->initializeAttribute($this->prepareAttributes());
80
81
        // insert the EAV entity attribute
82
        $this->persistEntityAttribute($entityAttribute);
83
    }
84
85
    /**
86
     * Prepare the attributes of the entity that has to be persisted.
87
     *
88
     * @return array The prepared attributes
89
     */
90
    protected function prepareAttributes()
91
    {
92
93
        // load the last attribute ID
94
        $attributeId = $this->getLastAttributeId();
95
96
        // map the entity type code to the ID
97
        $entityType = $this->getEntityType($entityTypeCode = $this->getValue(ColumnKeys::ENTITY_TYPE_CODE));
98
        $entityTypeId = $entityType[MemberNames::ENTITY_TYPE_ID];
99
100
        // load the attribute set ID
101
        $attributeSet = $this->getAttributeSetByAttributeSetNameAndEntityTypeCode($attributeSetName = $this->getValue(ColumnKeys::ATTRIBUTE_SET_NAME), $entityTypeCode);
102
        $attributeSetId = $attributeSet[MemberNames::ATTRIBUTE_SET_ID];
103
104
        // load the attribute group ID
105
        $attributeGroup = $this->getAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_NAME));
106
        $attributeGroupId = $attributeGroup[MemberNames::ATTRIBUTE_GROUP_ID];
107
108
        // return the prepared product
109
        return $this->initializeEntity(
110
            array(
111
                MemberNames::ATTRIBUTE_ID       => $attributeId,
112
                MemberNames::ENTITY_TYPE_ID     => $entityTypeId,
113
                MemberNames::ATTRIBUTE_SET_ID   => $attributeSetId,
114
                MemberNames::ATTRIBUTE_GROUP_ID => $attributeGroupId,
115
                MemberNames::SORT_ORDER         => 0
116
            )
117
        );
118
    }
119
120
    /**
121
     * Initialize the attribute with the passed attributes and returns an instance.
122
     *
123
     * @param array $attr The attribute attributes
124
     *
125
     * @return array The initialized attribute
126
     */
127
    protected function initializeAttribute(array $attr)
128
    {
129
        return $attr;
130
    }
131
132
    /**
133
     * Return's the attribute bunch processor instance.
134
     *
135
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
136
     */
137
    protected function getAttributeBunchProcessor()
138
    {
139
        return $this->attributeBunchProcessor;
140
    }
141
142
    /**
143
     * Queries whether or not the attribute with the passed code has already been processed.
144
     *
145
     * @param string $attributeCode The attribute code to check
146
     *
147
     * @return boolean TRUE if the path has been processed, else FALSE
148
     */
149
    protected function hasBeenProcessed($attributeCode)
150
    {
151
        return $this->getSubject()->hasBeenProcessed($attributeCode);
152
    }
153
154
    /**
155
     * Return's the ID of the attribute that has been created recently.
156
     *
157
     * @return integer The attribute ID
158
     */
159
    protected function getLastAttributeId()
160
    {
161
        return $this->getSubject()->getLastAttributeId();
162
    }
163
164
    /**
165
     * Return's the entity type for the passed code.
166
     *
167
     * @param string $entityTypeCode The entity type code
168
     *
169
     * @return array The requested entity type
170
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
171
     */
172
    protected function getEntityType($entityTypeCode)
173
    {
174
        return $this->getSubject()->getEntityType($entityTypeCode);
175
    }
176
177
    /**
178
     * Return's the attribute set with the passed attribute set name.
179
     *
180
     * @param string $attributeSetName The name of the requested attribute set
181
     * @param string $entityTypeCode   The entity type code of the requested attribute set
182
     *
183
     * @return array The EAV attribute set
184
     * @throws \Exception Is thrown, if the attribute set with the passed name is not available
185
     */
186
    protected function getAttributeSetByAttributeSetNameAndEntityTypeCode($attributeSetName, $entityTypeCode)
187
    {
188
        return $this->getSubject()->getAttributeSetByAttributeSetNameAndEntityTypeCode($attributeSetName, $entityTypeCode);
189
    }
190
191
    /**
192
     * Return's the attribute group with the passed attribute set/group name.
193
     *
194
     * @param string $entityTypeCode     The entity type code of the requested attribute group
195
     * @param string $attributeSetName   The name of the requested attribute group's attribute set
196
     * @param string $attributeGroupName The name of the requested attribute group
197
     *
198
     * @return array The EAV attribute group
199
     * @throws \Exception Is thrown, if the attribute group with the passed attribute set/group name is not available
200
     */
201
    protected function getAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName)
202
    {
203
        return $this->getSubject()->getAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName);
204
    }
205
206
    /**
207
     * Persist the passed entity attribute.
208
     *
209
     * @param array $entityAttribute The entity attribute to persist
210
     *
211
     * @return void
212
     */
213
    protected function persistEntityAttribute(array $entityAttribute)
214
    {
215
        return $this->getAttributeBunchProcessor()->persistEntityAttribute($entityAttribute);
216
    }
217
}
218