Passed
Push — master ( 583647...efdb33 )
by Dawid
02:55
created

Repository::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

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