Completed
Pull Request — master (#19)
by Tim
10:08
created

ClearProductObserver::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 2.004
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ClearProductObserver
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\Product\Utils\SqlStatements;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * Observer that removes the product with the SKU found in the CSV file.
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 ClearProductObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * Process the observer's business logic.
41
     *
42
     * @return array The processed row
43
     */
44 1
    protected function process()
45
    {
46
47
        // query whether or not, we've found a new SKU => means we've found a new product
48 1
        if ($this->isLastSku($sku = $this->getValue(ColumnKeys::SKU))) {
49
            return;
50
        }
51
52
        // FIRST delete the data related with the product with the passed SKU
53 1
        $this->deleteStockItem(array(ColumnKeys::SKU => $sku), SqlStatements::DELETE_STOCK_ITEM_BY_SKU);
54 1
        $this->deleteUrlRewrite(array(ColumnKeys::SKU => $sku), SqlStatements::DELETE_URL_REWRITE_BY_SKU);
55 1
        $this->deleteStockStatus(array(ColumnKeys::SKU => $sku), SqlStatements::DELETE_STOCK_STATUS_BY_SKU);
56 1
        $this->deleteProductWebsite(array(ColumnKeys::SKU => $sku), SqlStatements::DELETE_PRODUCT_WEBSITE_BY_SKU);
57 1
        $this->deleteCategoryProduct(array(ColumnKeys::SKU => $sku), SqlStatements::DELETE_CATEGORY_PRODUCT_BY_SKU);
58
59
        // delete the product with the passed SKU
60 1
        $this->deleteProduct(array(ColumnKeys::SKU => $sku));
61 1
    }
62
63
    /**
64
     * Delete's the entity with the passed attributes.
65
     *
66
     * @param array       $row  The attributes of the entity to delete
67
     * @param string|null $name The name of the prepared statement that has to be executed
68
     *
69
     * @return void
70
     */
71 1
    public function deleteProduct($row, $name = null)
72
    {
73 1
        $this->getSubject()->deleteProduct($row, $name);
74 1
    }
75
76
    /**
77
     * Delete's the URL rewrite(s) with the passed attributes.
78
     *
79
     * @param array       $row  The attributes of the entity to delete
80
     * @param string|null $name The name of the prepared statement that has to be executed
81
     *
82
     * @return void
83
     */
84 1
    public function deleteUrlRewrite($row, $name = null)
85
    {
86 1
        $this->getSubject()->deleteUrlRewrite($row, $name);
87 1
    }
88
89
    /**
90
     * Delete's the stock item(s) with the passed attributes.
91
     *
92
     * @param array       $row  The attributes of the entity to delete
93
     * @param string|null $name The name of the prepared statement that has to be executed
94
     *
95
     * @return void
96
     */
97 1
    public function deleteStockItem($row, $name = null)
98
    {
99 1
        $this->getSubject()->deleteStockItem($row, $name);
100 1
    }
101
102
    /**
103
     * Delete's the stock status with the passed attributes.
104
     *
105
     * @param array       $row  The attributes of the entity to delete
106
     * @param string|null $name The name of the prepared statement that has to be executed
107
     *
108
     * @return void
109
     */
110 1
    public function deleteStockStatus($row, $name = null)
111
    {
112 1
        $this->getSubject()->deleteStockStatus($row, $name);
113 1
    }
114
115
    /**
116
     * Delete's the product website relations with the passed attributes.
117
     *
118
     * @param array       $row  The attributes of the entity to delete
119
     * @param string|null $name The name of the prepared statement that has to be executed
120
     *
121
     * @return void
122
     */
123 1
    public function deleteProductWebsite($row, $name = null)
124
    {
125 1
        $this->getSubject()->deleteProductWebsite($row, $name);
126 1
    }
127
128
    /**
129
     * Delete's the category product relations with the passed attributes.
130
     *
131
     * @param array       $row  The attributes of the entity to delete
132
     * @param string|null $name The name of the prepared statement that has to be executed
133
     *
134
     * @return void
135
     */
136 1
    public function deleteCategoryProduct($row, $name = null)
137
    {
138 1
        $this->getSubject()->deleteCategoryProduct($row, $name);
139 1
    }
140
}
141