Completed
Pull Request — master (#48)
by Tim
03:15
created

AbstractObserver::getRegistryProcessor()   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 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\EntityStatus;
24
use TechDivision\Import\Utils\ColumnKeys;
25
use TechDivision\Import\Utils\ScopeKeys;
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
     * Queries whether or not debug mode is enabled or not, default is TRUE.
143
     *
144
     * @return boolean TRUE if debug mode is enabled, else FALSE
145
     */
146
    protected function isDebugMode()
147
    {
148
        return $this->getSubject()->isDebugMode();
149
    }
150
151
    /**
152
     * Stop's observer execution on the actual row.
153
     *
154
     * @return void
155
     */
156
    protected function skipRow()
157
    {
158
        $this->getSubject()->skipRow();
159
    }
160
161
    /**
162
     * Return's the name of the file to import.
163
     *
164
     * @return string The filename
165
     */
166
    protected function getFilename()
167
    {
168
        return $this->getSubject()->getFilename();
169
    }
170
171
    /**
172
     * Return's the actual line number.
173
     *
174
     * @return integer The line number
175
     */
176
    protected function getLineNumber()
177
    {
178
        return $this->getSubject()->getLineNumber();
179
    }
180
181
    /**
182
     * Return's the system logger.
183
     *
184
     * @return \Psr\Log\LoggerInterface The system logger instance
185
     */
186
    protected function getSystemLogger()
187
    {
188
        return $this->getSubject()->getSystemLogger();
189
    }
190
191
    /**
192
     * Return's the multiple field delimiter character to use, default value is comma (,).
193
     *
194
     * @return string The multiple field delimiter character
195
     */
196
    protected function getMultipleFieldDelimiter()
197
    {
198
        return $this->getSubject()->getMultipleFieldDelimiter();
199
    }
200
201
    /**
202
     * Queries whether or not the header with the passed name is available.
203
     *
204
     * @param string $name The header name to query
205
     *
206
     * @return boolean TRUE if the header is available, else FALSE
207
     */
208
    protected function hasHeader($name)
209
    {
210
        return $this->getSubject()->hasHeader($name);
211
    }
212
213
    /**
214
     * Return's the header value for the passed name.
215
     *
216
     * @param string $name The name of the header to return the value for
217
     *
218
     * @return mixed The header value
219
     * \InvalidArgumentException Is thrown, if the header with the passed name is NOT available
220
     */
221
    protected function getHeader($name)
222
    {
223
        return $this->getSubject()->getHeader($name);
224
    }
225
226
    /**
227
     * Add's the header with the passed name and position, if not NULL.
228
     *
229
     * @param string $name The header name to add
230
     *
231
     * @return integer The new headers position
232
     */
233
    protected function addHeader($name)
234
    {
235
        return $this->getSubject()->addHeader($name);
236
    }
237
238
    /**
239
     * Return's the ID of the product that has been created recently.
240
     *
241
     * @return string The entity Id
242
     */
243
    protected function getLastEntityId()
244
    {
245
        return $this->getSubject()->getLastEntityId();
246
    }
247
248
    /**
249
     * Return's the source date format to use.
250
     *
251
     * @return string The source date format
252
     */
253
    protected function getSourceDateFormat()
254
    {
255
        return $this->getSubject()->getSourceDateFormat();
256
    }
257
258
    /**
259
     * Cast's the passed value based on the backend type information.
260
     *
261
     * @param string $backendType The backend type to cast to
262
     * @param mixed  $value       The value to be casted
263
     *
264
     * @return mixed The casted value
265
     */
266
    protected function castValueByBackendType($backendType, $value)
267
    {
268
        return $this->getSubject()->castValueByBackendType($backendType, $value);
269
    }
270
271
    /**
272
     * Set's the store view code the create the product/attributes for.
273
     *
274
     * @param string $storeViewCode The store view code
275
     *
276
     * @return void
277
     */
278
    protected function setStoreViewCode($storeViewCode)
279
    {
280
        $this->getSubject()->setStoreViewCode($storeViewCode);
281
    }
282
283
    /**
284
     * Return's the store view code the create the product/attributes for.
285
     *
286
     * @param string|null $default The default value to return, if the store view code has not been set
287
     *
288
     * @return string The store view code
289
     */
290
    protected function getStoreViewCode($default = null)
291
    {
292
        return $this->getSubject()->getStoreViewCode($default);
293
    }
294
295
    /**
296
     * Prepare's the store view code in the subject.
297
     *
298
     * @return void
299
     */
300
    protected function prepareStoreViewCode()
301
    {
302
303
        // re-set the store view code
304
        $this->setStoreViewCode(null);
305
306
        // initialize the store view code
307
        if ($storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE)) {
308
            $this->setStoreViewCode($storeViewCode);
309
        }
310
    }
