Completed
Push — master ( 3ad691...419b95 )
by Tim
01:37
created

ClearUrlRewriteObserver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 88
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCategoryUrlRewriteProcessor() 0 4 1
A process() 0 27 3
A loadCategory() 0 4 1
A deleteUrlRewrite() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Category\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 2021 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\Category\Utils\MemberNames;
25
use TechDivision\Import\Category\Utils\SqlStatementKeys;
26
use TechDivision\Import\Category\Services\CategoryUrlRewriteProcessorInterface;
27
use TechDivision\Import\Exceptions\WrappedColumnException;
28
29
/**
30
 * Observer that removes the URL rewrite for the category with the path found in the CSV file.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2021 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import-category
36
 * @link      http://www.techdivision.com
37
 */
38
class ClearUrlRewriteObserver extends AbstractCategoryImportObserver
39
{
40
41
    /**
42
     * The product URL rewrite processor instance.
43
     *
44
     * @var \TechDivision\Import\Category\Services\CategoryUrlRewriteProcessorInterface
45
     */
46
    protected $categoryUrlRewriteProcessor;
47
48
    /**
49
     * Initialize the observer with the passed category URL rewrite processor instance.
50
     *
51
     * @param \TechDivision\Import\Category\Services\CategoryUrlRewriteProcessorInterface $categoryUrlRewriteProcessor The category URL rewrite processor instance
52
     */
53
    public function __construct(CategoryUrlRewriteProcessorInterface $categoryUrlRewriteProcessor)
54
    {
55
        $this->categoryUrlRewriteProcessor = $categoryUrlRewriteProcessor;
56
    }
57
58
    /**
59
     * Return's the category URL rewrite processor instance.
60
     *
61
     * @return \TechDivision\Import\Category\Services\CategoryUrlRewriteProcessorInterface The category URL rewrite processor instance
62
     */
63
    protected function getCategoryUrlRewriteProcessor()
64
    {
65
        return $this->categoryUrlRewriteProcessor;
66
    }
67
68
    /**
69
     * Process the observer's business logic.
70
     *
71
     * @return array The processed row
72
     */
73
    protected function process()
74
    {
75
76
        // query whether or not, we've found a new SKU => means we've found a new product
77
        if ($this->isLastPath($path = $this->getValue(ColumnKeys::PATH))) {
78
            return;
79
        }
80
81
        try {
82
            // try to load the entity ID for the product with the passed path
83
            $category = $this->loadCategory($this->mapPath($path));
84
            // delete the URL rewrites of the category with the passed path
85
            $this->deleteUrlRewrite(array(MemberNames::CATEGORY_ID => $category[MemberNames::ENTITY_ID]), SqlStatementKeys::DELETE_URL_REWRITE_BY_CATEGORY_ID);
86
            // delete the URL rewrites of the category with the passed path
87
            $this->deleteUrlRewrite(
88
                array(
89
                    MemberNames::ENTITY_ID   => $category[MemberNames::ENTITY_ID],
90
                    MemberNames::ENTITY_TYPE => 'category'
91
                ),
92
                SqlStatementKeys::DELETE_URL_REWRITE_BY_ENTITY_ID_AND_ENTITY_TYPE
93
            );
94
        } catch (WrappedColumnException $wce) {
95
            $this->getSubject()
96
                 ->getSystemLogger()
97
                 ->debug(sprintf('Category with path "%s" can\'t be loaded to clear URL rewrites', $path));
98
        }
99
    }
100
101
    /**
102
     * Return's the category with the passed ID.
103
     *
104
     * @param string $id The ID of the category to return
105
     *
106
     * @return array The category
107
     */
108
    protected function loadCategory($id)
109
    {
110
        return $this->getCategoryUrlRewriteProcessor()->loadCategory($id);
111
    }
112
113
    /**
114
     * Delete's the URL rewrite(s) with the passed attributes.
115
     *
116
     * @param array       $row  The attributes of the entity to delete
117
     * @param string|null $name The name of the prepared statement that has to be executed
118
     *
119
     * @return void
120
     */
121
    protected function deleteUrlRewrite($row, $name = null)
122
    {
123
        $this->getCategoryUrlRewriteProcessor()->deleteUrlRewrite($row, $name);
124
    }
125
}
126