Passed
Push — master ( efdb33...92d8b0 )
by Dawid
02:41
created

Repository::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Driver\Pdo;
4
5
use Igni\Storage\Driver\GenericRepository;
6
use Igni\Storage\Exception\RepositoryException;
7
use Igni\Storage\Storable;
8
9
abstract class Repository extends GenericRepository
10
{
11 11
    public function get($id): Storable
12
    {
13 11
        if ($this->entityManager->has($this->getEntityClass(), $id)) {
14 1
            return $this->entityManager->get($this->getEntityClass(), $id);
15
        }
16
17 11
        $cursor = $this->buildSelectQuery($id);
18 11
        $cursor->hydrateWith($this->hydrator);
19 11
        $entity = $cursor->current();
20 11
        $cursor->close();
21
22 11
        if (!$entity instanceof Storable) {
23 2
            throw RepositoryException::forNotFound($id);
24
        }
25
26 9
        return $entity;
27
    }
28
29 2
    public function create(Storable $entity): Storable
30
    {
31
        // Execute id auto-generation.
32 2
        $entity->getId();
33 2
        $cursor = $this->buildCreateQuery($entity);
34 2
        $cursor->execute();
35
36 2
        return $entity;
37
    }
38
39 2
    public function remove(Storable $entity): Storable
40
    {
41 2
        $cursor = $this->buildDeleteQuery($entity);
42 2
        $cursor->execute();
43
44 2
        return $entity;
45
    }
46
47 2
    public function update(Storable $entity): Storable
48
    {
49 2
        $cursor = $this->buildUpdateQuery($entity);
50 2
        $cursor->execute();
51
52 2
        return $entity;
53
    }
54
55 8
    protected function query($query, array $parameters = []): Cursor
56
    {
57 8
        $cursor = $this->connection->execute($query, $parameters);
58 8
        $cursor->hydrateWith($this->hydrator);
59 8
        return $cursor;
60
    }
61
62 11
    protected function buildSelectQuery($id): Cursor
63
    {
64 11
        $query = sprintf(
65 11
            'SELECT *FROM %s WHERE %s = :_id',
66 11
            $this->metaData->getSource(),
67 11
            $this->metaData->getIdentifier()->getFieldName()
68
        );
69
70 11
        return $this->connection->execute($query, ['_id' => $id]);
71
    }
72
73 2
    protected function buildDeleteQuery(Storable $entity): Cursor
74
    {
75 2
        $query = sprintf(
76 2
            'DELETE FROM %s WHERE %s = :_id',
77 2
            $this->metaData->getSource(),
78 2
            $this->metaData->getIdentifier()->getFieldName()
79
        );
80 2
        return $this->connection->execute($query, ['_id' => $entity->getId()]);
81
    }
82
83 2
    protected function buildCreateQuery(Storable $entity): Cursor
84
    {
85 2
        $data = $this->hydrator->extract($entity);
86 2
        $fields = array_keys($data);
87 2
        $binds = [];
88 2
        $columns = [];
89 2
        foreach ($fields as $columnName) {
90 2
            $columns[] = "\"${columnName}\"";
91 2
            $binds[] = ":${columnName}";
92
        }
93 2
        $sql = sprintf(
94 2
            'INSERT INTO %s (%s) VALUES(%s)',
95 2
            $this->metaData->getSource(),
96 2
            implode(',', $columns),
97 2
            implode(',', $binds)
98
        );
99 2
        return $this->connection->execute($sql, $data);
100
    }
101
102
    protected function buildUpdateQuery(Storable $entity): Cursor
103
    {
104 2
        $data = $this->hydrator->extract($entity);
105 2
        $fields = array_keys($data);
106 2
        $columns = [];
107 2
        foreach ($fields as $columnName) {
108 2
            $columns[] = "\"${columnName}\" = :${columnName}";
109
        }
110 2
        $sql = sprintf(
111 2
            'UPDATE %s SET %s WHERE %s = :_id',
112 2
            $this->metaData->getSource(),
113 2
            implode(', ', $columns),
114 2
            $this->metaData->getIdentifier()->getFieldName()
115
        );
116 2
        return $this->connection->execute($sql, array_merge($data, ['_id' => $entity->getId()]));
117
    }
118
}
119