Completed
Pull Request — master (#16)
by Tim
05:11
created

getAttributeSetByAttributeSetName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    public function handle(array $row)
48
    {
49
50
        // load the header information
51
        $headers = $this->getHeaders();
52
53
        // query whether or not, we've found a new SKU => means we've found a new product
54
        if ($this->isLastSku($row[$headers[ColumnKeys::SKU]])) {
55
            return $row;
56
        }
57
58
        // prepare the date format for the created at date
59
        $createdAt = date('Y-m-d H:i:s');
60 View Code Duplication
        if (isset($row[$headers[ColumnKeys::CREATED_AT]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            if ($cda = \DateTime::createFromFormat($this->getSourceDateFormat(), $row[$headers[ColumnKeys::CREATED_AT]])) {
62
                $createdAt = $cda->format('Y-m-d H:i:s');
63
            }
64
        }
65
66
        // prepare the date format for the updated at date
67
        $updatedAt = date('Y-m-d H:i:s');
68 View Code Duplication
        if (isset($row[$headers[ColumnKeys::UPDATED_AT]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            if ($uda = \DateTime::createFromFormat($this->getSourceDateFormat(), $row[$headers[ColumnKeys::UPDATED_AT]])) {
70
                $updatedAt = $uda->format('Y-m-d H:i:s');
71
            }
72
        }
73
74
        // load the product's attribute set
75
        $attributeSet = $this->getAttributeSetByAttributeSetName($row[$headers[ColumnKeys::ATTRIBUTE_SET_CODE]]);
76
77
        // initialize the product values
78
        $sku = $row[$headers[ColumnKeys::SKU]];
79
        $productType = $row[$headers[ColumnKeys::PRODUCT_TYPE]];
80
        $attributeSetId = $attributeSet[MemberNames::ATTRIBUTE_SET_ID];
81
82
        // prepare the static entity values
83
        $params = $this->initializeProduct($sku, $createdAt, $updatedAt, 0, 0, $productType, $attributeSetId);
84
85
        // insert the entity and set the entity ID, SKU and attribute set
86
        $this->setLastEntityId($this->persistProduct($params));
87
        $this->setAttributeSet($attributeSet);
88
89
        // returns the row
90
        return $row;
91
    }
92
93
    /**
94
     * Initialize the product with the passed data and returns an instance.
95
     *
96
     * @param string  $sku             The product's SKU
97
     * @param string  $createdAt       The product's creation date
98
     * @param string  $updatedAt       The product's last update date
99
     * @param integer $hasOptions      Marks the product to has options
100
     * @param integer $requiredOptions Marks the product that some of the options are required
101
     * @param string  $typeId          The product's type ID
102
     * @param integer $attributeSetId  The product's attribute set ID
103
     *
104
     * @return array The initialized product
105
     */
106 2
    public function initializeProduct(
107
        $sku,
108
        $createdAt,
109
        $updatedAt,
110
        $hasOptions,
111
        $requiredOptions,
112
        $typeId,
113
        $attributeSetId
114
    ) {
115
116
        // initialize and return the product
117
        return array(
118 2
            'sku'              => $sku,
119 2
            'created_at'       => $createdAt,
120 2
            'updated_at'       => $updatedAt,
121 2
            'has_options'      => $hasOptions,
122 2
            'required_options' => $requiredOptions,
123 2
            'type_id'          => $typeId,
124
            'attribute_set_id' => $attributeSetId
125 2
        );
126
    }
127
128
    /**
129
     * Persist's the passed product data and return's the ID.
130
     *
131
     * @param array $product The product data to persist
132
     *
133
     * @return string The ID of the persisted entity
134
     */
135
    public function persistProduct($product)
136
    {
137
        return $this->getSubject()->persistProduct($product);
138
    }
139
140
    /**
141
     * Set's the attribute set of the product that has to be created.
142
     *
143
     * @param array $attributeSet The attribute set
144
     *
145
     * @return void
146
     */
147
    public function setAttributeSet(array $attributeSet)
148
    {
149
        $this->getSubject()->setAttributeSet($attributeSet);
150
    }
151
152
    /**
153
     * Return's the attribute set with the passed attribute set name.
154
     *
155
     * @param string $attributeSetName The name of the requested attribute set
156
     *
157
     * @return array The attribute set data
158
     */
159
    public function getAttributeSetByAttributeSetName($attributeSetName)
160
    {
161
        return $this->getSubject()->getAttributeSetByAttributeSetName($attributeSetName);
162
    }
163
164
    /**
165
     * Set's the ID of the product that has been created recently.
166
     *
167
     * @param string $lastEntityId The entity ID
168
     *
169
     * @return void
170
     */
171
    public function setLastEntityId($lastEntityId)
172
    {
173
        $this->getSubject()->setLastEntityId($lastEntityId);
174
    }
175
}
176