Completed
Pull Request — master (#99)
by Tim
04:07
created

AbstractBaseProcessor::addPreparedStatement()   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 2
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
use TechDivision\Import\Utils\EntityStatus;
24
25
/**
26
 * An abstract processor implementation provide basic CRUD functionality.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
abstract class AbstractBaseProcessor extends AbstractProcessor
35
{
36
37
    /**
38
     * The array with the statements to be prepared.
39
     *
40
     * @var array
41
     */
42
    protected $statements = array();
43
44
    /**
45
     * The array with the prepared statements.
46
     *
47
     * @var array
48
     */
49
    protected $preparedStatements = array();
50
51
    /**
52
     * Return's the array with the SQL statements that has to be prepared.
53
     *
54
     * @return array The SQL statements to be prepared
55
     */
56
    protected function getStatements()
57
    {
58
        return $this->statements;
59
    }
60
61
    /**
62
     * Set's the prepared statement.
63
     *
64
     * @param \PDOStatement $preparedStatement The prepared statement
65
     *
66
     * @return void
67
     * @deprecated Use TechDivision\Import\Actions\Processors\AbstractBaseProcessor::addPreparedStatement() instead
68
     */
69
    protected function setPreparedStatement(\PDOStatement $preparedStatement)
70
    {
71
        $this->preparedStatements[$preparedStatement->queryString] = $preparedStatement;
72
    }
73
74
    /**
75
     * Add's the prepared statement.
76
     *
77
     * @param string        $name              The unique name of the prepared statement
78
     * @param \PDOStatement $preparedStatement The prepared statement
79
     *
80
     * @return void
81
     */
82
    protected function addPreparedStatement($name, \PDOStatement $preparedStatement)
83
    {
84
        $this->preparedStatements[$name] = $preparedStatement;
85
    }
86
87
    /**
88
     * Return's the prepared statement.
89
     *
90
     * @param string $name The name of the prepared statement to return
91
     *
92
     * @return \PDOStatement The prepared statement
93
     */
94
    protected function getPreparedStatement($name = null)
95
    {
96
97
        // try to load the prepared statement, or use the default one
98
        if (isset($this->preparedStatements[$name])) {
99
            return $this->preparedStatements[$name];
100
        }
101
102
        // return the first (default) prepared statement
103
        return reset($this->preparedStatements);
104
    }
105
106
    /**
107
     * Qeuery whether or not the prepared statement is available or not.
108
     *
109
     * @param string $name The nqme of the prepared statement
110
     *
111
     * @return boolean TRUE if the prepared statement is available, else FALSE
112
     */
113
    protected function hasPreparedStatement($name)
114
    {
115
        return isset($this->preparedStatements[$name]);
116
    }
117
118
    /**
119
     * The array with the prepared statements.
120
     *
121
     * @return array The prepared statments
122
     */
123
    protected function getPreparedStatements()
124
    {
125
        return $this->preparedStatements;
126
    }
127
128
    /**
129
     * Prepare's and return's the passed row by removing the
130
     * entity status.
131
     *
132
     * @param array $row The row to prepare
133
     *
134
     * @return array The prepared row
135
     */
136
    protected function prepareRow(array $row)
137
    {
138
139
        // remove the entity status
140
        unset($row[EntityStatus::MEMBER_NAME]);
141
142
        // return the prepared row
143
        return $row;
144
    }
145
146
    /**
147
     * Implements the CRUD functionality the processor is responsible for,
148
     * can be one of CREATE, READ, UPDATE or DELETE a entity.
149
     *
150
     * @param array       $row  The data to handle
151
     * @param string|null $name The name of the prepared statement to execute
152
     *
153
     * @return void
154
     */
155
    public function execute($row, $name = null)
156
    {
157
        $this->getPreparedStatement($name)->execute($this->prepareRow($row));
158
    }
159
160
    /**
161
     * Initializes the proceessor with the prepared statements.
162
     *
163
     * @return void
164
     */
165
    public function init()
166
    {
167
        foreach ($this->getStatements() as $name => $statement) {
168
            $this->addPreparedStatement($name, $this->getConnection()->prepare($statement));
169
        }
170
    }
171
}
172