Completed
Pull Request — master (#89)
by Tim
03:51
created

AbstractObserver::getFilename()   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 0
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\RowTrait;
24
use TechDivision\Import\Utils\ScopeKeys;
25
use TechDivision\Import\Utils\LoggerKeys;
26
use TechDivision\Import\Utils\EntityStatus;
27
use TechDivision\Import\Subjects\SubjectInterface;
28
29
/**
30
 * An abstract observer implementation.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2016 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import
36
 * @link      http://www.techdivision.com
37
 */
38
abstract class AbstractObserver implements ObserverInterface
39
{
40
41
    /**
42
     * The trait that provides row handling functionality.
43
     *
44
     * @var TechDivision\Import\RowTrait
45
     */
46
    use RowTrait;
47
48
    /**
49
     * The obeserver's subject instance.
50
     *
51
     * @var \TechDivision\Import\Subjects\SubjectInterface
52
     */
53
    protected $subject;
54
55
    /**
56
     * Set's the obeserver's subject instance to initialize the observer with.
57
     *
58
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject
59
     *
60
     * @return void
61
     */
62 8
    protected function setSubject(SubjectInterface $subject)
63
    {
64 8
        $this->subject = $subject;
65 8
    }
66
67
    /**
68
     * Return's the observer's subject instance.
69
     *
70
     * @return object The observer's subject instance
71
     */
72 6
    public function getSubject()
73
    {
74 6
        return $this->subject;
75
    }
76
77
    /**
78
     * Initialize's and return's a new entity with the status 'create'.
79
     *
80
     * @param array $attr The attributes to merge into the new entity
81
     *
82
     * @return array The initialized entity
83
     */
84 2
    protected function initializeEntity(array $attr = array())
85
    {
86 2
        return array_merge(array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_CREATE), $attr);
87
    }
88
89
    /**
90
     * Merge's and return's the entity with the passed attributes and set's the
91
     * status to 'update'.
92
     *
93
     * @param array $entity The entity to merge the attributes into
94
     * @param array $attr   The attributes to be merged
95
     *
96
     * @return array The merged entity
97
     */
98 1
    protected function mergeEntity(array $entity, array $attr)
99
    {
100 1
        return array_merge($entity, $attr, array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE));
101
    }
102
103
    /**
104
     * Set's the array containing header row.
105
     *
106
     * @param array $headers The array with the header row
107
     *
108
     * @return void
109
     * @deprecated Will be removed with version 1.0.0, use subject method instead
110
     *
111
     * @codeCoverageIgnore
112
     */
113
    protected function setHeaders(array $headers)
114
    {
115
        $this->getSubject()->setHeaders($headers);
116
    }
117
118
    /**
119
     * Return's the array containing header row.
120
     *
121
     * @return array The array with the header row
122
     * @deprecated Will be removed with version 1.0.0, use subject method instead
123
     *
124
     * @codeCoverageIgnore
125
     */
126
    protected function getHeaders()
127
    {
128
        return $this->getSubject()->getHeaders();
129
    }
130
131
    /**
132
     * Return's the RegistryProcessor instance to handle the running threads.
133
     *
134
     * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance
135
     * @deprecated Will be removed with version 1.0.0, use subject method instead
136
     *
137
     * @codeCoverageIgnore
138
     */
139
    protected function getRegistryProcessor()
140
    {
141
        return $this->getSubject()->getRegistryProcessor();
142
    }
143
144
    /**
145
     * Append's the exception suffix containing filename and line number to the
146
     * passed message. If no message has been passed, only the suffix will be
147
     * returned
148
     *
149
     * @param string|null $message    The message to append the exception suffix to
150
     * @param string|null $filename   The filename used to create the suffix
151
     * @param string|null $lineNumber The line number used to create the suffx
152
     *
153
     * @return string The message with the appended exception suffix
154
     * @deprecated Will be removed with version 1.0.0, use subject method instead
155
     *
156
     * @codeCoverageIgnore
157
     */
