Completed
Push — master ( acae87...3c5e0a )
by Iqbal
14:22
created

Pdo::normalizeRelation()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 4.909
cc 9
eloc 20
nc 12
nop 2
1
<?php
2
/*
3
 * This file is part of the Borobudur-Cqrs package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Cqrs\ReadModel\Storage\Pdo;
12
13
use Borobudur\Collection\Collection;
14
use Borobudur\Cqrs\Exception\InvalidArgumentException;
15
use Borobudur\Cqrs\ReadModel\ReadModelInterface;
16
use Borobudur\Cqrs\ReadModel\Storage\Finder\Expression\CompositeExpressionInterface;
17
use Borobudur\Cqrs\ReadModel\Storage\Pdo\Exception\PdoException;
18
use Borobudur\Cqrs\ReadModel\Storage\Pdo\Expression\PdoCompositeExpression;
19
use Borobudur\Cqrs\ReadModel\Storage\StorageInterface;
20
use Borobudur\Cqrs\Serializer\DataTypeCasterTrait;
21
use Borobudur\Serialization\StringInterface;
22
use Borobudur\Serialization\ValuableInterface;
23
use PDO as PhpPdo;
24
use PDOStatement;
25
26
/**
27
 * @author      Iqbal Maulana <[email protected]>
28
 * @created     8/18/15
29
 */
