Completed
Push — master ( 347ead...a6e359 )
by Tim
03:33 queued 01:54
created

ProductLinkDeleteProcessor::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 15
cp 0
rs 9.488
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Link\Actions\Processors\ProductLinkDeleteProcessor
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    Martin Eisenführer <[email protected]>
15
 * @copyright 2020 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-variant
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Link\Actions\Processors;
22
23
use TechDivision\Import\Product\Link\Utils\MemberNames;
24
use TechDivision\Import\Actions\Processors\AbstractDeleteProcessor;
25
use TechDivision\Import\Product\Link\Utils\SqlStatementKeys;
26
27
/**
28
 * The product link delete processor implementation.
29
 *
30
 * @author    Martin Eisenführer <[email protected]>
31
 * @copyright 2020 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-variant
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductLinkDeleteProcessor extends AbstractDeleteProcessor
37
{
38
39
    /**
40
     * Delete all links that are not actual imported
41
     *
42
     * @param array       $row                  The row to persist
43
     * @param string|null $name                 The name of the prepared statement that has to be executed
44
     * @param string|null $primaryKeyMemberName The primary key member name of the entity to use
45
     *
46
     * @return string The ID of the updated entity
47
     */
48
    public function execute($row, $name = null, $primaryKeyMemberName = null)
49
    {
50
51
        // load the SKUs from the row
52
        $skus = $row[MemberNames::SKU];
53
54
        // make sure we've an array
55
        if (!is_array($skus)) {
56
            $skus = [$skus];
57
        }
58
59
        // all SKUs that should NOT be deleted
60
        $vals = implode(',', $skus);
61
62
        // concatenate the SKUs as comma separated SQL string
63
        $vals = str_replace(',', "','", "'" . $vals . "'");
64
65
        // replace the placeholders
66
        $sql = str_replace(
67
            array(':skus', ':product_id', ':link_type_id'),
68
            array($vals, $row[MemberNames::PRODUCT_ID], $row[MemberNames::LINK_TYPE_ID] ),
69
            $this->loadStatement(SqlStatementKeys::DELETE_PRODUCT_LINK)
70
        );
71
72
        // delete the links that are NOT in values (take a look at DELETE_PRODUCT_LINK definition)
73
        $this->getConnection()->query($sql);
74
    }
75
}
76