158
    protected function appendExceptionSuffix($message = null, $filename = null, $lineNumber = null)
159
    {
160
        return $this->getSubject()->appendExceptionSuffix($message, $filename, $lineNumber);
161
    }
162
163
    /**
164
     * Wraps the passed exeception into a new one by trying to resolve the original filname,
165
     * line number and column name and use it for a detailed exception message.
166
     *
167
     * @param string     $columnName The column name that should be resolved
168
     * @param \Exception $parent     The exception we want to wrap
169
     * @param string     $className  The class name of the exception type we want to wrap the parent one
170
     *
171
     * @return \Exception the wrapped exception
172
     * @deprecated Will be removed with version 1.0.0, use subject method instead
173
     *
174
     * @codeCoverageIgnore
175
     */
176
    protected function wrapException(
177
        $columnName,
178
        \Exception $parent = null,
179
        $className = '\TechDivision\Import\Exceptions\WrappedColumnException'
180
    ) {
181
        return $this->getSubject()->wrapException($columnName, $parent, $className);
182
    }
183
184
    /**
185
     * Queries whether or not debug mode is enabled or not, default is TRUE.
186
     *
187
     * @return boolean TRUE if debug mode is enabled, else FALSE
188
     * @deprecated Will be removed with version 1.0.0, use subject method instead
189
     *
190
     * @codeCoverageIgnore
191
     */
192
    protected function isDebugMode()
193
    {
194
        return $this->getSubject()->isDebugMode();
195
    }
196
197
    /**
198
     * Stop's observer execution on the actual row.
199
     *
200
     * @return void
201
     * @deprecated Will be removed with version 1.0.0, use subject method instead
202
     *
203
     * @codeCoverageIgnore
204
     */
205
    protected function skipRow()
206
    {
207
        $this->getSubject()->skipRow();
208
    }
209
210
    /**
211
     * Return's the name of the file to import.
212
     *
213
     * @return string The filename
214
     * @deprecated Will be removed with version 1.0.0, use subject method instead
215
     *
216
     * @codeCoverageIgnore
217
     */
218
    protected function getFilename()
219
    {
220
        return $this->getSubject()->getFilename();
221
    }
222
223
    /**
224
     * Return's the actual line number.
225
     *
226
     * @return integer The line number
227
     * @deprecated Will be removed with version 1.0.0, use subject method instead
228
     *
229
     * @codeCoverageIgnore
230
     */
231
    protected function getLineNumber()
232
    {
233
        return $this->getSubject()->getLineNumber();
234
    }
235
236
    /**
237
     * Return's the logger with the passed name, by default the system logger.
238
     *
239
     * @param string $name The name of the requested system logger
240
     *
241
     * @return \Psr\Log\LoggerInterface The logger instance
242
     * @throws \Exception Is thrown, if the requested logger is NOT available
243
     * @deprecated Will be removed with version 1.0.0, use subject method instead
244
     *
245
     * @codeCoverageIgnore
246
     */
247
    protected function getSystemLogger($name = LoggerKeys::SYSTEM)
248
    {
249
        return $this->getSubject()->getSystemLogger($name);
250
    }
251
252
    /**
253
     * Return's the array with the system logger instances.
254
     *
255
     * @return array The logger instance
256
     * @deprecated Will be removed with version 1.0.0, use subject method instead
257
     *
258
     * @codeCoverageIgnore
259
     */
260
    protected function getSystemLoggers()
261
    {
262
        return $this->getSubject()->getSystemLoggers();
263
    }
264
265
    /**
266
     * Return's the multiple field delimiter character to use, default value is comma (,).
267
     *
268
     * @return string The multiple field delimiter character
269
     * @deprecated Will be removed with version 1.0.0, use subject method instead
270
     *
271
     * @codeCoverageIgnore
272
     */
273
    protected function getMultipleFieldDelimiter()
274
    {
275
        return $this->getSubject()->getMultipleFieldDelimiter();
276
    }
