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

AttributeLabelObserver::prepareAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.3142
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\AttributeLabelObserver
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 EAV attribute label.
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 AttributeLabelObserver 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
        // do nothing, if we're in admin store view
75
        if ($this->isAdminStore()) {
76
            return;
77
        }
78
79
        // prepare the store view code
80
        $this->prepareStoreViewCode();
81
82
        // query whether or not an value for the attribute label is available
83
        if ($attributeLabel = $this->prepareAttributes()) {
84
            // prepare and persist the attribue label
85
            $this->persistAttributeLabel($this->initializeAttribute($attributeLabel));
86
        }
87
    }
88
89
    /**
90
     * Prepare the attributes of the entity that has to be persisted.
91
     *
92
     * @return array The prepared attributes
93
     */
94
    protected function prepareAttributes()
95
    {
96
97
        // load the frontend label value
98
        if ($frontendLabel = $this->getValue(ColumnKeys::FRONTEND_LABEL)) {
99
            // load the last attribute ID
100
            $attributeId = $this->getLastAttributeId();
101
102
            // load the store ID
103
            $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
0 ignored issues
show
Bug introduced by
The method getRowStoreId() does not exist on TechDivision\Import\Attr...\AttributeLabelObserver. 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...
104
105
            // return the prepared attribute label
106
            return $this->initializeEntity(
107
                array(
108
                    MemberNames::ATTRIBUTE_ID  => $attributeId,
109
                    MemberNames::STORE_ID      => $storeId,
110
                    MemberNames::VALUE         => $frontendLabel
111
                )
112
            );
113
        }
114
    }
115
116
    /**
117
     * Initialize the attribute with the passed attributes and returns an instance.
118
     *
119
     * @param array $attr The attribute attributes
120
     *
121
     * @return array The initialized attribute
122
     */
123
    protected function initializeAttribute(array $attr)
124
    {
125
        return $attr;
126
    }
127
128
    /**
129
     * Return's the attribute bunch processor instance.
130
     *
131
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
132
     */
133
    protected function getAttributeBunchProcessor()
134
    {
135
        return $this->attributeBunchProcessor;
136
    }
137
138
    /**
139
     * Return's the ID of the attribute that has been created recently.
140
     *
141
     * @return integer The attribute ID
142
     */
143
    protected function getLastAttributeId()
144
    {
145
        return $this->getSubject()->getLastAttributeId();
146
    }
147
148
    /**
149
     * Persist the passed attribute label.
150
     *
151
     * @param array $attributeLabel The attribute label to persist
152
     *
153
     * @return void
154
     */
155
    protected function persistAttributeLabel(array $attributeLabel)
156
    {
157
        return $this->getAttributeBunchProcessor()->persistAttributeLabel($attributeLabel);
158
    }
159
}
160