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

PageRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 180
Duplicated Lines 36.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

7 Methods

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