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

UserRepository::getEntityFromDatabaseArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\User;
4
5
use Doctrine\DBAL\Connection;
6
use Oc\Repository\Exception\RecordAlreadyExistsException;
7
use Oc\Repository\Exception\RecordNotPersistedException;
8
9
/**
10
 * Class UserRepository
11
 *
12
 * @package Oc\User
13
 */
14
class UserRepository
15
{
16
    /**
17
     * Database table name that this repository maintains.
18
     *
19
     * @var string
20
     */
21
    const TABLE = 'user';
22
23
    /**
24
     * @var Connection
25
     */
26
    private $connection;
27
28
    /**
29
     * UserRepository constructor.
30
     *
31
     * @param Connection $connection
32
     */
33
    public function __construct(Connection $connection)
34
    {
35
        $this->connection = $connection;
36
    }
37
38
    /**
39
     * Fetches all users.
40
     *
41
     * @return UserEntity[]
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
        return $this->getEntityArrayFromDatabaseArray($result);
57
    }
58
59
    /**
60
     * Fetches a user by its id.
61
     *
62
     * @param int $id
63
     *
64
     * @return null|UserEntity
65
     */
66 View Code Duplication
    public function fetchOneById($id)
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...
67
    {
68
        $statement = $this->connection->createQueryBuilder()
69
            ->select('*')
70
            ->from(self::TABLE)
71
            ->where('user_id = :id')
72
            ->setParameter(':id', $id)
73
            ->execute();
74
75
        $result = $statement->fetch();
76
77
        if ($result === false) {
78
            return null;
79
        }
80
81
        return $this->getEntityFromDatabaseArray($result);
82
    }
83
84
    /**
85
     * Creates a user in the database.
86
     *
87
     * @param UserEntity $entity
88
     *
89
     * @return UserEntity
90
     *
91
     * @throws RecordAlreadyExistsException
92
     */
93
    public function create(UserEntity $entity)
94
    {
95
        if (!$entity->isNew()) {
96
            throw new RecordAlreadyExistsException('The user entity already exists');
97
        }
98
99
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
100
101
        $this->connection->insert(
102
            self::TABLE,
103
            $databaseArray
104
        );
105
106
        $entity->id = (int) $this->connection->lastInsertId();
107
108
        return $entity;
109
    }
110
111
    /**
112
     * Update a user in the database.
113
     *
114
     * @param UserEntity $entity
115
     *
116
     * @return UserEntity
117
     *
118
     * @throws RecordNotPersistedException
119
     */
120 View Code Duplication
    public function update(UserEntity $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...
121
    {
122
        if ($entity->isNew()) {
123
            throw new RecordNotPersistedException('The entity does not exist.');
124
        }
125
126
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
127
128
        $this->connection->update(
129
            self::TABLE,
130
            $databaseArray,
131
            ['id' => $entity->id]
132
        );
133
134
        $entity->id = (int) $this->connection->lastInsertId();
135
136
        return $entity;
137
    }
138
139
    /**
140
     * Removes a user from the database.
141
     *
142
     * @param UserEntity $entity
143
     *
144
     * @return UserEntity
145
     *
146
     * @throws RecordNotPersistedException
147
     */
148 View Code Duplication
    public function remove(UserEntity $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...
149
    {
150
        if ($entity->isNew()) {
151
            throw new RecordNotPersistedException('The entity does not exist.');
152
        }
153
154
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
155
156
        $this->connection->delete(
157
            self::TABLE,
158
            $databaseArray,
159
            ['id' => $entity->id]
160
        );
161
162
        $entity->id = null;
163
164
        return $entity;
165
    }
166
167
    /**
168
     * Converts database array to entity array.
169
     *
170
     * @param array $result
171
     *
172
     * @return UserEntity[]
173
     */
174
    private function getEntityArrayFromDatabaseArray(array $result)
175
    {
176
        $languages = [];
177
178
        foreach ($result as $item) {
179
            $languages[] = $this->getEntityFromDatabaseArray($item);
180
        }
181
182
        return $languages;
183
    }
184
185
    /**
186
     * Maps the given entity to the database array.
187
     *
188
     * @param UserEntity $entity
189
     *
190
     * @return array
191
     */
192
    public function getDatabaseArrayFromEntity(UserEntity $entity)
193
    {
194
        return [
195
            'user_id' => $entity->id,
196
            'username' => $entity->username,
197
            'password' => $entity->password,
198
            'email' => $entity->email,
199
            'latitude' => $entity->latitude,
200
            'longitude' => $entity->longitude,
201
            'is_active_flag' => $entity->isActive,
202
            'first_name' => $entity->firstname,
203
            'last_name' => $entity->lastname,
204
            'country' => $entity->country,
205
            'language' => $entity->language,
206
        ];
207
    }
208
209
    /**
210
     * Prepares database array from properties.
211
     *
212
     * @param array $data
213
     *
214
     * @return UserEntity
215
     */
216
    public function getEntityFromDatabaseArray(array $data)
217
    {
218
        $entity = new UserEntity();
219
        $entity->id = (int) $data['user_id'];
220
        $entity->username = (string) $data['username'];
221
        $entity->password = (string) $data['password'];
222
        $entity->email = (string) $data['email'];
223
        $entity->latitude = (double) $data['latitude'];
224
        $entity->longitude = (double) $data['longitude'];
225
        $entity->isActive = (bool) $data['is_active_flag'];
226
        $entity->firstname = (string) $data['first_name'];
227
        $entity->lastname = (string) $data['last_name'];
228
        $entity->country = (string) $data['country'];
229
        $entity->language = strtolower($data['language']);
230
231
        return $entity;
232
    }
233
}
234