1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Category\Observers\UrlKeyAndPathObserver |
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-category |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Category\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Category\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Utils\Filter\UrlKeyFilterTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Observer that extracts the URL key/path from the category path |
28
|
|
|
* and adds them as two new columns with the their values. |
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-category |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
class UrlKeyAndPathObserver extends AbstractCategoryImportObserver |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The trait that provides string => URL key conversion functionality. |
41
|
|
|
* |
42
|
|
|
* @var \TechDivision\Import\Utils\Filter\UrlKeyFilterTrait |
43
|
|
|
*/ |
44
|
|
|
use UrlKeyFilterTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Process the observer's business logic. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
protected function process() |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
// initialize the URL key and array for the categories |
55
|
|
|
$urlKey = null; |
|
|
|
|
56
|
|
|
$urlPath = array(); |
57
|
|
|
$categories = array(); |
58
|
|
|
|
59
|
|
|
// explode the category names from the path |
60
|
|
|
if ($path = $this->getValue(ColumnKeys::PATH)) { |
61
|
|
|
$categories = $this->explode($path, '/'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// query whether or not the URL key column has a value |
65
|
|
|
if ($this->hasValue(ColumnKeys::URL_KEY)) { |
66
|
|
|
$urlKey = $this->getValue(ColumnKeys::URL_KEY); |
67
|
|
|
} elseif (sizeof($categories) > 0) { |
68
|
|
|
$this->setValue(ColumnKeys::URL_KEY, $urlKey = $this->convertNameToUrlKey(end($categories))); |
69
|
|
|
} else { |
70
|
|
|
$this->getSystemLogger()->debug( |
71
|
|
|
sprintf( |
72
|
|
|
'Can\'t find an URL key or a path in CSV file %s on line %d', |
73
|
|
|
$this->getFilename(), |
74
|
|
|
$this->getLineNumber() |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// add the column for the URL path, if not yet available |
81
|
|
|
if (!$this->hasHeader(ColumnKeys::URL_PATH)) { |
82
|
|
|
$this->addHeader(ColumnKeys::URL_PATH); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// prepare the URL path |
86
|
|
|
if (sizeof($categories) > 0) { |
87
|
|
|
$urlPath = array_slice($categories, 1, sizeof($categories) - 2); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// convert the elements of the URL path |
91
|
|
|
array_walk($urlPath, function (&$value) { |
92
|
|
|
$value = $this->convertNameToUrlKey($value); |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
// add the URL key to the URL path |
96
|
|
|
array_push($urlPath, $urlKey); |
97
|
|
|
|
98
|
|
|
// append the column with the URL path |
99
|
|
|
$this->setValue(ColumnKeys::URL_PATH, implode('/', $urlPath)); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.