Completed
Push — master ( 38c615...0ce8a4 )
by Tim
14s
created

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