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

AbstractProductImportObserver::getValue()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.105

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 12
cts 14
cp 0.8571
rs 8.439
c 0
b 0
f 0
cc 6
eloc 11
nc 8
nop 3
crap 6.105
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\AbstractProductImportObserver
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\Utils\EntityStatus;
24
use TechDivision\Import\Product\Utils\ColumnKeys;
25
use TechDivision\Import\Observers\AbstractObserver;
26
27
/**
28
 * A SLSB that handles the process to import product bunches.
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
abstract class AbstractProductImportObserver extends AbstractObserver implements ProductImportObserverInterface
37
{
38
39
    /**
40
     * The actual row, that has to be processed.
41
     *
42
     * @var array
43
     */
44
    protected $row = array();
45
46
    /**
47
     * Set's the array containing header row.
48
     *
49
     * @param array $headers The array with the header row
50
     *
51
     * @return void
52
     */
53
    public function setHeaders(array $headers)
54
    {
55
        $this->getSubject()->setHeaders($headers);
56
    }
57
58
    /**
59
     * Return's the array containing header row.
60
     *
61
     * @return array The array with the header row
62
     */
63 7
    public function getHeaders()
64
    {
65 7
        return $this->getSubject()->getHeaders();
66
    }
67
68
    /**
69
     * Return's TRUE if the passed SKU is the actual one.
70
     *
71
     * @param string $sku The SKU to check
72
     *
73
     * @return boolean TRUE if the passed SKU is the actual one
74
     */
75 7
    public function isLastSku($sku)
76
    {
77 7
        return $this->getSubject()->getLastSku() === $sku;
78
    }
79
80
    /**
81
     * Return's the ID of the product that has been created recently.
82
     *
83
     * @return string The entity Id
84
     */
85 3
    public function getLastEntityId()
86
    {
87 3
        return $this->getSubject()->getLastEntityId();
88
    }
89
90
    /**
91
     * Return's the source date format to use.
92
     *
93
     * @return string The source date format
94
     */
95 1
    public function getSourceDateFormat()
96
    {
97 1
        return $this->getSubject()->getSourceDateFormat();
98
    }
99
100
    /**
101
     * Cast's the passed value based on the backend type information.
102
     *
103
     * @param string $backendType The backend type to cast to
104
     * @param mixed  $value       The value to be casted
105
     *
106
     * @return mixed The casted value
107
     */
108
    public function castValueByBackendType($backendType, $value)
109
    {
110
        return $this->getSubject()->castValueByBackendType($backendType, $value);
111
    }
112
113
    /**
114
     * Set's the store view code the create the product/attributes for.
115
     *
116
     * @param string $storeViewCode The store view code
117
     *
118
     * @return void
119
     */
120 3
    public function setStoreViewCode($storeViewCode)
121
    {
122 3
        $this->getSubject()->setStoreViewCode($storeViewCode);
123 3
    }
124
125
    /**
126
     * Return's the store view code the create the product/attributes for.
127
     *
128
     * @param string|null $default The default value to return, if the store view code has not been set
129
     *
130
     * @return string The store view code
131
     */
132
    public function getStoreViewCode($default = null)
133
    {
134
        return $this->getSubject()->getStoreViewCode($default);
135
    }
136
137
    /**
138
     * Prepare's the store view code in the subject.
139
     *
140
     * @return void
141
     */
142 3
    public function prepareStoreViewCode()
143
    {
144
145
        // load the headers
146 3
        $row = $this->getRow();
147 3
        $headers = $this->getHeaders();
148
149
        // initialize the store view code
150 3
        $this->setStoreViewCode(null);
151
152
        // initialize the store view code
153 3
        if (isset($row[$headers[ColumnKeys::STORE_VIEW_CODE]])) {
154
            $storeViewCode = $row[$headers[ColumnKeys::STORE_VIEW_CODE]];
155
            if (!empty($storeViewCode)) {
156
                $this->setStoreViewCode($storeViewCode);
157
            }
158
        }
159 3
    }
160
161
    /**
162
     * Set's the actual row, that has to be processed.
163
     *
164
     * @param array $row The row
165
     *
166
     * @return void
167
     */
168 6
    public function setRow(array $row)
169
    {
170 6
        $this->row = $row;
171 6
    }
172
173
    /**
174
     * Return's the actual row, that has to be processed.
175
     *
176
     * @return array The row
177
     */
178 6
    public function getRow()
179
    {
180 6
        return $this->row;
181
    }
182
183
    /**
184
     * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'.
185
     * If the passed value is NOT a valid date, NULL will be returned.
186
     *
187
     * @param string|null $value The value to format
188
     *
189
     * @return string The formatted date
190
     */
191 1
    public function formatDate($value)
192
    {
193
194
        // create a DateTime instance from the passed value
195 1
        if ($dateTime = \DateTime::createFromFormat($this->getSourceDateFormat(), $value)) {
196 1
            return $dateTime->format('Y-m-d H:i:s');
197
        }
198
199
        // return NULL, if the passed value is NOT a valid date
200
        return null;
201
    }
202
203
    /**
204
     * Query whether or not the value with the passed key exists.
205
     *
206
     * @param string $key The key of the value to query
207
     *
208
     * @return boolean TRUE if the value is set, else FALSE
209
     */
210 2
    public function hasValue($key)
211
    {
212
213
        // load row and headers
214 2
        $row = $this->getRow();
215 2
        $headers = $this->getHeaders();
216
217
        // query whether or not the value exists
218 2
        return isset($row[$headers[$key]]);
219
    }
220
221
    /**
222
     * Resolve's the value with the passed key from the actual row. If a callback will
223
     * be passed, the callback will be invoked with the found value as parameter. If
224
     * the value is NULL or empty, the default value will be returned.
225
     *
226
     * @param string        $key      The key of the value to return
227
     * @param mixed|null    $default  The default value, that has to be returned, if the row's value is empty
228
     * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it
229
     *
230
     * @return mixed|null The, almost formatted, value
231
     */
232 6
    public function getValue($key, $default = null, callable $callback = null)
233
    {
234
235
        // load row and headers
236 6
        $row = $this->getRow();
237 6
        $headers = $this->getHeaders();
238
239
        // initialize the value
240 6
        $value = null;
241
242
        // query wheter or not, the value with the requested key is available
243 6
        if (isset($headers[$key]) && isset($row[$headers[$key]])) {
244 6
            $value = $row[$headers[$key]];
245 6
        }
246
247
        // query whether or not, a callback has been passed
248 6
        if (is_callable($callback)) {
249 1
            $value = call_user_func($callback, $value);
250 1
        }
251
252
        // query whether or not
253 6
        if ($value == null && $default != null) {
254
            $value = $default;
255
        }
256
257
        // return the value
258 6
        return $value;
259
    }
260
261
    /**
262
     * Initialize's and return's a new entity with the status 'create'.
263
     *
264
     * @param array $attr The attributes to merge into the new entity
265
     *
266
     * @return array The initialized entity
267
     */
268 4
    public function initializeEntity(array $attr = array())
269
    {
270 4
        return array_merge(array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_CREATE), $attr);
271
    }
272
273
    /**
274
     * Merge's and return's the entity with the passed attributes and set's the
275
     * status to 'update'.
276
     *
277
     * @param array $entity The entity to merge the attributes into
278
     * @param array $attr   The attributes to be merged
279
     *
280
     * @return array The merged entity
281
     */
282 2
    public function mergeEntity(array $entity, array $attr)
283
    {
284 2
        return array_merge($entity, $attr, array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE));
285
    }
286
}
287