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

LanguageRepository::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\Language;
4
5
use Doctrine\DBAL\Connection;
6
use InvalidArgumentException;
7
use Oc\Repository\Exception\RecordAlreadyExistsException;
8
use Oc\Repository\Exception\RecordNotPersistedException;
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 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...
45
    {
46
        $statement = $this->connection->createQueryBuilder()
47
            ->select('*')
48
            ->from(self::TABLE)
49
            ->execute();
50
51
        $result = $statement->fetchAll();
52
53
        if ($result === false) {
54
            return [];
55
        }
56
57
        return $this->getEntityArrayFromDatabaseArray($result);
58
    }
59
60
    /**
61
     * Fetches all translated languages.
62
     *
63
     * @return LanguageEntity[]
64
     */
65 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...
66
    {
67
        $statement = $this->connection->createQueryBuilder()
68
            ->select('*')
69
            ->from(self::TABLE)
70
            ->where('is_translated = 1')
71
            ->execute();
72
73
        $result = $statement->fetchAll();
74
75
        if ($result === false) {
76
            return null;
77
        }
78
79
        return $this->getEntityArrayFromDatabaseArray($result);
80
    }
81
82
    /**
83
     * Creates a language in the database.
84
     *
85
     * @param LanguageEntity $entity
86
     *
87
     * @return LanguageEntity
88
     *
89
     * @throws RecordAlreadyExistsException
90
     */
91
    public function create(LanguageEntity $entity)
92
    {
93
        if (!$entity->isNew()) {
94
            throw new RecordAlreadyExistsException('The entity does already exist.');
95
        }
96
97
        $this->connection->insert(
98
            self::TABLE,
99
            $entity->toDatabaseArray()
100
        );
101
102
        $entity->short = $this->connection->lastInsertId();
103
104
        return $entity;
105
    }
106
107
    /**
108
     * Update a language in the database.
109
     *
110
     * @param LanguageEntity $entity
111
     *
112
     * @return LanguageEntity
113
     *
114
     * @throws RecordNotPersistedException
115
     */
116 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...
117
    {
118
        if ($entity->isNew()) {
119
            throw new RecordNotPersistedException('The entity does not exist.');
120
        }
121
122
        $this->connection->update(
123
            self::TABLE,
124
            $entity->toDatabaseArray(),
125
            ['short' => $entity->short]
126
        );
127
128
        $entity->short = $this->connection->lastInsertId();
129
130
        return $entity;
131
    }
132
133
    /**
134
     * Removes a language from the database.
135
     *
136
     * @param LanguageEntity $entity
137
     *
138
     * @return LanguageEntity
139
     *
140
     * @throws RecordNotPersistedException
141
     */
142 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...
143
    {
144
        if ($entity->isNew()) {
145
            throw new RecordNotPersistedException('The entity does not exist.');
146
        }
147
148
        $this->connection->delete(
149
            self::TABLE,
150
            $entity->toDatabaseArray(),
151
            ['short' => $entity->short]
152
        );
153
154
        $entity->short = null;
155
156
        return $entity;
157
    }
158
159
    /**
160
     * Converts database array to entity array.
161
     *
162
     * @param array $result
163
     *
164
     * @return LanguageEntity[]
165
     */
166
    private function getEntityArrayFromDatabaseArray(array $result)
167
    {
168
        $languages = [];
169
170
        foreach ($result as $item) {
171
            $languages[] = (new LanguageEntity())->fromDatabaseArray($item);
172
        }
173
174
        return $languages;
175
    }
176
}
177