Test Setup Failed
Push — master ( 948172...201d68 )
by Fernando
05:02
created

UserRepository::updateUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 14
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 25
ccs 15
cts 15
cp 1
crap 1
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Entity\User;
8
9
final class UserRepository extends BaseRepository
10
{
11
    public function checkAndGetUser(int $userId): User
12 6
    {
13
        $query = 'SELECT `id`, `name`, `email` FROM `users` WHERE `id` = :id';
14 6
        $statement = $this->getDb()->prepare($query);
15 6
        $statement->bindParam('id', $userId);
16 6
        $statement->execute();
17 6
        $user = $statement->fetchObject(User::class);
18 6
        if (!$user) {
19 6
            throw new \App\Exception\UserException('User not found.', 404);
20 1
        }
21
22
        return $user;
23 5
    }
24
25
    public function checkUserByEmail(string $email): void
26 2
    {
27
        $query = 'SELECT * FROM `users` WHERE `email` = :email';
28 2
        $statement = $this->getDb()->prepare($query);
29 2
        $statement->bindParam('email', $email);
30 2
        $statement->execute();
31 2
        $user = $statement->fetchObject();
32 2
        if ($user) {
33 2
            throw new \App\Exception\UserException('Email already exists.', 400);
34 1
        }
35
    }
36 1
37
    public function getAllUsers(): array
38 1
    {
39
        $query = 'SELECT `id`, `name`, `email` FROM `users` ORDER BY `id`';
40 1
        $statement = $this->getDb()->prepare($query);
41 1
        $statement->execute();
42 1
43
        return (array) $statement->fetchAll();
44 1
    }
45
46
    public function getQueryUsersByPage(): string
47 2
    {
48
        return "
49 2
            SELECT `id`, `name`, `email`
50
            FROM `users`
51
            WHERE `name` LIKE CONCAT('%', :name, '%')
52
            AND `email` LIKE CONCAT('%', :email, '%')
53
            ORDER BY `id`
54
        ";
55
    }
56
57
    public function getUsersByPage(
58 2
        int $page,
59
        int $perPage,
60
        ?string $name,
61
        ?string $email
62
    ): array {
63
        $params = [
64
            'name' => is_null($name) ? '' : $name,
65 2
            'email' => is_null($email) ? '' : $email,
66 2
        ];
67
        $query = $this->getQueryUsersByPage();
68 2
        $statement = $this->getDb()->prepare($query);
69 2
        $statement->bindParam('name', $params['name']);
70 2
        $statement->bindParam('email', $params['email']);
71 2
        $statement->execute();
72 2
        $total = $statement->rowCount();
73 2
74
        return $this->getResultsWithPagination(
75 2
            $query,
76 2
            $page,
77
            $perPage,
78
            $params,
79
            $total
80
        );
81
    }
82
83
    public function search(string $usersName): array
84 2
    {
85
        $query = '
86 2
            SELECT `id`, `name`, `email`
87
            FROM `users`
88
            WHERE `name` LIKE :name
89
            ORDER BY `id`
90
        ';
91
        $name = '%' . $usersName . '%';
92 2
        $statement = $this->getDb()->prepare($query);
93 2
        $statement->bindParam('name', $name);
94 2
        $statement->execute();
95 2
        $users = $statement->fetchAll();
96 2
        if (!$users) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $users of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97 2
            throw new \App\Exception\UserException('User name not found.', 404);
98 1
        }
99
100
        return $users;
101 1
    }
102
103
    public function create(User $user): User
104 1
    {
105
        $query = '
106 1
            INSERT INTO `users`
107
                (`name`, `email`, `password`, `createdAt`)
108
            VALUES
109
                (:name, :email, :password, :createdAt)
110
        ';
111
        $statement = $this->getDb()->prepare($query);
112 1
        $name = $user->getName();
113 1
        $email = $user->getEmail();
114 1
        $password = $user->getPassword();
115 1
        $created = $user->getCreatedAt();
116 1
        $statement->bindParam('name', $name);
117 1
        $statement->bindParam('email', $email);
118 1
        $statement->bindParam('password', $password);
119 1
        $statement->bindParam('createdAt', $created);
120 1
        $statement->execute();
121 1
122
        return $this->checkAndGetUser((int) $this->getDb()->lastInsertId());
123 1
    }
124
125
    public function update(User $user): User
126 1
    {
127
        $query = '
128 1
            UPDATE `users` 
129
            SET 
130
                `name` = :name, 
131
                `email` = :email, 
132
                `updatedAt` = :updatedAt 
133
            WHERE `id` = :id
134
        ';
135
        $statement = $this->getDb()->prepare($query);
136
        $id = $user->getId();
137 1
        $name = $user->getName();
138 1
        $email = $user->getEmail();
139 1
        $updated = $user->getUpdatedAt();
140 1
        $statement->bindParam('id', $id);
141 1
        $statement->bindParam('name', $name);
142 1
        $statement->bindParam('email', $email);
143 1
        $statement->bindParam('updatedAt', $updated);
144 1
        $statement->execute();
145 1
146 1
        return $this->checkAndGetUser((int) $id);
147 1
    }
148 1
149
    public function delete(int $userId): void
150 1
    {
151
        $query = 'DELETE FROM `users` WHERE `id` = :id';
152
        $statement = $this->getDb()->prepare($query);
153 1
        $statement->bindParam('id', $userId);
154
        $statement->execute();
155 1
    }
156 1
157 1
    public function deleteUserTasks(int $userId): void
158 1
    {
159 1
        $query = 'DELETE FROM `tasks` WHERE `userId` = :userId';
160
        $statement = $this->getDb()->prepare($query);
161 1
        $statement->bindParam('userId', $userId);
162
        $statement->execute();
163 1
    }
164 1
165 1
    public function login(string $email, string $password): User
166 1
    {
167 1
        $query = '
168
            SELECT *
169 4
            FROM `users`
170
            WHERE `email` = :email AND `password` = :password
171 4
            ORDER BY `id`
172
        ';
173
        $statement = $this->getDb()->prepare($query);
174
        $statement->bindParam('email', $email);
175
        $statement->bindParam('password', $password);
176
        $statement->execute();
177 4
        $user = $statement->fetchObject(User::class);
178 4
        if (!$user) {
179 4
            throw new \App\Exception\UserException('Login failed: Email or password incorrect.', 400);
180 4
        }
181 4
182 4
        return $user;
183 1
    }
184
}
185