Completed
Pull Request — master (#37)
by Tim
03:43
created

AbstractObserver::getHeader()   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 4
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\Observers\AbstractObserver
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Observers;
22
23
use TechDivision\Import\Utils\EntityStatus;
24
use TechDivision\Import\Utils\ColumnKeys;
25
26
/**
27
 * An abstract observer implementation.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
abstract class AbstractObserver implements ObserverInterface
36
{
37
38
    /**
39
     * The actual row, that has to be processed.
40
     *
41
     * @var array
42
     */
43
    protected $row = array();
44
45
    /**
46
     * The obeserver's subject instance.
47
     *
48
     * @var object
49
     */
50
    protected $subject;
51
52
    /**
53
     * Initializes the observer with the passed subject instance.
54
     *
55
     * @param object|null $subject The observer's subject instance
56
     */
57
    public function __construct($subject = null)
58
    {
59
        if ($subject != null) {
60
            $this->setSubject($subject);
61
        }
62
    }
63
64
    /**
65
     * Set's the obeserver's subject instance to initialize the observer with.
66
     *
67
     * @param object $subject The observer's subject
68
     *
69
     * @return void
70
     */
71
    public function setSubject($subject)
72
    {
73
        $this->subject = $subject;
74
    }
75
76
    /**
77
     * Return's the observer's subject instance.
78
     *
79
     * @return object The observer's subject instance
80
     */
81
    public function getSubject()
82
    {
83
        return $this->subject;
84
    }
85
86
    /**
87
     * Set's the array containing header row.
88
     *
89
     * @param array $headers The array with the header row
90
     *
91
     * @return void
92
     */
93
    public function setHeaders(array $headers)
94
    {
95
        $this->getSubject()->setHeaders($headers);
96
    }
97
98
    /**
99
     * Return's the array containing header row.
100
     *
101
     * @return array The array with the header row
102
     */
103
    public function getHeaders()
104
    {
105
        return $this->getSubject()->getHeaders();
106
    }
107
108
    /**
109
     * Set's the actual row, that has to be processed.
110
     *
111
     * @param array $row The row
112
     *
113
     * @return void
114
     */
115
    protected function setRow(array $row)
116
    {
117
        $this->row = $row;
118
    }
119
120
    /**
121
     * Return's the actual row, that has to be processed.
122
     *
123
     * @return array The row
124
     */
125
    protected function getRow()
126
    {
127
        return $this->row;
128
    }
129
130
    /**
131
     * Queries whether or not debug mode is enabled or not, default is TRUE.
132
     *
133
     * @return boolean TRUE if debug mode is enabled, else FALSE
134
     */
135
    protected function isDebugMode()
136
    {
137
        return $this->getSubject()->isDebugMode();
138
    }
139
140
    /**
141
     * Stop's observer execution on the actual row.
142
     *
143
     * @return void
144
     */
145
    protected function skipRow()
146
    {
147
        $this->getSubject()->skipRow();
148
    }
149
150
    /**
151
     * Return's the name of the file to import.
152
     *
153
     * @return string The filename
154
     */
155
    protected function getFilename()
156
    {
157
        return $this->getSubject()->getFilename();
158
    }
159
160
    /**
161
     * Return's the actual line number.
162
     *
163
     * @return integer The line number
164
     */
165
    protected function getLineNumber()
166
    {
167
        return $this->getSubject()->getLineNumber();
168
    }
169
170
    /**
171
     * Return's the system logger.
172
     *
173
     * @return \Psr\Log\LoggerInterface The system logger instance
174
     */
175
    protected function getSystemLogger()
176
    {
177
        return $this->getSubject()->getSystemLogger();
178
    }
179
180
    /**
181
     * Return's the multiple field delimiter character to use, default value is comma (,).
182
     *
183
     * @return string The multiple field delimiter character
184
     */
185
    protected function getMultipleFieldDelimiter()
186
    {
187
        return $this->getSubject()->getMultipleFieldDelimiter();
188
    }
189
190
    /**
191
     * Queries whether or not the header with the passed name is available.
192
     *
193
     * @param string $name The header name to query
194
     *
195
     * @return boolean TRUE if the header is available, else FALSE
196
     */
197
    protected function hasHeader($name)
198
    {
199
        return $this->getSubject()->hasHeader($name);
200
    }
201
202
    /**
203
     * Return's the header value for the passed name.
204
     *
205
     * @param string $name The name of the header to return the value for
206
     *
207
     * @return mixed The header value
208
     * \InvalidArgumentException Is thrown, if the header with the passed name is NOT available
209
     */
210
    protected function getHeader($name)
211
    {
212
        return $this->getSubject()->getHeader($name);
213
    }
214
215
    /**
216
     * Add's the header with the passed name and position, if not NULL.
217
     *
218
     * @param string $name The header name to add
219
     *
220
     * @return integer The new headers position
221
     */
222
    protected function addHeader($name)
223
    {
224
        return $this->getSubject()->addHeader($name);
225
    }
226
227
    /**
228
     * Return's the ID of the product that has been created recently.
229
     *
230
     * @return string The entity Id
231
     */
232
    protected function getLastEntityId()
233
    {
234
        return $this->getSubject()->getLastEntityId();
235
    }
236
237
    /**
238
     * Return's the source date format to use.
239
     *
240
     * @return string The source date format
241
     */
242
    protected function getSourceDateFormat()
243
    {
244
        return $this->getSubject()->getSourceDateFormat();
245
    }
246
247
    /**
248
     * Cast's the passed value based on the backend type information.
249
     *
250
     * @param string $backendType The backend type to cast to
251
     * @param mixed  $value       The value to be casted
252
     *
253
     * @return mixed The casted value
254
     */
255
    protected function castValueByBackendType($backendType, $value)
256
    {
257
        return $this->getSubject()->castValueByBackendType($backendType, $value);
258
    }
259
260
    /**
261
     * Set's the store view code the create the product/attributes for.
262
     *
263
     * @param string $storeViewCode The store view code
264
     *
265
     * @return void
266
     */
267
    protected function setStoreViewCode($storeViewCode)
268
    {
269
        $this->getSubject()->setStoreViewCode($storeViewCode);
270
    }
271
272
    /**
273
     * Return's the store view code the create the product/attributes for.
274
     *
275
     * @param string|null $default The default value to return, if the store view code has not been set
276
     *
277
     * @return string The store view code
278
     */
279
    protected function getStoreViewCode($default = null)
280
    {
281
        return $this->getSubject()->getStoreViewCode($default);
282
    }
283
284
    /**
285
     * Prepare's the store view code in the subject.
286
     *
287
     * @return void
288
     */
289
    protected function prepareStoreViewCode()
290
    {
291
292
        // re-set the store view code
293
        $this->setStoreViewCode(null);
294
295
        // initialize the store view code
296
        if ($storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE)) {
297
            $this->setStoreViewCode($storeViewCode);
298
        }
299
    }
