Completed
Pull Request — master (#9)
by Tim
02:18
created

UrlKeyAndPathObserver::process()   C

Complexity

Conditions 7
Paths 34

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
rs 6.9743
cc 7
eloc 25
nc 34
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use TechDivision\Import\Category\Utils\MemberNames;
26
27
/**
28
 * Observer that extracts the URL key/path from the category path
29
 * and adds them as two new columns 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-category
35
 * @link      http://www.techdivision.com
36
 */
37
class UrlKeyAndPathObserver extends AbstractCategoryImportObserver
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
     * Process the observer's business logic.
49
     *
50
     * @return void
51
     */
52
    protected function process()
53
    {
54
55
        // initialize the URL key and array for the categories
56
        $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...
57
        $categories = array();
0 ignored issues
show
Unused Code introduced by
$categories 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...
58
59
        // query whether or not the URL key column has a value
60
        if ($this->hasValue(ColumnKeys::URL_KEY)) {
61
            $urlKey = $this->getValue(ColumnKeys::URL_KEY);
62
        } else {
63
            $this->setValue(
64
                ColumnKeys::URL_KEY,
65
                $urlKey = $this->convertNameToUrlKey($this->getValue(ColumnKeys::NAME))
66
            );
67
        }
68
69
        // explode the path into the category names
70
        if ($categories = $this->explode($this->getValue(ColumnKeys::PATH), '/')) {
71
            // initialize the category with the actual category's URL key
72
            $categoryPaths = array($urlKey);
73
74
            // iterate over the category names and try to load the category therefore
75
            for ($i = sizeof($categories); $i > 1; $i--) {
76
                try {
77
                    // prepare the expected category name
78
                    $categoryPath = implode('/', array_slice($categories, 0, $i));
79
80
                    // load the existing category and prepend the URL key the array with the category URL keys
81
                    $existingCategory = $this->getCategoryByPath($categoryPath);
82
                    if (isset($existingCategory[MemberNames::URL_KEY])) {
83
                        array_unshift($categoryPaths, $existingCategory[MemberNames::URL_KEY]);
84
                    } else {
85
                        error_log(var_export($existingCategory, true));
86
                        $this->getSystemLogger()->debug(sprintf('Can\'t find URL key for category %s', $categoryPath));
87
                    }
88
89
                } catch (\Exception $e) {
90
                    $this->getSystemLogger()->debug(sprintf('Can\'t load parent category %s', $categoryPath));
91
                }
92
            }
93
94
            // create the header for the URL path
95
            if (!$this->hasHeader(ColumnKeys::URL_PATH)) {
96
                $this->addHeader(ColumnKeys::URL_PATH);
97
            }
98
99
            // set the URL path
100
            $this->setValue(ColumnKeys::URL_PATH, implode('/', $categoryPaths));
101
        }
102
    }
103
104
    /**
105
     * Return's the category with the passed path.
106
     *
107
     * @param string $path The path of the category to return
108
     *
109
     * @return array The category
110
     */
111
    protected function getCategoryByPath($path)
112
    {
113
        return $this->getSubject()->getCategoryByPath($path);
114
    }
115
}
116