30
class Pdo implements StorageInterface
31
{
32
    use DataTypeCasterTrait;
33
34
    /**
35
     * @var PhpPdo
36
     */
37
    protected $conn;
38
39
    /**
40
     * @var PdoConfig
41
     */
42
    protected $config;
43
44
    /**
45
     * @var array
46
     */
47
    protected $quoteMaps = array('mysql' => '`', 'pgsql' => '"');
48
49
    /**
50
     * @var string
51
     */
52
    protected $quote = '`';
53
54
    /**
55
     * @var int
56
     */
57
    protected $transactionalLevel = 0;
58
59
    /**
60
     * @var bool
61
     */
62
    protected $transactional = false;
63
64
    /**
65
     * Constructor.
66
     *
67
     * @param PdoConfig $config
68
     */
69
    public function __construct(PdoConfig $config)
70
    {
71
        try {
72
            $this->conn = new PhpPdo($config->getDsn(), $config->getUser(), $config->getPass());
73
            $this->conn->setAttribute(PhpPdo::ATTR_ERRMODE, PhpPdo::ERRMODE_EXCEPTION);
74
        } catch (\PDOException $e) {
75
            throw new PdoException($e);
76
        }
77
78
        $this->config = $config;
79
80
        if (isset($this->quoteMaps[$config->getEngine()])) {
81
            $this->quote = $this->quoteMaps[$config->getEngine()];
82
        }
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function save(ReadModelInterface $model, $table)
89
    {
90
        $class = get_class($model);
91
        $id = $this->resolveValue($model->getId());
92
        if (null !== $prev = $this->findById($id, $table, $class)) {
93
            $values = $this->normalize(array_merge($prev->serialize(), $model->serialize()));
94
            $conditions = $this->normalize(array('id' => $id));
95
            unset($values['id']);
96
            $this->exec($this->buildUpdateQuery($values, $conditions, $table), array_merge($values, $conditions));
97
98
            return;
99
        }
100
101
        $values = $this->normalize($this->normalizeRelation($model->serialize(), $class));
102
        $values['id'] = $id;
103
        $this->exec($this->buildInsertQuery($values, $table), $values);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function remove($id, $table)
110
    {
111
        $values = array('id' => $this->resolveValue($id));
112
        $this->exec(sprintf('DELETE FROM %s WHERE id = :id', $this->quote($table)), $values);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function findById($id, $table, $class)
119
    {
120
        $finder = $this->finder($table, $class);
121
122
        return $finder->where($finder->expr()->equal('id', $this->resolveValue($id)))->first();
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function finder($table, $class)
129
    {
130
        return new PdoFinder($this, $table, $class, $this->config->getParser(), $this->quote);
131
    }
132
133
    /**
134
     * Execute query without statement.
135
     *
136
     * @param string $sql
137
     * @param array  $values
138
     */
139
    public function exec($sql, array $values = array())
140
    {
141
        if (!empty($values)) {
142
            $normalized = array();
143
            foreach ($values as $index => $value) {
144
                $normalized[':' . $index] = $value;
145
            }
146
147
            $values = $normalized;
148
        }
149
150
        try {
151
            $conn = $this->conn->prepare($sql);
152
            $conn->execute($values);
153
        } catch (\PDOException $e) {
154
            throw new PdoException($e);
155
        }
156
    }
157
158
    /**
159
     * Execute query with statement.
160
     *
161
     * @param string $sql
162
     * @param int    $mode
163
     *
164
     * @return PDOStatement
165
     */
166
    public function query($sql, $mode = PhpPdo::FETCH_ASSOC)
167
    {
168
        try {
169
            return $this->conn->query($sql, $mode);
170
        } catch (\PDOException $e) {
171
            throw new PdoException($e);
172
        }
173
    }
174
175
    /**
176
     * @return int
177
     */
178
    public function getTransactionalLevel()
179
    {
180
        return $this->transactionalLevel;
181
    }
182
183
    /**
184
     * @return boolean
185
     */
186
    public function isTransactional()
187
    {
188
        return $this->transactional;
189
    }
190
191
    /**
192
     * Begin transaction
193
     */
194
    public function beginTransaction()
195
    {
196
        $this->transactional = true;
197
        if (0 === $this->transactionalLevel) {
198
            $this->conn->beginTransaction();
199
        }
200
201
        $this->transactionalLevel += 1;
202
    }
203
204
    /**
205
     * Rollback transaction
206
     */
207
    public function rollback()
208
    {
209
        if (true === $this->transactional) {
210
            $this->transactionalLevel = 0;
211
            $this->transactional = false;
212
            $this->conn->rollBack();
213
        }
214
    }
215
216
    /**
217
     * Commit transaction.
218
     */
219
    public function commit()
220
    {
221
        if (true === $this->transactional) {
222
            $this->transactionalLevel -= 1;
223
            if (0 === $this->transactionalLevel) {
224
                $this->transactional = false;
225
                $this->conn->commit();
226
            }
227
        }
228
    }
229
230
    /**
231
     * Compute fields to expressions.
232
     *
233
     * @param array     $fields
234
     * @param PdoFinder $finder
235
     *
236
     * @return PdoCompositeExpression
237
     */
238
    protected function computeFieldsExpression(array $fields, PdoFinder $finder)
239
    {
240
        $expressions = array();
241
        foreach ($fields as $name => $value) {
242
            $expressions[] = $finder->expr()->equal($name, $value);
243
        }
244
245
        return new PdoCompositeExpression(CompositeExpressionInterface::LOGICAL_AND, $expressions);
246
    }
247
248
    /**
249
     * @param array  $values
250
     * @param string $class
251
     *
252
     * @return array
253
     */
254
    protected function normalizeRelation(array $values, $class)
255
    {
256
        $relations = $class::{'relations'}();
257
        foreach ($relations as $property => $relation) {
258
            $field = $relation['reference'];
259
            if (isset($values[$property])) {
260
                $value = $values[$property];
261
                if (is_object($value)) {
262
                    if (method_exists($value, 'getId')) {
263
                        $value = $value->{'getId'}();
264
                    } elseif (property_exists($value, 'id')) {
265
                        $value = $value->{'id'};
266
                    } else {
267
                        $value = null;
268
                    }
269
                } elseif (is_array($value) && isset($value['id'])) {
270
                    $value = $value['id'];
271
                }
272
273
                $values[$field] = $value ? (string) $value : null;
274
                unset($values[$property]);
275
                continue;
276
            }
277
278
            $values[$field] = null;
279
        }
280
281
        return $values;
282
    }
283
284
    /**
285
     * Normalize value.
286
     *
287
     * @param array $values
288
     *
289
     * @return array
290
     */
291
    protected function normalize(array $values)
292
    {
293
        foreach ($values as $name => $value) {
294
            if (is_array($value)) {
295
                throw new InvalidArgumentException(
296
                    sprintf(
297
                        'Cannot parse value with named "%s", data should be flat array.',
298
                        $name
299
                    )
300
                );
301
            }
302
303
            if (is_bool($value)) {
304
                $value = true === $value ? 'true' : 'false';
305
            }
306
307
            $values[$name] = self::castType($value);
308
        }
309
310
        return $values;
311
    }
312
313
    /**
314
     * Build query update.
315
     *
316
     * @param array  $values
317
     * @param array  $conditions
318
     * @param string $table
319
     *
320
     * @return string
321
     */
322
    protected function buildUpdateQuery(array $values, array $conditions, $table)
323
    {
324
        $sets = $this->buildDataSets($values);
325
        $wheres = $this->buildDataSets($conditions);
326
327
        return
328
            'UPDATE ' . $this->quote($table) .
329
            ' SET ' . implode(', ', $sets) .
330
            ' WHERE ' . implode(' AND ', $wheres);
331
    }
332
333
    /**
334
     * Build insert query.
335
     *
336
     * @param array  $values
337
     * @param string $table
338
     *
339
     * @return string
340
     */
341
    public function buildInsertQuery(array $values, $table)
342
    {
343
        $keys = array_map(array($this, 'quote'), array_keys($values));
344
        $values = array_keys($values);
345
346
        return
347
            'INSERT INTO ' . $this->quote($table) .
348
            ' (' . implode(', ', $keys) . ') VALUES (:' . implode(', :', $values) . ')';
349
    }
350
351
    /**
352
     * Build data sets.
353
     *
354
     * @param array $parts
355
     *
356
     * @return array
357
     */
358
    protected function buildDataSets(array $parts)
359
    {
360
        $sets = array();
361
        foreach ($parts as $name => $value) {
362
            $sets[] = $this->quote($name) . '=:' . $name;
363
        }
364
365
        return $sets;
366
    }
367
368
    /**
369
     * Quote field.
370
     *
371
     * @param string $field
372
     *
373
     * @return string
374
     */
375
    protected function quote($field)
376
    {
377
        return $this->quote . $field . $this->quote;
378
    }
379
380
    /**
381
     * @param mixed $value
382
     *
383
     * @return mixed|null|string
384
     */
385
    protected function resolveValue($value)
386
    {
387
        if ($value instanceof ValuableInterface) {
388
            return $value->getValue();
389
        }
390
391
        if ($value instanceof StringInterface) {
392
            return (string) $value;
393
        }
394
395
        return $value;
396
    }
397
}
398