300
301
    /**
302
     * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'.
303
     * If the passed value is NOT a valid date, NULL will be returned.
304
     *
305
     * @param string|null $value The value to format
306
     *
307
     * @return string The formatted date
308
     */
309
    protected function formatDate($value)
310
    {
311
312
        // create a DateTime instance from the passed value
313
        if ($dateTime = \DateTime::createFromFormat($this->getSourceDateFormat(), $value)) {
314
            return $dateTime->format('Y-m-d H:i:s');
315
        }
316
317
        // return NULL, if the passed value is NOT a valid date
318
        return null;
319
    }
320
321
    /**
322
     * Extracts the elements of the passed value by exploding them
323
     * with the also passed delimiter.
324
     *
325
     * @param string      $value     The value to extract
326
     * @param string|null $delimiter The delimiter used to extrace the elements
327
     *
328
     * @return array The exploded values
329
     */
330
    protected function explode($value, $delimiter = null)
331
    {
332
333
        // load the default multiple field delimiter
334
        if ($delimiter === null) {
335
            $delimiter = $this->getMultipleFieldDelimiter();
336
        }
337
338
        // explode and return the array with the values, by using the delimiter
339
        return explode($delimiter, $value);
340
    }
341
342
    /**
343
     * Query whether or not a value for the column with the passed name exists.
344
     *
345
     * @param string $name The column name to query for a valid value
346
     *
347
     * @return boolean TRUE if the value is set, else FALSE
348
     */
349
    protected function hasValue($name)
350
    {
351
352
        // query whether or not the header is available
353
        if ($this->hasHeader($name)) {
354
            // load the key for the row
355
            $headerValue = $this->getHeader($name);
356
357
            // query whether the rows column has a vaild value
358
            return (isset($this->row[$headerValue]) && $this->row[$headerValue] != '');
359
        }
360
361
        // return FALSE if not
362
        return false;
363
    }
364
365
    /**
366
     * Set the value in the passed column name.
367
     *
368
     * @param string $name  The column name to set the value for
369
     * @param mixed  $value The value to set
370
     *
371
     * @return void
372
     */
373
    protected function setValue($name, $value)
374
    {
375
        $this->row[$this->getHeader($name)] = $value;
376
    }
377
378
    /**
379
     * Resolve's the value with the passed colum name from the actual row. If a callback will
380
     * be passed, the callback will be invoked with the found value as parameter. If
381
     * the value is NULL or empty, the default value will be returned.
382
     *
383
     * @param string        $name     The name of the column to return the value for
384
     * @param mixed|null    $default  The default value, that has to be returned, if the row's value is empty
385
     * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it
386
     *
387
     * @return mixed|null The, almost formatted, value
388
     */
389
    protected function getValue($name, $default = null, callable $callback = null)
390
    {
391
392
        // initialize the value
393
        $value = null;
394
395
        // query whether or not the header is available
396
        if ($this->hasHeader($name)) {
397
            // load the header value
398
            $headerValue = $this->getHeader($name);
399
            // query wheter or not, the value with the requested key is available
400
            if ((isset($this->row[$headerValue]) && $this->row[$headerValue] != '')) {
401
                $value = $this->row[$headerValue];
402
            }
403
        }
404
405
        // query whether or not, a callback has been passed
406
        if ($value != null && is_callable($callback)) {
407
            $value = call_user_func($callback, $value);
408
        }
409
410
        // query whether or not
411
        if ($value == null && $default !== null) {
412
            $value = $default;
413
        }
414
415
        // return the value
416
        return $value;
417
    }
418
419
    /**
420
     * Initialize's and return's a new entity with the status 'create'.
421
     *
422
     * @param array $attr The attributes to merge into the new entity
423
     *
424
     * @return array The initialized entity
425
     */
426
    protected function initializeEntity(array $attr = array())
427
    {
428
        return array_merge(array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_CREATE), $attr);
429
    }
430
431
    /**
432
     * Merge's and return's the entity with the passed attributes and set's the
433
     * status to 'update'.
434
     *
435
     * @param array $entity The entity to merge the attributes into
436
     * @param array $attr   The attributes to be merged
437
     *
438
     * @return array The merged entity
439
     */
440
    protected function mergeEntity(array $entity, array $attr)
441
    {
442
        return array_merge($entity, $attr, array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE));
443
    }
444
}
445