Completed
Pull Request — master (#17)
by Tim
03:53
created

ProductObserver::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ProductObserver
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-product
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Observers;
22
23
use TechDivision\Import\Product\Utils\ColumnKeys;
24
use TechDivision\Import\Product\Utils\MemberNames;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * Observer that create's the product itself.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * Will be invoked by the action on the events the listener has been registered for.
41
     *
42
     * @param array $row The row to handle
43
     *
44
     * @return array The modified row
45
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
46
     */
47 1
    public function handle(array $row)
48
    {
49
50
        // initialize the row
51 1
        $this->setRow($row);
52
53
        // process the functionality and return the row
54 1
        $this->process();
55
56
        // return the processed row
57 1
        return $this->getRow();
58
    }
59
60
    /**
61
     * Process the observer's business logic.
62
     *
63
     * @return array The processed row
64
     */
65 1
    protected function process()
66
    {
67
68
        // query whether or not, we've found a new SKU => means we've found a new product
69 1
        if ($this->isLastSku($this->getValue(ColumnKeys::SKU))) {
70
            return $this->getRow();
71
        }
72
73
        // prepare the static entity values
74 1
        $product = $this->initializeProduct($this->prepareAttributes());
75
76
        // insert the entity and set the entity ID
77 1
        $this->setLastEntityId($this->persistProduct($product));
78 1
    }
79
80
    /**
81
     * Prepare the attributes of the entity that has to be persisted.
82
     *
83
     * @return array The prepared attributes
84
     */
85 1
    protected function prepareAttributes()
86
    {
87
88
        // load row and headers
89 1
        $row = $this->getRow();
0 ignored issues
show
Unused Code introduced by
$row is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90 1
        $headers = $this->getHeaders();
0 ignored issues
show
Unused Code introduced by
$headers is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
92
        // prepare the date format for the created at/updated at dates
93 1
        $createdAt = $this->getValue(ColumnKeys::CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
94 1
        $updatedAt = $this->getValue(ColumnKeys::UPDATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
95
96
        // load the product's attribute set ID
97 1
        $attributeSet = $this->getAttributeSetByAttributeSetName($this->getValue(ColumnKeys::ATTRIBUTE_SET_CODE));
98 1
        $attributeSetId = $attributeSet[MemberNames::ATTRIBUTE_SET_ID];
99 1
        $this->setAttributeSet($attributeSet);
100
101
        // initialize the product values
102 1
        $sku = $this->getValue(ColumnKeys::SKU);
103 1
        $productType = $this->getValue(ColumnKeys::PRODUCT_TYPE);
104
105
        // return the prepared product
106 1
        return $this->initializeEntity(
107
            array(
108 1
                MemberNames::SKU              => $sku,
109 1
                MemberNames::CREATED_AT       => $createdAt,
110 1
                MemberNames::UPDATED_AT       => $updatedAt,
111 1
                MemberNames::HAS_OPTIONS      => 0,
112 1
                MemberNames::REQUIRED_OPTIONS => 0,
113 1
                MemberNames::TYPE_ID          => $productType,
114 1
                MemberNames::ATTRIBUTE_SET_ID => $attributeSetId
115
            )
116
        );
117
    }
118
119
    /**
120
     * Initialize the product with the passed attributes and returns an instance.
121
     *
122
     * @param array $attr The product attributes
123
     *
124
     * @return array The initialized product
125
     */
126
    protected function initializeProduct(array $attr)
127
    {
128
        return $attr;
129
    }
130
131
    /**
132
     * Persist's the passed product data and return's the ID.
133
     *
134
     * @param array $product The product data to persist
135
     *
136
     * @return string The ID of the persisted entity
137
     */
138 1
    protected function persistProduct($product)
139
    {
140 1
        return $this->getSubject()->persistProduct($product);
141
    }
142
143
    /**
144
     * Set's the attribute set of the product that has to be created.
145
     *
146
     * @param array $attributeSet The attribute set
147
     *
148
     * @return void
149
     */
150 1
    protected function setAttributeSet(array $attributeSet)
151
    {
152 1
        $this->getSubject()->setAttributeSet($attributeSet);
153 1
    }
154
155
    /**
156
     * Return's the attribute set of the product that has to be created.
157
     *
158
     * @return array The attribute set
159
     */
160
    protected function getAttributeSet()
161
    {
162
        $this->getSubject()->getAttributeSet();
163
    }
164
165
    /**
166
     * Return's the attribute set with the passed attribute set name.
167
     *
168
     * @param string $attributeSetName The name of the requested attribute set
169
     *
170
     * @return array The attribute set data
171
     */
172 1
    protected function getAttributeSetByAttributeSetName($attributeSetName)
173
    {
174 1
        return $this->getSubject()->getAttributeSetByAttributeSetName($attributeSetName);
175
    }
176
177
    /**
178
     * Set's the ID of the product that has been created recently.
179
     *
180
     * @param string $lastEntityId The entity ID
181
     *
182
     * @return void
183
     */
184 1
    protected function setLastEntityId($lastEntityId)
185
    {
186 1
        $this->getSubject()->setLastEntityId($lastEntityId);
187 1
    }
188
}
189