Completed
Pull Request — master (#97)
by Tim
06:34
created

StockStatusUpdateProcessor::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 44
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 44
ccs 0
cts 21
cp 0
rs 8.8571
cc 2
eloc 17
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Actions\Processors\StockStatusUpdateProcessor
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\Actions\Processors;
22
23
use TechDivision\Import\Utils\EntityStatus;
24
use TechDivision\Import\Product\Utils\MemberNames;
25
use TechDivision\Import\Product\Utils\SqlStatements;
26
use TechDivision\Import\Actions\Processors\AbstractUpdateProcessor;
27
28
/**
29
 * The stock status update processor implementation.
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
35
 * @link      http://www.techdivision.com
36
 */
37
class StockStatusUpdateProcessor extends AbstractUpdateProcessor
38
{
39
40
    /**
41
     * Implements the CRUD functionality the processor is responsible for,
42
     * can be one of CREATE, READ, UPDATE or DELETE a entity.
43
     *
44
     * @param array       $row  The data to handle
45
     * @param string|null $name The name of the prepared statement to execute
46
     *
47
     * @return void
48
     */
49
    public function execute($row, $name = null)
50
    {
51
52
        // UPDATE cataloginventory_stock_status SET %s WHERE %s',
53
54
        // load the field names
55
        $keys = array_keys($row);
56
57
        // create a unique name for the prepared statement
58
        $name = sprintf('%s-%s', $name, md5(implode('-', $keys)));
59
60
        // query whether or not the statement has been prepared
61
        if (!$this->hasPreparedStatement($name)) {
62
            // initialize the array for the primary key fields
63
            $pks = array();
64
            // remove the last value as PK from the array with the keys
65
            $pks[] = $keys[array_search(MemberNames::PRODUCT_ID, $row, true)];
66
            $pks[] = $keys[array_search(MemberNames::WEBSITE_ID, $row, true)];
67
            $pks[] = $keys[array_search(MemberNames::STOCK_ID, $row, true)];
68
69
            // remove the entity status from the keys
70
            unset($keys[array_search(MemberNames::ATTRIBUTE_ID, $keys, true)]);
71
            unset($keys[array_search(EntityStatus::MEMBER_NAME, $keys, true)]);
72
73
            // prepare the SET part of the SQL statement
74
            array_walk($keys, function (&$value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
                $value = sprintf('%s=:%s', $value, $value);
76
            });
77
78
            // prepare the WHERE part of the SQL statement
79
            array_walk($pks, function(&$value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
80
                $value = sprintf('%s=:%s', $value, $value);
81
            });
82
83
            // create the prepared UPDATE statement
84
            $statement = sprintf($this->getUtilityClass()->find(SqlStatements::UPDATE_STOCK_STATUS), implode(',', $keys), implode(' AND ', $pks));
85
86
            // prepare the statement
87
            $this->addPreparedStatement($name, $this->getConnection()->prepare($statement));
88
        }
89
90
        // pass the call to the parent method
91
        return parent::execute($row, $name);
92
    }
93
}
94