Completed
Pull Request — development (#546)
by Nick
07:54 queued 01:05
created

getEntityArrayFromDatabaseArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
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
8
/**
9
 * Class LanguageRepository
10
 *
11
 * @package Oc\Language
12
 * @author Nick Lubisch <[email protected]>
13
 */
14
class LanguageRepository
15
{
16
    /**
17
     * Database table name that this repository maintains.
18
     *
19
     * @var string
20
     */
21
    const TABLE = 'languages';
22
23
    /**
24
     * @var Connection
25
     */
26
    private $connection;
27
28
    /**
29
     * LanguageRepository constructor.
30
     *
31
     * @param Connection $connection
32
     */
33
    public function __construct(Connection $connection)
34
    {
35
        $this->connection = $connection;
36
    }
37
38
    /**
39
     * Fetches all languages.
40
     *
41
     * @return LanguageEntity[]
42
     */
43 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...
44
    {
45
        $statement = $this->connection->createQueryBuilder()
46
            ->select('*')
47
            ->from(self::TABLE)
48
            ->execute();
49
50
        $result = $statement->fetchAll();
51
52
        if ($result === false) {
53
            return [];
54
        }
55
56
        return $this->getEntityArrayFromDatabaseArray($result);
57
    }
58
59
    /**
60
     * Fetches all translated languages.
61
     *
62
     * @return LanguageEntity[]
63
     */
64 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...
65
    {
66
        $statement = $this->connection->createQueryBuilder()
67
            ->select('*')
68
            ->from(self::TABLE)
69
            ->where('is_translated = 1')
70
            ->execute();
71
72
        $result = $statement->fetchAll();
73
74
        if ($result === false) {
75
            return null;
76
        }
77
78
        return $this->getEntityArrayFromDatabaseArray($result);
79
    }
80
81
    /**
82
     * Creates a language in the database.
83
     *
84
     * @param LanguageEntity $entity
85
     *
86
     * @return LanguageEntity
87
     */
88 View Code Duplication
    public function create(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...
89
    {
90
        if (!$entity->isNew()) {
91
            throw new InvalidArgumentException('The entity does already exist.');
92
        }
93
94
        $this->connection->insert(
95
            self::TABLE,
96
            $entity->toDatabaseArray()
97
        );
98
99
        $entity->short = $this->connection->lastInsertId();
100
101
        return $entity;
102
    }
103
104
    /**
105
     * Update a language in the database.
106
     *
107
     * @param LanguageEntity $entity
108
     *
109
     * @return LanguageEntity
110
     */
111 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...
112
    {
113
        if ($entity->isNew()) {
114
            throw new \InvalidArgumentException('The entity does not exist.');
115
        }
116
117
        $this->connection->update(
118
            self::TABLE,
119
            $entity->toDatabaseArray(),
120
            ['short' => $entity->short]
121
        );
122
123
        $entity->short = $this->connection->lastInsertId();
124
125
        return $entity;
126
    }
127
128
    /**
129
     * Removes a language from the database.
130
     *
131
     * @param LanguageEntity $entity
132
     *
133
     * @return LanguageEntity
134
     */
135 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...
136
    {
137
        if ($entity->isNew()) {
138
            throw new \InvalidArgumentException('The entity does not exist.');
139
        }
140
141
        $this->connection->delete(
142
            self::TABLE,
143
            $entity->toDatabaseArray(),
144
            ['short' => $entity->short]
145
        );
146
147
        $entity->short = null;
148
149
        return $entity;
150
    }
151
152
    /**
153
     * Converts database array to entity array.
154
     *
155
     * @param array $result
156
     *
157
     * @return LanguageEntity[]
158
     */
159
    private function getEntityArrayFromDatabaseArray(array $result)
160
    {
161
        $languages = [];
162
163
        foreach ($result as $item) {
164
            $languages[] = (new LanguageEntity())->fromDatabaseArray($item);
165
        }
166
167
        return $languages;
168
    }
169
}
170