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

AttributeOptionSwatchObserver::prepareAttributes()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
rs 8.9713
cc 3
eloc 12
nc 2
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\AttributeOptionSwatchObserver
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\Utils\StoreViewCodes;
24
use TechDivision\Import\Attribute\Utils\ColumnKeys;
25
use TechDivision\Import\Attribute\Utils\MemberNames;
26
use TechDivision\Import\Subjects\SubjectInterface;
27
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface;
28
29
/**
30
 * Observer that create's the attribute option swatchs found in the additional CSV file.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2016 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
36
 * @link      http://www.techdivision.com
37
 */
38
class AttributeOptionSwatchObserver extends AbstractAttributeImportObserver
39
{
40
41
    /**
42
     * The attribute processor instance.
43
     *
44
     * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface
45
     */
46
    protected $attributeBunchProcessor;
47
48
    /**
49
     * Initializes the observer with the passed subject instance.
50
     *
51
     * @param \TechDivision\Import\Subjects\SubjectInterface                           $subject                 The observer's subject instance
52
     * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance
53
     */
54
    public function __construct(
55
        SubjectInterface $subject,
56
        AttributeBunchProcessorInterface $attributeBunchProcessor
57
    ) {
58
59
        // pass the subject through to the parend observer
60
        parent::__construct($subject);
61
62
        // initialize the attribute bunch processor
63
        $this->attributeBunchProcessor = $attributeBunchProcessor;
64
    }
65
66
    /**
67
     * Process the observer's business logic.
68
     *
69
     * @return void
70
     */
71
    protected function process()
72
    {
73
74
        // prepare the store view code
75
        $this->prepareStoreViewCode();
76
77
        // prepare and insert the attribute option swatch
78
        if ($attr = $this->prepareAttributes()) {
79
            $this->persistAttributeOptionSwatch($this->initializeAttribute($attr));
80
        }
81
    }
82
83
    /**
84
     * Prepare the attributes of the entity that has to be persisted.
85
     *
86
     * @return array The prepared attributes
87
     */
88
    protected function prepareAttributes()
89
    {
90
91
        // load the option ID
92
        $optionId = $this->getLastOptionId();
93
94
        // load the store ID, value + type
95
        $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
0 ignored issues
show
Bug introduced by
The method getRowStoreId() does not exist on TechDivision\Import\Attr...uteOptionSwatchObserver. Did you maybe mean getRow()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
96
        $value = $this->getValue(ColumnKeys::SWATCH_VALUE);
97
        $type = $this->getValue(ColumnKeys::SWATCH_TYPE);
98
99
        // load the attribute option swatch value/type
100
        if ($value && $type) {
101
            // return the prepared attribute option
102
            return $this->initializeEntity(
103
                array(
104
                    MemberNames::OPTION_ID  => $optionId,
105
                    MemberNames::STORE_ID   => $storeId,
106
                    MemberNames::VALUE      => $value,
107
                    MemberNames::TYPE       => $type
108
                )
109
            );
110
        }
111
    }
112
113
    /**
114
     * Initialize the EAV attribute option value with the passed attributes and returns an instance.
115
     *
116
     * @param array $attr The EAV attribute option value attributes
117
     *
118
     * @return array The initialized EAV attribute option value
119
     */
120
    protected function initializeAttribute(array $attr)
121
    {
122
        return $attr;
123
    }
124
125
    /**
126
     * Return's the attribute bunch processor instance.
127
     *
128
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
129
     */
130
    protected function getAttributeBunchProcessor()
131
    {
132
        return $this->attributeBunchProcessor;
133
    }
134
135
    /**
136
     * Return's the ID of the option that has been created recently.
137
     *
138
     * @return integer The option ID
139
     */
140
    protected function getLastOptionId()
141
    {
142
        return $this->getSubject()->getLastOptionId();
143
    }
144
145
    /**
146
     * Persist the passed attribute option swatch.
147
     *
148
     * @param array $attributeOptionSwatch The attribute option swatch to persist
149
     *
150
     * @return void
151
     */
152
    protected function persistAttributeOptionSwatch(array $attributeOptionSwatch)
153
    {
154
        return $this->getAttributeBunchProcessor()->persistAttributeOptionSwatch($attributeOptionSwatch);
155
    }
156
}
157