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

LanguageRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 156
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 78
loc 156
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 15 15 2
A fetchAllTranslated() 16 16 2
A update() 16 16 2
A remove() 16 16 2
A getEntityArrayFromDatabaseArray() 0 10 2
A create() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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