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

StockItemUpdateProcessor::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
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 43
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\StockItemUpdateProcessor
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\AbstractCreateProcessor;
27
28
/**
29
 * The stock item 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 StockItemUpdateProcessor extends AbstractCreateProcessor
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
        // load the field names
53
        $keys = array_keys($row);
54
55
        // create a unique name for the prepared statement
56
        $name = sprintf('%s-%s', $name, md5(implode('-', $keys)));
57
58
        // query whether or not the statement has been prepared
59
        if (!$this->hasPreparedStatement($name)) {
60
            // initialize the array for the primary key fields
61
            $pks = array();
62
            // load the last value as PK from the array with the keys
63
            $pks[] = $keys[array_search(MemberNames::ITEM_ID, $row, true)];
64
65
            // remove the entity status and the primary key from the keys
66
            unset($keys[array_search(MemberNames::ITEM_ID, $keys, true)]);
67
            unset($keys[array_search(EntityStatus::MEMBER_NAME, $keys, true)]);
68
69
            // prepare the SET part of the SQL statement
70
            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...
71
                $value = sprintf('%s=:%s', $value, $value);
72
            });
73
74
            // prepare the SET part of the SQL statement
75
            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...
76
                $value = sprintf('%s=:%s', $value, $value);
77
            });
78
79
            // create the prepared UPDATE statement
80
            $statement = sprintf($this->getUtilityClass()->find(SqlStatements::UPDATE_STOCK_ITEM), implode(',', $keys), implode(',', $pks));
81
82
            error_log($statement);
83
            error_log(print_r($row, true));
84
85
            // prepare the statement
86
            $this->addPreparedStatement($name, $this->getConnection()->prepare($statement));
87
        }
88
89
        // pass the call to the parent method
90
        return parent::execute($row, $name);
91
    }
92
}
93