Core   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 70
c 2
b 0
f 0
dl 0
loc 266
ccs 66
cts 66
cp 1
rs 10
wmc 22

12 Methods

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