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

UserRepository::fetchOneById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 19
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 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 a user by its id.
61
     *
62
     * @param int $id
63
     *
64
     * @return null|UserEntity
65
     */
66
    public function fetchOneById($id)
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
        $entity = new UserEntity();
82
83
        return $entity->fromDatabaseArray($result);
84
    }
85
86
    /**
87
     * Creates a user in the database.
88
     *
89
     * @param UserEntity $entity
90
     *
91
     * @return UserEntity
92
     *
93
     * @throws RecordAlreadyExistsException
94
     */
95
    public function create(UserEntity $entity)
96
    {
97
        if (!$entity->isNew()) {
98
            throw new RecordAlreadyExistsException('The user entity already exists');
99
        }
100
101
        $this->connection->insert(
102
            self::TABLE,
103
            $entity->toDatabaseArray()
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
        $this->connection->update(
127
            self::TABLE,
128
            $entity->toDatabaseArray(),
129
            ['id' => $entity->id]
130
        );
131
132
        $entity->id = (int) $this->connection->lastInsertId();
133
134
        return $entity;
135
    }
136
137
    /**
138
     * Removes a user from the database.
139
     *
140
     * @param UserEntity $entity
141
     *
142
     * @return UserEntity
143
     *
144
     * @throws RecordNotPersistedException
145
     */
146 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...
147
    {
148
        if ($entity->isNew()) {
149
            throw new RecordNotPersistedException('The entity does not exist.');
150
        }
151
152
        $this->connection->delete(
153
            self::TABLE,
154
            $entity->toDatabaseArray(),
155
            ['id' => $entity->id]
156
        );
157
158
        $entity->id = null;
159
160
        return $entity;
161
    }
162
163
    /**
164
     * Converts database array to entity array.
165
     *
166
     * @param array $result
167
     *
168
     * @return UserEntity[]
169
     */
170
    private function getEntityArrayFromDatabaseArray(array $result)
171
    {
172
        $languages = [];
173
174
        foreach ($result as $item) {
175
            $languages[] = (new UserEntity())->fromDatabaseArray($item);
176
        }
177
178
        return $languages;
179
    }
180
}
181