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

CountryRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 179
Duplicated Lines 36.31 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 65
loc 179
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 0 21 3
A create() 0 15 2
A update() 18 18 2
A remove() 18 18 2
A getDatabaseArrayFromEntity() 14 14 1
A getEntityFromDatabaseArray() 15 15 1

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\Country;
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 CountryRepository
12
 *
13
 * @package Oc\Country
14
 */
15
class CountryRepository
16
{
17
    /**
18
     * Database table name that this repository maintains.
19
     *
20
     * @var string
21
     */
22
    const TABLE = 'countries';
23
24
    /**
25
     * @var Connection
26
     */
27
    private $connection;
28
29
    /**
30
     * CountryRepository constructor.
31
     *
32
     * @param Connection $connection
33
     */
34
    public function __construct(Connection $connection)
35
    {
36
        $this->connection = $connection;
37
    }
38
39
    /**
40
     * Fetches all countries.
41
     *
42
     * @return CountryEntity[]
43
     *
44
     * @throws RecordsNotFoundException Thrown when no records are found
45
     */
46
    public function fetchAll()
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
        $countries = [];
60
61
        foreach ($result as $item) {
62
            $countries[] = $this->getEntityFromDatabaseArray($item);
63
        }
64
65
        return $countries;
66
    }
67
68
    /**
69
     * Creates a country in the database.
70
     *
71
     * @param CountryEntity $entity
72
     *
73
     * @return CountryEntity
74
     *
75
     * @throws RecordAlreadyExistsException
76
     */
77
    public function create(CountryEntity $entity)
78
    {
79
        if (!$entity->isNew()) {
80
            throw new RecordAlreadyExistsException('The entity does already exist.');
81
        }
82
83
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
84
85
        $this->connection->insert(
86
            self::TABLE,
87
            $databaseArray
88
        );
89
90
        $entity->short = $this->connection->lastInsertId();
91
    }
92
93
    /**
94
     * Update a country in the database.
95
     *
96
     * @param CountryEntity $entity
97
     *
98
     * @return CountryEntity
99
     *
100
     * @throws RecordNotPersistedException
101
     */
102 View Code Duplication
    public function update(CountryEntity $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...
103
    {
104
        if ($entity->isNew()) {
105
            throw new RecordNotPersistedException('The entity does not exist.');
106
        }
107
108
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
109
110
        $this->connection->update(
111
            self::TABLE,
112
            $databaseArray,
113
            ['short' => $entity->short]
114
        );
115
116
        $entity->short = $this->connection->lastInsertId();
117
118
        return $entity;
119
    }
120
121
    /**
122
     * Removes a country from the database.
123
     *
124
     * @param CountryEntity $entity
125
     *
126
     * @return CountryEntity
127
     *
128
     * @throws RecordNotPersistedException
129
     */
130 View Code Duplication
    public function remove(CountryEntity $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...
131
    {
132
        if ($entity->isNew()) {
133
            throw new RecordNotPersistedException('The entity does not exist.');
134
        }
135
136
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
137
138
        $this->connection->delete(
139
            self::TABLE,
140
            $databaseArray,
141
            ['short' => $entity->short]
142
        );
143
144
        $entity->short = null;
145
146
        return $entity;
147
    }
148
149
    /**
150
     * Maps the given entity to the database array.
151
     *
152
     * @param CountryEntity $entity
153
     *
154
     * @return array
155
     */
156 View Code Duplication
    public function getDatabaseArrayFromEntity(CountryEntity $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...
157
    {
158
        return [
159
            'short' => $entity->short,
160
            'name' => $entity->name,
161
            'de' => $entity->de,
162
            'en' => $entity->en,
163
            'trans_id' => $entity->translationId,
164
            'list_default_de' => $entity->listDefaultDe,
165
            'list_default_en' => $entity->listDefaultEn,
166
            'sort_de' => $entity->sortDe,
167
            'sort_en' => $entity->sortEn
168
        ];
169
    }
170
171
    /**
172
     * Prepares database array from properties.
173
     *
174
     * @param array $data
175
     *
176
     * @return CountryEntity
177
     */
178 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...
179
    {
180
        $entity = new CountryEntity();
181
        $entity->short = (string) $data['short'];
182
        $entity->name = (string) $data['name'];
183
        $entity->de = (string) $data['de'];
184
        $entity->en = (string) $data['en'];
185
        $entity->translationId = (int) $data['trans_id'];
186
        $entity->listDefaultDe = (bool) $data['list_default_de'];
187
        $entity->listDefaultEn = (bool) $data['list_default_en'];
188
        $entity->sortDe = (string) $data['sort_de'];
189
        $entity->sortEn = (string) $data['sort_en'];
190
191
        return $entity;
192
    }
193
}
194