Completed
Push — master ( 5bcafe...c5dea9 )
by Tim
13s
created

ClearUrlRewriteObserver::__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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\UrlRewrite\Observers\ClearUrlRewriteObserver
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-url-rewrite
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\UrlRewrite\Observers;
22
23
use TechDivision\Import\Product\Utils\ColumnKeys;
24
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
25
use TechDivision\Import\Product\UrlRewrite\Utils\SqlStatements;
26
use TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface;
27
28
/**
29
 * Observer that removes the product URL rewrite with the SKU found in the CSV file.
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-url-rewrite
35
 * @link      http://www.techdivision.com
36
 */
37
class ClearUrlRewriteObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * The product URL rewrite processor instance.
42
     *
43
     * @var \TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface
44
     */
45
    protected $productUrlRewriteProcessor;
46
47
    /**
48
     * Initialize the observer with the passed product URL rewrite processor instance.
49
     *
50
     * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productUrlRewriteProcessor The product URL rewrite processor instance
51
     */
52
    public function __construct(ProductUrlRewriteProcessorInterface $productUrlRewriteProcessor)
53
    {
54
        $this->productUrlRewriteProcessor = $productUrlRewriteProcessor;
55
    }
56
57
    /**
58
     * Return's the product URL rewrite processor instance.
59
     *
60
     * @return \TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface The product URL rewrite processor instance
61
     */
62
    protected function getProductUrlRewriteProcessor()
63
    {
64
        return $this->productUrlRewriteProcessor;
65
    }
66
67
    /**
68
     * Process the observer's business logic.
69
     *
70
     * @return array The processed row
71
     */
72
    protected function process()
73
    {
74
75
        // query whether or not, we've found a new SKU => means we've found a new product
76
        if ($this->isLastSku($sku = $this->getValue(ColumnKeys::SKU))) {
77
            return;
78
        }
79
80
        // elete the URL rewrites of the product with the passed SKU
81
        $this->deleteUrlRewrite(array(ColumnKeys::SKU => $sku), SqlStatements::DELETE_URL_REWRITE_BY_SKU);
82
    }
83
84
    /**
85
     * Delete's the URL rewrite(s) with the passed attributes.
86
     *
87
     * @param array       $row  The attributes of the entity to delete
88
     * @param string|null $name The name of the prepared statement that has to be executed
89
     *
90
     * @return void
91
     */
92
    protected function deleteUrlRewrite($row, $name = null)
93
    {
94
        $this->getProductUrlRewriteProcessor()->deleteUrlRewrite($row, $name);
95
    }
96
}
97