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

CountryRepository::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

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