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

BlockRepository   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 182
Duplicated Lines 36.26 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 66
loc 182
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B fetchBy() 5 28 5
A create() 0 15 2
A update() 18 18 2
A remove() 18 18 2
A getDatabaseArrayFromEntity() 12 12 1
A getEntityFromDatabaseArray() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Oc\Page;
4
5
use Doctrine\DBAL\Connection;
6
use Oc\Repository\Exception\RecordAlreadyExistsException;
7
use Oc\Repository\Exception\RecordNotPersistedException;
8
9
/**
10
 * Class BlockRepository
11
 *
12
 * @package Oc\Page
13
 */
14
class BlockRepository
15
{
16
    /**
17
     * Database table name that this repository maintains.
18
     *
19
     * @var string
20
     */
21
    const TABLE = 'page_block';
22
23
    /**
24
     * @var Connection
25
     */
26
    private $connection;
27
28
    /**
29
     * BlockRepository constructor.
30
     *
31
     * @param Connection $connection
32
     */
33
    public function __construct(Connection $connection)
34
    {
35
        $this->connection = $connection;
36
    }
37
38
    /**
39
     * Fetches all blocks of a page.
40
     *
41
     * @param array $where
42
     *
43
     * @return BlockEntity[]
44
     */
45
    public function fetchBy(array $where = [])
46
    {
47
        $queryBuilder = $this->connection->createQueryBuilder()
48
            ->select('*')
49
            ->from(self::TABLE);
50
51 View Code Duplication
        if (count($where) > 0) {
52
            foreach ($where as $column => $value) {
53
                $queryBuilder->andWhere($column . ' = ' .  $queryBuilder->createNamedParameter($value));
54
            }
55
        }
56
57
        $statement = $queryBuilder->execute();
58
59
        $result = $statement->fetchAll();
60
61
        if ($result === false) {
62
            return [];
63
        }
64
65
        $blocks = [];
66
67
        foreach ($result as $item) {
68
            $blocks[] = $this->getEntityFromDatabaseArray($item);
69
        }
70
71
        return $blocks;
72
    }
73
74
    /**
75
     * Creates a block in the database.
76
     *
77
     * @param BlockEntity $entity
78
     *
79
     * @return BlockEntity
80
     *
81
     * @throws RecordAlreadyExistsException
82
     */
83
    public function create(BlockEntity $entity)
84
    {
85
        if (!$entity->isNew()) {
86
            throw new RecordAlreadyExistsException('The entity does already exist.');
87
        }
88
89
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
90
91
        $this->connection->insert(
92
            self::TABLE,
93
            $databaseArray
94
        );
95
96
        $entity->id = (int) $this->connection->lastInsertId();
97
    }
98
99
    /**
100
     * Update a page in the database.
101
     *
102
     * @param BlockEntity $entity
103
     *
104
     * @return BlockEntity
105
     *
106
     * @throws RecordNotPersistedException
107
     */
108 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...
109
    {
110
        if ($entity->isNew()) {
111
            throw new RecordNotPersistedException('The entity does not exist.');
112
        }
113
114
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
115
116
        $this->connection->update(
117
            self::TABLE,
118
            $databaseArray,
119
            ['id' => $entity->id]
120
        );
121
122
        $entity->id = (int) $this->connection->lastInsertId();
123
124
        return $entity;
125
    }
126
127
    /**
128
     * Removes a page from the database.
129
     *
130
     * @param BlockEntity $entity
131
     *
132
     * @return BlockEntity
133
     *
134
     * @throws RecordNotPersistedException
135
     */
136 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...
137
    {
138
        if ($entity->isNew()) {
139
            throw new RecordNotPersistedException('The entity does not exist.');
140
        }
141
142
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
143
144
        $this->connection->delete(
145
            self::TABLE,
146
            $databaseArray,
147
            ['id' => $entity->id]
148
        );
149
150
        $entity->id = null;
151
152
        return $entity;
153
    }
154
155
    /**
156
     * Maps the given entity to the database array.
157
     *
158
     * @param BlockEntity $entity
159
     *
160
     * @return array
161
     */
162 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...
163
    {
164
        return [
165
            'id' => $entity->id,
166
            'page_id' => $entity->pageId,
167
            'title' => $entity->title,
168
            'html' => $entity->html,
169
            'position' => (int) $entity->position,
170
            'updated_at' => $entity->updatedAt->format(DateTime::ATOM),
171
            'active' => $entity->active
172
        ];
173
    }
174
175
    /**
176
     * Prepares database array from properties.
177
     *
178
     * @param array $data
179
     *
180
     * @return BlockEntity
181
     */
182 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...
183
    {
184
        $entity = new BlockEntity();
185
        $entity->id = (int) $data['id'];
186
        $entity->pageId = (int) $data['page_id'];
187
        $entity->title = (string) $data['title'];
188
        $entity->html = (string) $data['html'];
189
        $entity->position = (int) $data['position'];
190
        $entity->updatedAt = new DateTime($data['updated_at']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Oc\Page\DateTime($data['updated_at']) of type object<Oc\Page\DateTime> is incompatible with the declared type object<DateTime> of property $updatedAt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
191
        $entity->active = (bool) $data['active'];
192
193
        return $entity;
194
    }
195
}
196