Completed
Push — master ( fa31eb...190b3f )
by Tim
9s
created

CatalogAttributeObserver::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\CatalogAttributeObserver
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 catalog 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 CatalogAttributeObserver 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
        // initialize and persist the EAV catalog attribute
79
        $this->persistCatalogAttribute($this->initializeAttribute($this->prepareAttributes()));
80
    }
81
82
    /**
83
     * Prepare the attributes of the entity that has to be persisted.
84
     *
85
     * @return array The prepared attributes
86
     */
87
    protected function prepareAttributes()
88
    {
89
90
        // load the recently created EAV attribute ID
91
        $attributeId = $this->getLastAttributeId();
92
93
        // load the data from the row
94
        $frontendInputRenderer = $this->getValue(ColumnKeys::FRONTEND_INPUT_RENDERER);
95
        $isGlobal = $this->getValue(ColumnKeys::IS_GLOBAL, 1);
96
        $isVisible = $this->getValue(ColumnKeys::IS_VISIBLE, 1);
97
        $isSearchable = $this->getValue(ColumnKeys::IS_SEARCHABLE, 0);
98
        $isFilterable = $this->getValue(ColumnKeys::IS_FILTERABLE, 0);
99
        $isComparable = $this->getValue(ColumnKeys::IS_COMPARABLE, 0);
100
        $isVisibleOnFront = $this->getValue(ColumnKeys::IS_VISIBLE_ON_FRONT, 0);
101
        $isHtmlAllowedOnFront = $this->getValue(ColumnKeys::IS_HTML_ALLOWED_ON_FRONT, 0);
102
        $isUsedForPriceRules = $this->getValue(ColumnKeys::IS_USED_FOR_PRICE_RULES, 0);
103
        $isFilterableInSearch = $this->getValue(ColumnKeys::IS_FILTERABLE_IN_SEARCH, 0);
104
        $usedInProductListing = $this->getValue(ColumnKeys::USED_IN_PRODUCT_LISTING, 0);
105
        $usedForSortBy = $this->getValue(ColumnKeys::USED_FOR_SORT_BY, 0);
106
        $applyTo = $this->getValue(ColumnKeys::APPLY_TO);
107
        $isVisibleInAdvancedSearch = $this->getValue(ColumnKeys::IS_VISIBLE_IN_ADVANCED_SEARCH, 0);
108
        $position = $this->getValue(ColumnKeys::POSITION, 0);
109
        $isWysiwygEnabled = $this->getValue(ColumnKeys::IS_WYSIWYG_ENABLED, 0);
110
        $isUsedForPromoRules = $this->getValue(ColumnKeys::IS_USED_FOR_PROMO_RULES, 0);
111
        $isRequiredInAdminStore = $this->getValue(ColumnKeys::IS_REQUIRED_IN_ADMIN_STORE, 0);
112
        $isUsedInGrid = $this->getValue(ColumnKeys::IS_USED_IN_GRID, 0);
113
        $isVisibleInGrid = $this->getValue(ColumnKeys::IS_VISIBLE_IN_GRID, 0);
114
        $isFilterableInGrid = $this->getValue(ColumnKeys::IS_FILTERABLE_IN_GRID, 0);
115
        $searchWeight = $this->getValue(ColumnKeys::SEARCH_WEIGHT, 1);
116
        $additionalData = $this->getValue(ColumnKeys::ADDITIONAL_DATA);
117
118
        // return the prepared product
119
        return $this->initializeEntity(
120
            array(
121
                MemberNames::ATTRIBUTE_ID                  => $attributeId,
122
                MemberNames::FRONTEND_INPUT_RENDERER       => $frontendInputRenderer,
123
                MemberNames::IS_GLOBAL                     => $isGlobal,
124
                MemberNames::IS_VISIBLE                    => $isVisible,
125
                MemberNames::IS_SEARCHABLE                 => $isSearchable,
126
                MemberNames::IS_FILTERABLE                 => $isFilterable,
127
                MemberNames::IS_COMPARABLE                 => $isComparable,
128
                MemberNames::IS_VISIBLE_ON_FRONT           => $isVisibleOnFront,
129
                MemberNames::IS_HTML_ALLOWED_ON_FRONT      => $isHtmlAllowedOnFront,
130
                MemberNames::IS_USED_FOR_PRICE_RULES       => $isUsedForPriceRules,
131
                MemberNames::IS_FILTERABLE_IN_SEARCH       => $isFilterableInSearch,
132
                MemberNames::USED_IN_PRODUCT_LISTING       => $usedInProductListing,
133
                MemberNames::USED_FOR_SORT_BY              => $usedForSortBy,
134
                MemberNames::APPLY_TO                      => $applyTo,
135
                MemberNames::IS_VISIBLE_IN_ADVANCED_SEARCH => $isVisibleInAdvancedSearch,
136
                MemberNames::POSITION                      => $position,
137
                MemberNames::IS_WYSIWYG_ENABLED            => $isWysiwygEnabled,
138
                MemberNames::IS_USED_FOR_PROMO_RULES       => $isUsedForPromoRules,
139
                MemberNames::IS_REQUIRED_IN_ADMIN_STORE    => $isRequiredInAdminStore,
140
                MemberNames::IS_USED_IN_GRID               => $isUsedInGrid,
141
                MemberNames::IS_VISIBLE_IN_GRID            => $isVisibleInGrid,
142
                MemberNames::IS_FILTERABLE_IN_GRID         => $isFilterableInGrid,
143
                MemberNames::SEARCH_WEIGHT                 => $searchWeight,
144
                MemberNames::ADDITIONAL_DATA               => $additionalData
145
            )
146
        );
147
    }
148
149
    /**
150
     * Initialize the attribute with the passed attributes and returns an instance.
151
     *
152
     * @param array $attr The attribute attributes
153
     *
154
     * @return array The initialized attribute
155
     */
156
    protected function initializeAttribute(array $attr)
157
    {
158
        return $attr;
159
    }
160
161
    /**
162
     * Return's the attribute bunch processor instance.
163
     *
164
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
165
     */
166
    protected function getAttributeBunchProcessor()
167
    {
168
        return $this->attributeBunchProcessor;
169
    }
170
171
    /**
172
     * Persist the passed EAV catalog attribute.
173
     *
174
     * @param array $catalogAttribute The EAV catalog attribute to persist
175
     *
176
     * @return void
177
     */
178
    protected function persistCatalogAttribute(array $catalogAttribute)
179
    {
180
        return $this->getAttributeBunchProcessor()->persistCatalogAttribute($catalogAttribute);
181
    }
182
}
183