Completed
Pull Request — master (#38)
by Tim
10:16
created

UrlObserver::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\UrlObserver
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\Category\Utils\ColumnKeys;
24
use TechDivision\Import\Utils\Filter\ConvertLiteralUrl;
25
26
/**
27
 * Observer that extracts the URL key/path from the path and adds a two new columns
28
 * 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
34
 * @link      http://www.techdivision.com
35
 */
36
class UrlObserver extends AbstractObserver
37
{
38
39
    /**
40
     * Will be invoked by the action on the events the listener has been registered for.
41
     *
42
     * @param array $row The row to handle
43
     *
44
     * @return array The modified row
45
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
46
     */
47
    public function handle(array $row)
48
    {
49
50
        // initialize the row
51
        $this->setRow($row);
52
53
        // process the functionality and return the row
54
        $this->process();
55
56
        // return the processed row
57
        return $this->getRow();
58
    }
59
60
    /**
61
     * Process the observer's business logic.
62
     *
63
     * @return void
64
     */
65
    protected function process()
66
    {
67
68
        // initialize the URL key and array for the categories
69
        $urlKey = null;
0 ignored issues
show
Unused Code introduced by
$urlKey is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
70
        $urlPath = array();
71
        $categories = array();
72
73
        // explode the category names from the path
74
        if ($path = $this->getValue(ColumnKeys::PATH)) {
75
            $categories = $this->explode($path, '/');
76
        }
77
78
        // query whether or not the URL key column has a value
79
        if ($this->hasValue(ColumnKeys::URL_KEY)) {
80
            $urlKey = $this->getValue(ColumnKeys::URL_KEY);
81
        } elseif (sizeof($categories) > 0) {
82
            $this->setValue(ColumnKeys::URL_KEY, $urlKey = $this->convertNameToUrlKey(end($categories)));
83
        } else {
84
            $this->getSystemLogger()->debug(
85
                sprintf(
86
                    'Can\'t find an URL key or a path in CSV file %s on line %d',
87
                    $this->getFilename(),
88
                    $this->getLineNumber()
89
                )
90
            );
91
            return;
92
        }
93
94
        // add the column for the URL path, if not yet available
95
        if (!$this->hasHeader(ColumnKeys::URL_PATH)) {
96
            $this->addHeader(ColumnKeys::URL_PATH);
97
        }
98
99
        // prepare the URL path
100
        if (sizeof($categories) > 0) {
101
            $urlPath = array_slice($categories, 1, sizeof($categories) - 2);
102
        }
103
104
        // convert the elements of the URL path
105
        array_walk($urlPath, function (&$value) {
106
            $value = $this->convertNameToUrlKey($value);
107
        });
108
109
        // add the URL key to the URL path
110
        array_push($urlPath, $urlKey);
111
112
        // append the column with the URL path
113
        $this->setValue(ColumnKeys::URL_PATH, implode('/', $urlPath));
114
    }
115
116
    /**
117
     * Initialize's and return's the URL key filter.
118
     *
119
     * @return \TechDivision\Import\Product\Utils\ConvertLiteralUrl The URL key filter
120
     */
121
    protected function getUrlKeyFilter()
122
    {
123
        return new ConvertLiteralUrl();
124
    }
125
126
    /**
127
     * Convert's the passed string into a valid URL key.
128
     *
129
     * @param string $string The string to be converted, e. g. the product name
130
     *
131
     * @return string The converted string as valid URL key
132
     */
133
    protected function convertNameToUrlKey($string)
134
    {
135
        return $this->getUrlKeyFilter()->filter($string);
136
    }
137
}
138