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

PageRepository::fetchOneBy()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 5
Ratio 20.83 %

Importance

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