Completed
Pull Request — master (#74)
by Tim
04:34
created

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