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

LanguageRepository::fetchAllTranslated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\Language;
4
5
use Doctrine\DBAL\Connection;
6
use Oc\Repository\Exception\RecordAlreadyExistsException;
7
use Oc\Repository\Exception\RecordNotPersistedException;
8
use Oc\Repository\Exception\RecordsNotFoundException;
9
10
/**
11
 * Class LanguageRepository
12
 *
13
 * @package Oc\Language
14
 */
15
class LanguageRepository
16
{
17
    /**
18
     * Database table name that this repository maintains.
19
     *
20
     * @var string
21
     */
22
    const TABLE = 'languages';
23
24
    /**
25
     * @var Connection
26
     */
27
    private $connection;
28
29
    /**
30
     * LanguageRepository constructor.
31
     *
32
     * @param Connection $connection
33
     */
34
    public function __construct(Connection $connection)
35
    {
36
        $this->connection = $connection;
37
    }
38
39
    /**
40
     * Fetches all languages.
41
     *
42
     * @return LanguageEntity[]
43
     *
44
     * @throws RecordsNotFoundException Thrown when no records are found
45
     */
46 View Code Duplication
    public function fetchAll()
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
        $statement = $this->connection->createQueryBuilder()
49
            ->select('*')
50
            ->from(self::TABLE)
51
            ->execute();
52
53
        $result = $statement->fetchAll();
54
55
        if ($statement->rowCount() === 0) {
56
            throw new RecordsNotFoundException('No records found');
57
        }
58
59
        return $this->getEntityArrayFromDatabaseArray($result);
60
    }
61
62
    /**
63
     * Fetches all translated languages.
64
     *
65
     * @return LanguageEntity[]
66
     *
67
     * @throws RecordsNotFoundException Thrown when no records are found
68
     */
69 View Code Duplication
    public function fetchAllTranslated()
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...
70
    {
71
        $statement = $this->connection->createQueryBuilder()
72
            ->select('*')
73
            ->from(self::TABLE)
74
            ->where('is_translated = 1')
75
            ->execute();
76
77
        $result = $statement->fetchAll();
78
79
        if ($statement->rowCount() === 0) {
80
            throw new RecordsNotFoundException('No records with given where clause found');
81
        }
82
83
        return $this->getEntityArrayFromDatabaseArray($result);
84
    }
85
86
    /**
87
     * Creates a language in the database.
88
     *
89
     * @param LanguageEntity $entity
90
     *
91
     * @return LanguageEntity
92
     *
93
     * @throws RecordAlreadyExistsException
94
     */
95
    public function create(LanguageEntity $entity)
96
    {
97
        if (!$entity->isNew()) {
98
            throw new RecordAlreadyExistsException('The entity does already exist.');
99
        }
100
101
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
102
103
        $this->connection->insert(
104
            self::TABLE,
105
            $databaseArray
106
        );
107
108
        $entity->short = $this->connection->lastInsertId();
109
110
        return $entity;
111
    }
112
113
    /**
114
     * Update a language in the database.
115
     *
116
     * @param LanguageEntity $entity
117
     *
118
     * @return LanguageEntity
119
     *
120
     * @throws RecordNotPersistedException
121
     */
122 View Code Duplication
    public function update(LanguageEntity $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...
123
    {
124
        if ($entity->isNew()) {
125
            throw new RecordNotPersistedException('The entity does not exist.');
126
        }
127
128
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
129
130
        $this->connection->update(
131
            self::TABLE,
132
            $databaseArray,
133
            ['short' => $entity->short]
134
        );
135
136
        $entity->short = $this->connection->lastInsertId();
137
138
        return $entity;
139
    }
140
141
    /**
142
     * Removes a language from the database.
143
     *
144
     * @param LanguageEntity $entity
145
     *
146
     * @return LanguageEntity
147
     *
148
     * @throws RecordNotPersistedException
149
     */
150 View Code Duplication
    public function remove(LanguageEntity $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...
151
    {
152
        if ($entity->isNew()) {
153
            throw new RecordNotPersistedException('The entity does not exist.');
154
        }
155
156
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
157
158
        $this->connection->delete(
159
            self::TABLE,
160
            $databaseArray,
161
            ['short' => $entity->short]
162
        );
163
164
        $entity->short = null;
165
166
        return $entity;
167
    }
168
169
    /**
170
     * Converts database array to entity array.
171
     *
172
     * @param array $result
173
     *
174
     * @return LanguageEntity[]
175
     */
176
    private function getEntityArrayFromDatabaseArray(array $result)
177
    {
178
        $languages = [];
179
180
        foreach ($result as $item) {
181
            $languages[] = $this->getEntityFromDatabaseArray($item);
182
        }
183
184
        return $languages;
185
    }
186
187
    /**
188
     * Maps the given entity to the database array.
189
     *
190
     * @param LanguageEntity $entity
191
     *
192
     * @return array
193
     */
194 View Code Duplication
    public function getDatabaseArrayFromEntity(LanguageEntity $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...
195
    {
196
        return [
197
            'short' => $entity->short,
198
            'name' => $entity->name,
199
            'native_name' => $entity->nativeName,
200
            'de' => $entity->de,
201
            'en' => $entity->en,
202
            'trans_id' => $entity->translationId,
203
            'list_default_de' => $entity->listDefaultDe,
204
            'list_default_en' => $entity->listDefaultEn,
205
            'is_translated' => $entity->isTranslated
206
        ];
207
    }
208
209
    /**
210
     * Prepares database array from properties.
211
     *
212
     * @param array $data
213
     *
214
     * @return LanguageEntity
215
     */
216 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...
217
    {
218
        $entity = new LanguageEntity();
219
        $entity->short = (string) strtolower($data['short']);
220
        $entity->name = (string) $data['name'];
221
        $entity->nativeName = (string) $data['native_name'];
222
        $entity->de = (string) $data['de'];
223
        $entity->en = (string) $data['en'];
224
        $entity->translationId = (int) $data['trans_id'];
225
        $entity->listDefaultDe = (bool) $data['list_default_de'];
226
        $entity->listDefaultEn = (bool) $data['list_default_en'];
227
        $entity->isTranslated = (bool) $data['is_translated'];
228
229
        return $entity;
230
    }
231
}
232