Completed
Push — development ( b626b0...ff940a )
by Thomas
16s
created

PageRepository::fetchOneBy()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 24
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\Page\Persistence;
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
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
     * @throws RecordNotFoundException Thrown when no record is found
44
     * @return null|PageEntity
45
     */
46 View Code Duplication
    public function fetchOneBy(array $where = [])
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...
47
    {
48
        $queryBuilder = $this->connection->createQueryBuilder()
49
            ->select('*')
50
            ->from(self::TABLE)
51
            ->setMaxResults(1);
52
53
54
        if (count($where) > 0) {
55
            foreach ($where as $column => $value) {
56
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
57
            }
58
        }
59
60
        $statement = $queryBuilder->execute();
61
62
        $result = $statement->fetch();
63
64
        if ($statement->rowCount() === 0) {
65
            throw new RecordNotFoundException('Record with given where clause not found');
66
        }
67
68
        return $this->getEntityFromDatabaseArray($result);
69
    }
70
71
    /**
72
     * Creates a page in the database.
73
     *
74
     * @param PageEntity $entity
75
     *
76
     * @throws RecordAlreadyExistsException
77
     * @return PageEntity
78
     */
79 View Code Duplication
    public function create(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...
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
     * @throws RecordNotPersistedException
103
     * @return PageEntity
104
     */
105
    public function update(PageEntity $entity)
106
    {
107
        if ($entity->isNew()) {
108
            throw new RecordNotPersistedException('The entity does not exist.');
109
        }
110
111
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
112
113
        $this->connection->update(
114
            self::TABLE,
115
            $databaseArray,
116
            ['id' => $entity->id]
117
        );
118
119
        $entity->id = (int) $this->connection->lastInsertId();
120
121
        return $entity;
122
    }
123
124
    /**
125
     * Removes a page from the database.
126
     *
127
     * @param PageEntity $entity
128
     *
129
     * @throws RecordNotPersistedException
130
     * @return PageEntity
131
     */
132 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...
133
    {
134
        if ($entity->isNew()) {
135
            throw new RecordNotPersistedException('The entity does not exist.');
136
        }
137
138
        $this->connection->delete(
139
            self::TABLE,
140
            ['id' => $entity->id]
141
        );
142
143
        $entity->id = null;
144
145
        return $entity;
146
    }
147
148
    /**
149
     * Maps the given entity to the database array.
150
     *
151
     * @param PageEntity $entity
152
     *
153
     * @return array
154
     */
155 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...
156
    {
157
        return [
158
            'id' => $entity->id,
159
            'slug' => $entity->slug,
160
            'meta_keywords' => $entity->metaKeywords,
161
            'meta_description' => $entity->metaDescription,
162
            'meta_social' => $entity->metaSocial,
163
            'updated_at' => $entity->updatedAt->format(DateTime::ATOM),
164
            'active' => $entity->active,
165
        ];
166
    }
167
168
    /**
169
     * Prepares database array from properties.
170
     *
171
     * @param array $data
172
     *
173
     * @return PageEntity
174
     */
175 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...
176
    {
177
        $entity = new PageEntity();
178
        $entity->id = (int) $data['id'];
179
        $entity->slug = $data['slug'];
180
        $entity->metaKeywords = $data['meta_keywords'];
181
        $entity->metaDescription = $data['meta_description'];
182
        $entity->metaSocial = $data['meta_social'];
183
        $entity->updatedAt = new DateTime($data['updated_at']);
184
        $entity->active = (bool) $data['active'];
185
186
        return $entity;
187
    }
188
}
189