Completed
Pull Request — development (#546)
by Nick
06:40
created

BlockRepository::fetchBy()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 28
Code Lines 15

Duplication

Lines 5
Ratio 17.86 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 6
nop 1
dl 5
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\Page;
4
5
use DateTime;
6
use Doctrine\DBAL\Connection;
7
use Oc\Repository\Exception\RecordAlreadyExistsException;
8
use Oc\Repository\Exception\RecordNotPersistedException;
9
use Oc\Repository\Exception\RecordsNotFoundException;
10
11
/**
12
 * Class BlockRepository
13
 *
14
 * @package Oc\Page
15
 */
16
class BlockRepository
17
{
18
    /**
19
     * Database table name that this repository maintains.
20
     *
21
     * @var string
22
     */
23
    const TABLE = 'page_block';
24
25
    /**
26
     * @var Connection
27
     */
28
    private $connection;
29
30
    /**
31
     * BlockRepository constructor.
32
     *
33
     * @param Connection $connection
34
     */
35
    public function __construct(Connection $connection)
36
    {
37
        $this->connection = $connection;
38
    }
39
40
    /**
41
     * Fetches all blocks of a page.
42
     *
43
     * @param array $where
44
     *
45
     * @return BlockEntity[]
46
     *
47
     * @throws RecordsNotFoundException Thrown when no records are found
48
     */
49
    public function fetchBy(array $where = [])
50
    {
51
        $queryBuilder = $this->connection->createQueryBuilder()
52
            ->select('*')
53
            ->from(self::TABLE);
54
55 View Code Duplication
        if (count($where) > 0) {
56
            foreach ($where as $column => $value) {
57
                $queryBuilder->andWhere($column . ' = ' .  $queryBuilder->createNamedParameter($value));
58
            }
59
        }
60
61
        $statement = $queryBuilder->execute();
62
63
        $result = $statement->fetchAll();
64
65
        if ($statement->rowCount() === 0) {
66
            throw new RecordsNotFoundException('No records with given where clause found');
67
        }
68
69
        $blocks = [];
70
71
        foreach ($result as $item) {
72
            $blocks[] = $this->getEntityFromDatabaseArray($item);
73
        }
74
75
        return $blocks;
76
    }
77
78
    /**
79
     * Creates a block in the database.
80
     *
81
     * @param BlockEntity $entity
82
     *
83
     * @return BlockEntity
84
     *
85
     * @throws RecordAlreadyExistsException
86
     */
87
    public function create(BlockEntity $entity)
88
    {
89
        if (!$entity->isNew()) {
90
            throw new RecordAlreadyExistsException('The entity does already exist.');
91
        }
92
93
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
94
95
        $this->connection->insert(
96
            self::TABLE,
97
            $databaseArray
98
        );
99
100
        $entity->id = (int) $this->connection->lastInsertId();
101
    }
102
103
    /**
104
     * Update a page in the database.
105
     *
106
     * @param BlockEntity $entity
107
     *
108
     * @return BlockEntity
109
     *
110
     * @throws RecordNotPersistedException
111
     */
112 View Code Duplication
    public function update(BlockEntity $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        if ($entity->isNew()) {
115
            throw new RecordNotPersistedException('The entity does not exist.');
116
        }
117
118
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
119
120
        $this->connection->update(
121
            self::TABLE,
122
            $databaseArray,
123
            ['id' => $entity->id]
124
        );
125
126
        $entity->id = (int) $this->connection->lastInsertId();
127
128
        return $entity;
129
    }
130
131
    /**
132
     * Removes a page from the database.
133
     *
134
     * @param BlockEntity $entity
135
     *
136
     * @return BlockEntity
137
     *
138
     * @throws RecordNotPersistedException
139
     */
140 View Code Duplication
    public function remove(BlockEntity $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        if ($entity->isNew()) {
143
            throw new RecordNotPersistedException('The entity does not exist.');
144
        }
145
146
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
147
148
        $this->connection->delete(
149
            self::TABLE,
150
            $databaseArray,
151
            ['id' => $entity->id]
152
        );
153
154
        $entity->id = null;
155
156
        return $entity;
157
    }
158
159
    /**
160
     * Maps the given entity to the database array.
161
     *
162
     * @param BlockEntity $entity
163
     *
164
     * @return array
165
     */
166 View Code Duplication
    public function getDatabaseArrayFromEntity(BlockEntity $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        return [
169
            'id' => $entity->id,
170
            'page_id' => $entity->pageId,
171
            'title' => $entity->title,
172
            'html' => $entity->html,
173
            'position' => (int) $entity->position,
174
            'updated_at' => $entity->updatedAt->format(DateTime::ATOM),
175
            'active' => $entity->active
176
        ];
177
    }
178
179
    /**
180
     * Prepares database array from properties.
181
     *
182
     * @param array $data
183
     *
184
     * @return BlockEntity
185
     */
186 View Code Duplication
    public function getEntityFromDatabaseArray(array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        $entity = new BlockEntity();
189
        $entity->id = (int) $data['id'];
190
        $entity->pageId = (int) $data['page_id'];
191
        $entity->title = (string) $data['title'];
192
        $entity->html = (string) $data['html'];
193
        $entity->position = (int) $data['position'];
194
        $entity->updatedAt = new DateTime($data['updated_at']);
195
        $entity->active = (bool) $data['active'];
196
197
        return $entity;
198
    }
199
}
200