Completed
Push — master ( 660080...baa810 )
by Tim
10s
created

UrlKeyObserver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 39
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 23 3
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 TechDivision\Import\Product\Utils\ColumnKeys;
24
use TechDivision\Import\Utils\Filter\UrlKeyFilterTrait;
25
26
/**
27
 * Observer that extracts the URL key from the product name 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-product
34
 * @link      http://www.techdivision.com
35
 */
36
class UrlKeyObserver extends AbstractProductImportObserver
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
        // query whether or not the URL key column has a value
55
        if ($this->hasValue(ColumnKeys::URL_KEY)) {
56
            return;
57
        }
58
59
        // query whether or not a product name is available
60
        if ($this->hasValue(ColumnKeys::NAME)) {
61
            $this->setValue(ColumnKeys::URL_KEY, $urlKey = $this->convertNameToUrlKey($this->getValue(ColumnKeys::NAME)));
62
            return;
63
        }
64
65
        // throw an exception, that the URL key can not be initialized
66
        $this->getSystemLogger()->debug(
67
            sprintf(
68
                'Can\'t initialize the URL key in CSV file %s on line %d',
69
                $this->getFilename(),
70
                $this->getLineNumber()
71
            )
72
        );
73
    }
74
}
75