277
278
    /**
279
     * Return's the multiple value delimiter character to use, default value is comma (|).
280
     *
281
     * @return string The multiple value delimiter character
282
     * @deprecated Will be removed with version 1.0.0, use subject method instead
283
     *
284
     * @codeCoverageIgnore
285
     */
286
    protected function getMultipleValueDelimiter()
287
    {
288
        return $this->getSubject()->getMultipleValueDelimiter();
289
    }
290
291
    /**
292
     * Queries whether or not the header with the passed name is available.
293
     *
294
     * @param string $name The header name to query
295
     *
296
     * @return boolean TRUE if the header is available, else FALSE
297
     * @deprecated Will be removed with version 1.0.0, use subject method instead
298
     *
299
     * @codeCoverageIgnore
300
     */
301
    protected function hasHeader($name)
302
    {
303
        return $this->getSubject()->hasHeader($name);
304
    }
305
306
    /**
307
     * Return's the header value for the passed name.
308
     *
309
     * @param string $name The name of the header to return the value for
310
     *
311
     * @return mixed The header value
312
     * @throws \InvalidArgumentException Is thrown, if the header with the passed name is NOT available
313
     * @deprecated Will be removed with version 1.0.0, use subject method instead
314
     *
315
     * @codeCoverageIgnore
316
     */
317
    protected function getHeader($name)
318
    {
319
        return $this->getSubject()->getHeader($name);
320
    }
321
322
    /**
323
     * Add's the header with the passed name and position, if not NULL.
324
     *
325
     * @param string $name The header name to add
326
     *
327
     * @return integer The new headers position
328
     * @deprecated Will be removed with version 1.0.0, use subject method instead
329
     *
330
     * @codeCoverageIgnore
331
     */
332
    protected function addHeader($name)
333
    {
334
        return $this->getSubject()->addHeader($name);
335
    }
336
337
    /**
338
     * Return's the ID of the product that has been created recently.
339
     *
340
     * @return string The entity Id
341
     * @deprecated Will be removed with version 1.0.0, use subject method instead
342
     *
343
     * @codeCoverageIgnore
344
     */
345
    protected function getLastEntityId()
346
    {
347
        return $this->getSubject()->getLastEntityId();
0 ignored issues
show
Bug introduced by
The method getLastEntityId() does not seem to exist on object<TechDivision\Impo...jects\SubjectInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
348
    }
349
350
    /**
351
     * Return's the source date format to use.
352
     *
353
     * @return string The source date format
354
     * @deprecated Will be removed with version 1.0.0, use subject method instead
355
     *
356
     * @codeCoverageIgnore
357
     */
358
    protected function getSourceDateFormat()
359
    {
360
        return $this->getSubject()->getSourceDateFormat();
361
    }
362
363
    /**
364
     * Cast's the passed value based on the backend type information.
365
     *
366
     * @param string $backendType The backend type to cast to
367
     * @param mixed  $value       The value to be casted
368
     *
369
     * @return mixed The casted value
370
     * @deprecated Will be removed with version 1.0.0, use subject method instead
371
     *
372
     * @codeCoverageIgnore
373
     */
374
    protected function castValueByBackendType($backendType, $value)
375
    {
376
        return $this->getSubject()->castValueByBackendType($backendType, $value);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method castValueByBackendType() does only exist in the following implementations of said interface: TechDivision\Import\Subjects\AbstractEavSubject.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
377
    }
378
379
    /**
380
     * Set's the store view code the create the product/attributes for.
381
     *
382
     * @param string $storeViewCode The store view code
383
     *
384
     * @return void
385
     * @deprecated Will be removed with version 1.0.0, use subject method instead
386
     *
387
     * @codeCoverageIgnore
388
     */
389
    protected function setStoreViewCode($storeViewCode)
390
    {
391
        $this->getSubject()->setStoreViewCode($storeViewCode);
392
    }