311
312
    /**
313
     * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'.
314
     * If the passed value is NOT a valid date, NULL will be returned.
315
     *
316
     * @param string|null $value The value to format
317
     *
318
     * @return string The formatted date
319
     */
320
    protected function formatDate($value)
321
    {
322
323
        // create a DateTime instance from the passed value
324
        if ($dateTime = \DateTime::createFromFormat($this->getSourceDateFormat(), $value)) {
325
            return $dateTime->format('Y-m-d H:i:s');
326
        }
327
328
        // return NULL, if the passed value is NOT a valid date
329
        return null;
330
    }
331
332
    /**
333
     * Extracts the elements of the passed value by exploding them
334
     * with the also passed delimiter.
335
     *
336
     * @param string      $value     The value to extract
337
     * @param string|null $delimiter The delimiter used to extrace the elements
338
     *
339
     * @return array The exploded values
340
     */
341
    protected function explode($value, $delimiter = null)
342
    {
343
344
        // load the default multiple field delimiter
345
        if ($delimiter === null) {
346
            $delimiter = $this->getMultipleFieldDelimiter();
347
        }
348
349
        // explode and return the array with the values, by using the delimiter
350
        return explode($delimiter, $value);
351
    }
352
353
    /**
354
     * Query whether or not a value for the column with the passed name exists.
355
     *
356
     * @param string $name The column name to query for a valid value
357
     *
358
     * @return boolean TRUE if the value is set, else FALSE
359
     */
360
    protected function hasValue($name)
361
    {
362
363
        // query whether or not the header is available
364
        if ($this->hasHeader($name)) {
365
            // load the key for the row
366
            $headerValue = $this->getHeader($name);
367
368
            // query whether the rows column has a vaild value
369
            return (isset($this->row[$headerValue]) && $this->row[$headerValue] != '');
370
        }
371
372
        // return FALSE if not
373
        return false;
374
    }
375
376
    /**
377
     * Set the value in the passed column name.
378
     *
379
     * @param string $name  The column name to set the value for
380
     * @param mixed  $value The value to set
381
     *
382
     * @return void
383
     */
384
    protected function setValue($name, $value)
385
    {
386
        $this->row[$this->getHeader($name)] = $value;
387
    }
388
389
    /**
390
     * Resolve's the value with the passed colum name from the actual row. If a callback will
391
     * be passed, the callback will be invoked with the found value as parameter. If
392
     * the value is NULL or empty, the default value will be returned.
393
     *
394
     * @param string        $name     The name of the column to return the value for
395
     * @param mixed|null    $default  The default value, that has to be returned, if the row's value is empty
396
     * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it
397
     *
398
     * @return mixed|null The, almost formatted, value
399
     */
400
    protected function getValue($name, $default = null, callable $callback = null)
401
    {
402
403
        // initialize the value
404
        $value = null;
405
406
        // query whether or not the header is available
407
        if ($this->hasHeader($name)) {
408
            // load the header value
409
            $headerValue = $this->getHeader($name);
410
            // query wheter or not, the value with the requested key is available
411
            if ((isset($this->row[$headerValue]) && $this->row[$headerValue] != '')) {
412
                $value = $this->row[$headerValue];
413
            }
414
        }
415
416
        // query whether or not, a callback has been passed
417
        if ($value != null && is_callable($callback)) {
418
            $value = call_user_func($callback, $value);
419
        }
420
421
        // query whether or not
422
        if ($value == null && $default !== null) {
423
            $value = $default;
424
        }
425
426
        // return the value
427
        return $value;
428
    }
429
430
    /**
431
     * Return's the Magento configuration value.
432
     *
433
     * @param string  $path    The Magento path of the requested configuration value
434
     * @param mixed   $default The default value that has to be returned, if the requested configuration value is not set
435
     * @param string  $scope   The scope the configuration value has been set
436
     * @param integer $scopeId The scope ID the configuration value has been set
437
     *
438
     * @return mixed The configuration value
439
     * @throws \Exception Is thrown, if nor a value can be found or a default value has been passed
440
     */
441
    protected function getCoreConfigData($path, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0)
442
    {
443
        return $this->getSubject()->getCoreConfigData($path, $default, $scope, $scopeId);
444
    }
445
446
    /**
447
     * Initialize's and return's a new entity with the status 'create'.
448
     *
449
     * @param array $attr The attributes to merge into the new entity
450
     *
451
     * @return array The initialized entity
452
     */
453
    protected function initializeEntity(array $attr = array())
454
    {
455
        return array_merge(array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_CREATE), $attr);
456
    }
457
458
    /**
459
     * Merge's and return's the entity with the passed attributes and set's the
460
     * status to 'update'.
461
     *
462
     * @param array $entity The entity to merge the attributes into
463
     * @param array $attr   The attributes to be merged
464
     *
465
     * @return array The merged entity
466
     */
467
    protected function mergeEntity(array $entity, array $attr)
468
    {
469
        return array_merge($entity, $attr, array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE));
470
    }
471
}
472