Completed
Pull Request — master (#67)
by Tim
08:20
created

UrlKeyObserver::__construct()   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 2
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\Product\Observers\UrlKeyObserver
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-product
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Observers;
22
23
use Zend\Filter\FilterInterface;
24
use TechDivision\Import\Product\Utils\ColumnKeys;
25
use TechDivision\Import\Utils\Filter\UrlKeyFilterTrait;
26
27
/**
28
 * Observer that extracts the URL key from the product name and adds a two new columns
29
 * with the their values.
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-product
35
 * @link      http://www.techdivision.com
36
 */
37
class UrlKeyObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * The trait that provides string => URL key conversion functionality.
42
     *
43
     * @var \TechDivision\Import\Utils\Filter\UrlKeyFilterTrait
44
     */
45
    use UrlKeyFilterTrait;
46
47
    /**
48
     * Initialize the observer with the passed product bunch processor instance.
49
     *
50
     * @param \Zend\Filter\FilterInterface $convertLiteralUrlFilter The URL filter instance
51
     */
52
    public function __construct(FilterInterface $convertLiteralUrlFilter)
53
    {
54
        $this->convertLiteralUrlFilter = $convertLiteralUrlFilter;
0 ignored issues
show
Bug introduced by
The property convertLiteralUrlFilter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
    }
56
57
    /**
58
     * Process the observer's business logic.
59
     *
60
     * @return void
61
     */
62
    protected function process()
63
    {
64
65
        // query whether or not the URL key column has a value
66
        if ($this->hasValue(ColumnKeys::URL_KEY)) {
67
            return;
68
        }
69
70
        // query whether or not a product name is available
71
        if ($this->hasValue(ColumnKeys::NAME)) {
72
            $this->setValue(ColumnKeys::URL_KEY, $this->convertNameToUrlKey($this->getValue(ColumnKeys::NAME)));
73
            return;
74
        }
75
76
        // throw an exception, that the URL key can not be initialized
77
        $this->getSystemLogger()->debug(
78
            sprintf(
79
                'Can\'t initialize the URL key in CSV file %s on line %d',
80
                $this->getFilename(),
81
                $this->getLineNumber()
82
            )
83
        );
84
    }
85
}
86