Completed
Pull Request — development (#546)
by Nick
07:54 queued 01:05
created

UserRepository::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\User;
4
5
use Doctrine\DBAL\Connection;
6
7
/**
8
 * Class UserRepository
9
 *
10
 * @package Oc\User
11
 * @author Nick Lubisch <[email protected]>
12
 */
13
class UserRepository
14
{
15
    /**
16
     * Database table name that this repository maintains.
17
     *
18
     * @var string
19
     */
20
    const TABLE = 'user';
21
22
    /**
23
     * @var Connection
24
     */
25
    private $connection;
26
27
    /**
28
     * UserRepository constructor.
29
     *
30
     * @param Connection $connection
31
     */
32
    public function __construct(Connection $connection)
33
    {
34
        $this->connection = $connection;
35
    }
36
37
    /**
38
     * Fetches all users.
39
     *
40
     * @return UserEntity[]
41
     */
42 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...
43
    {
44
        $statement = $this->connection->createQueryBuilder()
45
            ->select('*')
46
            ->from(self::TABLE)
47
            ->execute();
48
49
        $result = $statement->fetchAll();
50
51
        if ($result === false) {
52
            return [];
53
        }
54
55
        return $this->getEntityArrayFromDatabaseArray($result);
56
    }
57
58
    /**
59
     * Fetches a user by its id.
60
     *
61
     * @param int $id
62
     *
63
     * @return null|UserEntity
64
     */
65
    public function fetchOneById($id)
66
    {
67
        $statement = $this->connection->createQueryBuilder()
68
            ->select('*')
69
            ->from(self::TABLE)
70
            ->where('user_id = :id')
71
            ->setParameter(':id', $id)
72
            ->execute();
73
74
        $result = $statement->fetch();
75
76
        if ($result === false) {
77
            return null;
78
        }
79
80
        $entity = new UserEntity();
81
82
        return $entity->fromDatabaseArray($result);
83
    }
84
85
    /**
86
     * Creates a user in the database.
87
     *
88
     * @param UserEntity $entity
89
     *
90
     * @return UserEntity
91
     */
92
    public function create(UserEntity $entity)
93
    {
94
        if (!$entity->isNew()) {
95
            throw new InvalidArgumentException('The entity does already exist.');
96
        }
97
98
        $this->connection->insert(
99
            self::TABLE,
100
            $entity->toDatabaseArray()
101
        );
102
103
        $entity->id = (int) $this->connection->lastInsertId();
104
105
        return $entity;
106
    }
107
108
    /**
109
     * Update a user in the database.
110
     *
111
     * @param UserEntity $entity
112
     *
113
     * @return UserEntity
114
     */
115 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...
116
    {
117
        if ($entity->isNew()) {
118
            throw new \InvalidArgumentException('The entity does not exist.');
119
        }
120
121
        $this->connection->update(
122
            self::TABLE,
123
            $entity->toDatabaseArray(),
124
            ['id' => $entity->id]
125
        );
126
127
        $entity->id = (int) $this->connection->lastInsertId();
128
129
        return $entity;
130
    }
131
132
    /**
133
     * Removes a user from the database.
134
     *
135
     * @param UserEntity $entity
136
     *
137
     * @return UserEntity
138
     */
139 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...
140
    {
141
        if ($entity->isNew()) {
142
            throw new \InvalidArgumentException('The entity does not exist.');
143
        }
144
145
        $this->connection->delete(
146
            self::TABLE,
147
            $entity->toDatabaseArray(),
148
            ['id' => $entity->id]
149
        );
150
151
        $entity->id = null;
152
153
        return $entity;
154
    }
155
156
    /**
157
     * Converts database array to entity array.
158
     *
159
     * @param array $result
160
     *
161
     * @return UserEntity[]
162
     */
163
    private function getEntityArrayFromDatabaseArray(array $result)
164
    {
165
        $languages = [];
166
167
        foreach ($result as $item) {
168
            $languages[] = (new UserEntity())->fromDatabaseArray($item);
169
        }
170
171
        return $languages;
172
    }
173
}
174