393
394
    /**
395
     * Return's the store view code the create the product/attributes for.
396
     *
397
     * @param string|null $default The default value to return, if the store view code has not been set
398
     *
399
     * @return string The store view code
400
     * @deprecated Will be removed with version 1.0.0, use subject method instead
401
     *
402
     * @codeCoverageIgnore
403
     */
404
    protected function getStoreViewCode($default = null)
405
    {
406
        return $this->getSubject()->getStoreViewCode($default);
407
    }
408
409
    /**
410
     * Prepare's the store view code in the subject.
411
     *
412
     * @return void
413
     * @deprecated Will be removed with version 1.0.0, use subject method instead
414
     *
415
     * @codeCoverageIgnore
416
     */
417
    protected function prepareStoreViewCode()
418
    {
419
        $this->getSubject()->prepareStoreViewCode();
420
    }
421
422
    /**
423
     * Return's the store ID of the store with the passed store view code
424
     *
425
     * @param string $storeViewCode The store view code to return the store ID for
426
     *
427
     * @return integer The ID of the store with the passed ID
428
     * @throws \Exception Is thrown, if the store with the actual code is not available
429
     * @deprecated Will be removed with version 1.0.0, use subject method instead
430
     *
431
     * @codeCoverageIgnore
432
     */
433
    protected function getStoreId($storeViewCode)
434
    {
435
        return $this->getSubject()->getStoreId($storeViewCode);
436
    }
437
438
    /**
439
     * Return's the store ID of the actual row, or of the default store
440
     * if no store view code is set in the CSV file.
441
     *
442
     * @param string|null $default The default store view code to use, if no store view code is set in the CSV file
443
     *
444
     * @return integer The ID of the actual store
445
     * @throws \Exception Is thrown, if the store with the actual code is not available
446
     * @deprecated Will be removed with version 1.0.0, use subject method instead
447
     *
448
     * @codeCoverageIgnore
449
     */
450
    protected function getRowStoreId($default = null)
451
    {
452
        return $this->getSubject()->getRowStoreId($default);
0 ignored issues
show
Bug introduced by
The method getRowStoreId() does not exist on TechDivision\Import\Subjects\SubjectInterface. Did you maybe mean getRow()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
453
    }
454
455
    /**
456
     * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'.
457
     * If the passed value is NOT a valid date, NULL will be returned.
458
     *
459
     * @param string|null $value The value to format
460
     *
461
     * @return string The formatted date
462
     * @deprecated Will be removed with version 1.0.0, use subject method instead
463
     *
464
     * @codeCoverageIgnore
465
     */
466
    protected function formatDate($value)
467
    {
468
        return $this->getSubject()->formatDate($value);
469
    }
470
471
    /**
472
     * Extracts the elements of the passed value by exploding them
473
     * with the also passed delimiter.
474
     *
475
     * @param string      $value     The value to extract
476
     * @param string|null $delimiter The delimiter used to extrace the elements
477
     *
478
     * @return array The exploded values
479
     * @deprecated Will be removed with version 1.0.0, use subject method instead
480
     *
481
     * @codeCoverageIgnore
482
     */
483
    protected function explode($value, $delimiter = null)
484
    {
485
        return $this->getSubject()->explode($value, $delimiter);
486
    }
487
488
    /**
489
     * Return's the Magento configuration value.
490
     *
491
     * @param string  $path    The Magento path of the requested configuration value
492
     * @param mixed   $default The default value that has to be returned, if the requested configuration value is not set
493
     * @param string  $scope   The scope the configuration value has been set
494
     * @param integer $scopeId The scope ID the configuration value has been set
495
     *
496
     * @return mixed The configuration value
497
     * @throws \Exception Is thrown, if nor a value can be found or a default value has been passed
498
     * @deprecated Will be removed with version 1.0.0, use subject method instead
499
     *
500
     * @codeCoverageIgnore
501
     */
502
    protected function getCoreConfigData($path, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0)
503
    {
504
        return $this->getSubject()->getCoreConfigData($path, $default, $scope, $scopeId);
505
    }
506
}
507