Completed
Push — development ( b06117...356f74 )
by Thomas
17s
created

htdocs/src/Oc/Country/CountryRepository.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
class CountryRepository
14
{
15
    /**
16
     * Database table name that this repository maintains.
17
     *
18
     * @var string
19
     */
20
    const TABLE = 'countries';
21
22
    /**
23
     * @var Connection
24
     */
25
    private $connection;
26
27
    /**
28
     * CountryRepository constructor.
29
     *
30
     * @param Connection $connection
31
     */
32
    public function __construct(Connection $connection)
33
    {
34
        $this->connection = $connection;
35
    }
36
37
    /**
38
     * Fetches all countries.
39
     *
40
     * @throws RecordsNotFoundException Thrown when no records are found
41
     * @return CountryEntity[]
42
     */
43 View Code Duplication
    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 ($statement->rowCount() === 0) {
53
            throw new RecordsNotFoundException('No records found');
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
     * @throws RecordAlreadyExistsException
71
     * @return CountryEntity
72
     */
73 View Code Duplication
    public function create(CountryEntity $entity)
0 ignored issues
show
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...
74
    {
75
        if (!$entity->isNew()) {
76
            throw new RecordAlreadyExistsException('The entity does already exist.');
77
        }
78
79
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
80
81
        $this->connection->insert(
82
            self::TABLE,
83
            $databaseArray
84
        );
85
86
        $entity->short = $this->connection->lastInsertId();
87
88
        return $entity;
89
    }
90
91
    /**
92
     * Update a country in the database.
93
     *
94
     * @param CountryEntity $entity
95
     *
96
     * @throws RecordNotPersistedException
97
     * @return CountryEntity
98
     */
99 View Code Duplication
    public function update(CountryEntity $entity)
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
     * @throws RecordNotPersistedException
124
     * @return CountryEntity
125
     */
126 View Code Duplication
    public function remove(CountryEntity $entity)
127
    {
128
        if ($entity->isNew()) {
129
            throw new RecordNotPersistedException('The entity does not exist.');
130
        }
131
132
        $this->connection->delete(
133
            self::TABLE,
134
            ['short' => $entity->short]
135
        );
136
137
        $entity->short = null;
138
139
        return $entity;
140
    }
141
142
    /**
143
     * Maps the given entity to the database array.
144
     *
145
     * @param CountryEntity $entity
146
     *
147
     * @return array
148
     */
149 View Code Duplication
    public function getDatabaseArrayFromEntity(CountryEntity $entity)
150
    {
151
        return [
152
            'short' => $entity->short,
153
            'name' => $entity->name,
154
            'de' => $entity->de,
155
            'en' => $entity->en,
156
            'trans_id' => $entity->translationId,
157
            'list_default_de' => $entity->listDefaultDe,
158
            'list_default_en' => $entity->listDefaultEn,
159
            'sort_de' => $entity->sortDe,
160
            'sort_en' => $entity->sortEn,
161
        ];
162
    }
163
164
    /**
165
     * Prepares database array from properties.
166
     *
167
     * @param array $data
168
     *
169
     * @return CountryEntity
170
     */
171 View Code Duplication
    public function getEntityFromDatabaseArray(array $data)
172
    {
173
        $entity = new CountryEntity();
174
        $entity->short = $data['short'];
175
        $entity->name = $data['name'];
176
        $entity->de = $data['de'];
177
        $entity->en = $data['en'];
178
        $entity->translationId = (int) $data['trans_id'];
179
        $entity->listDefaultDe = (bool) $data['list_default_de'];
180
        $entity->listDefaultEn = (bool) $data['list_default_en'];
181
        $entity->sortDe = $data['sort_de'];
182
        $entity->sortEn = $data['sort_en'];
183
184
        return $entity;
185
    }
186
}
187