Completed
Push — master ( c889ea...14eda7 )
by Pierre
03:04
created

Core::setConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Component\Db;
4
5
use App\Component\Model\Orm\Orm;
6
use App\Component\Db\Factory;
7
use Nymfonya\Component\Container;
8
9
class Core
10
{
11
    /**
12
     * connection
13
     *
14
     * @var \PDO | boolean
15
     */
16
    protected $connection;
17
18
    /**
19
     * container
20
     *
21
     * @var Container
22
     */
23
    protected $container;
24
25
    /**
26
     * factory
27
     *
28
     * @var Factory
29
     */
30
    protected $factory;
31
32
    /**
33
     * logger
34
     *
35
     * @var \Monolog\Logger
36
     */
37
    protected $logger;
38
39
    /**
40
     * sql
41
     *
42
     * @var string
43
     */
44
    protected $sql;
45
46
    /**
47
     * statement
48
     *
49
     * @var \PDOStatement | boolean
50
     */
51
    protected $statement;
52
53
    /**
54
     * fetch mode
55
     *
56
     * @var int
57
     */
58
    protected $fetchMode = \PDO::FETCH_ASSOC;
59
60
    /**
61
     * rowset result
62
     *
63
     * @var array
64
     */
65
    protected $rowset;
66
67
    /**
68
     * error
69
     *
70
     * @var boolean
71
     */
72
    protected $error;
73
74
    /**
75
     * code error
76
     *
77
     * @var string
78
     */
79
    protected $errorCode;
80
81
    /**
82
     * error message
83
     *
84
     * @var string
85
     */
86
    protected $errorMessage;
87
88
    /**
89
     * instanciate
90
     */
91 11
    public function __construct(Container $container)
92
    {
93 11
        $this->container = $container;
94 11
        $this->factory = new Factory($this->container);
95 11
        $this->logger = $this->container->getService(\Monolog\Logger::class);
96 11
        $this->rowset = [];
97 11
        $this->resetError();
98
    }
99
100
    /**
101
     * set connection from Orm instance
102
     *
103
     * @param Orm $ormInstance
104
     * @return Core
105
     */
106 6
    public function fromOrm(Orm &$ormInstance): Core
107
    {
108 6
        $this->connection = $this->factory->getConnection(
109 6
            $ormInstance->getSlot(),
110 6
            $ormInstance->getDatabase()
111
        );
112 6
        return $this;
113
    }
114
115
    /**
116
     * run query with bind values and types
117
     *
118
     * @param string $sql
119
     * @param array $bindParams
120
     * @param array $bindTypes
121
     * @return Core
122
     */
123 7
    public function run(
124
        string $sql,
125
        array $bindParams = [],
126
        array $bindTypes = []
127
    ): Core {
128 7
        $this->sql = $sql;
129 7
        $this->resetError();
130
        try {
131 7
            $this->statement = $this->connection->prepare($sql);
132 7
            if ($this->statement instanceof \PDOStatement) {
133 7
                $this->statement->setFetchMode($this->fetchMode);
134 7
                if (!empty($bindParams)) {
135 3
                    $this->bindArray(
136 3
                        $this->statement,
137 3
                        $bindParams,
138 3
                        $bindTypes
139
                    );
140
                }
141
            }
142 7
            $this->statement->execute();
143 5
        } catch (\PDOException $e) {
144 5
            $this->setError(true, $e->getCode(), $e->getMessage());
145 5
            $this->logger->alert('Core Db : Run failed');
146 5
            $this->logger->alert($this->errorMessage);
147
        }
148 7
        return $this;
149
    }
150
151
    /**
152
     * hydrate
153
     *
154
     * @return Core
155
     */
156 6
    public function hydrate(): Core
157
    {
158 6
        if ($this->statement instanceof \PDOStatement) {
159 2
            $this->rowset = $this->statement->fetchAll($this->fetchMode);
160 2
            $this->statement->closeCursor();
161
        }
162 6
        return $this;
163
    }
164
165
    /**
166
     * return run result
167
     *
168
     * @return array
169
     */
170 5
    public function getRowset(): array
171
    {
172 5
        return $this->rowset;
173
    }
174
175
    /**
176
     * bindArray
177
     *
178
     * @param \PDOStatement $poStatement
179
     * @param array $paArray
180
     * @param array $forcedTypes
181
     * @return $this
182
     */
183 3
    public function bindArray(\PDOStatement &$poStatement, array &$paArray, array $forcedTypes = [])
184
    {
185 3
        foreach ($paArray as $k => $v) {
186 3
            $type = (is_int($v)) ? \PDO::PARAM_INT : \PDO::PARAM_STR;
187
188 3
            if (isset($forcedTypes[$k])) {
189 1
                $type = $forcedTypes[$k];
190 1
                $v = ($type == \PDO::PARAM_INT) ? (int) $v : $v;
191
            }
192 3
            $value = is_array($v) ? serialize($v) : $v;
193 3
            $key = $k;
194
            try {
195 3
                $poStatement->bindValue($key, $value, $type);
196 1
            } catch (\PDOException $e) {
197 1
                $this->setError(true, $e->getCode(), $e->getMessage());
198 1
                $this->logger->alert(
199 1
                    'Sql Bind Error [' . $key . ':' . $value . ':' . $type . ']'
200
                );
201
            }
202
        }
203 3
        return $this;
204
    }
205
206
    /**
207
     * set connection
208
     *
209
     * @param \PDO $connection
210
     * @return Core
211
     */
212 1
    protected function setConnection(\PDO $connection): Core
213
    {
214 1
        $this->connection = $connection;
215 1
        return $this;
216
    }
217
218
    /**
219
     * return true if error
220
     *
221
     * @return boolean
222
     */
223 4
    protected function isError(): bool
224
    {
225 4
        return $this->error === true;
226
    }
227
228
    /**
229
     * reset last error
230
     *
231
     * @return Core
232
     */
233 1
    protected function resetError(): Core
234
    {
235 1
        $this->setError(false, 0, '');
236 1
        return $this;
237
    }
238
239
    /**
240
     * get error code
241
     *
242
     * @return mixed
243
     */
244 1
    protected function getErrorCode()
245
    {
246 1
        return $this->errorCode;
247
    }
248
249
    /**
250
     * get error message
251
     *
252
     * @return string
253
     */
254 1
    protected function getErrorMessage(): string
255
    {
256 1
        return $this->errorMessage;
257
    }
258
259
    /**
260
     * set error status, code and message
261
     *
262
     * @param boolean $status
263
     * @param integer | string $code
264
     * @param string $message
265
     * @return Core
266
     */
267 1
    protected function setError(bool $status, $code, string $message): Core
268
    {
269 1
        $this->error = $status;
270 1
        $this->errorCode = $code;
271 1
        $this->errorMessage = $message;
272 1
        return $this;
273
    }
274
}
275