Completed
Pull Request — master (#6)
by Tim
09:07
created

AbstractBaseProcessor::getStatements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Actions\Processors\AbstractBaseProcessor
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Actions\Processors;
22
23
/**
24
 * An abstract processor implementation provide basic CRUD functionality.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
abstract class AbstractBaseProcessor extends AbstractProcessor
33
{
34
35
    /**
36
     * The array with the statements to be prepared.
37
     *
38
     * @var array
39
     */
40
    protected $statements = array();
41
42
    /**
43
     * The array with the prepared statements.
44
     *
45
     * @var array
46
     */
47
    protected $preparedStatements = array();
48
49
    /**
50
     * Return's the array with the SQL statements that has to be prepared.
51
     *
52
     * @return array The SQL statements to be prepared
53
     */
54
    protected function getStatements()
55
    {
56
        return $this->statements;
57
    }
58
59
    /**
60
     * Set's the prepared statement.
61
     *
62
     * @param \PDOStatement $preparedStatement The prepared statement
63
     *
64
     * @return void
65
     * @deprecated Use TechDivision\Import\Actions\Processors\AbstractBaseProcessor::addPreparedStatement() instead
66
     */
67
    protected function setPreparedStatement(\PDOStatement $preparedStatement)
68
    {
69
        $this->preparedStatements[$preparedStatement->queryString] = $preparedStatement;
70
    }
71
72
    /**
73
     * Add's the prepared statement.
74
     *
75
     * @param string        $name              The unique name of the prepared statement
76
     * @param \PDOStatement $preparedStatement The prepared statement
77
     *
78
     * @return void
79
     */
80
    protected function addPreparedStatement($name, \PDOStatement $preparedStatement)
81
    {
82
        $this->preparedStatements[$name] = $preparedStatement;
83
    }
84
85
    /**
86
     * Return's the prepared statement.
87
     *
88
     * @param string $name The name of the prepared statement to return
89
     *
90
     * @return \PDOStatement The prepared statement
91
     */
92
    protected function getPreparedStatement($name = null)
93
    {
94
95
        // try to load the prepared statement, or use the default one
96
        if (isset($this->preparedStatements[$name])) {
97
            return $this->preparedStatements[$name];
98
        }
99
100
        // return the first (default) prepared statement
101
        return reset($this->preparedStatements);
102
    }
103
104
    /**
105
     * The array with the prepared statements.
106
     *
107
     * @return array The prepared statments
108
     */
109
    protected function getPreparedStatements()
110
    {
111
        return $this->preparedStatements;
112
    }
113
114
    /**
115
     * Implements the CRUD functionality the processor is responsible for,
116
     * can be one of CREATE, READ, UPDATE or DELETE a entity.
117
     *
118
     * @param array       $row  The data to handle
119
     * @param string|null $name The name of the prepared statement to execute
120
     *
121
     * @return void
122
     */
123
    public function execute($row, $name = null)
124
    {
125
        $this->getPreparedStatement($name)->execute($row);
126
    }
127
128
    /**
129
     * Initializes the proceessor with the prepared statements.
130
     *
131
     * @return void
132
     */
133
    public function init()
134
    {
135
        foreach ($this->getStatements() as $name => $statement) {
136
            $this->addPreparedStatement($name, $this->getConnection()->prepare($statement));
137
        }
138
    }
139
}
140