UserRepository::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Exception\UserException;
8
9
class UserRepository extends BaseRepository
10
{
11
    public function __construct(\PDO $database)
12
    {
13
        $this->database = $database;
14
    }
15
16
    public function getUser(int $userId)
17
    {
18
        $query = 'SELECT `id`, `name`, `email` FROM `users` WHERE `id` = :id';
19
        $statement = $this->database->prepare($query);
20
        $statement->bindParam('id', $userId);
21
        $statement->execute();
22
        $user = $statement->fetchObject();
23
        if (empty($user)) {
24
            throw new UserException('User not found.', 404);
25
        }
26
27
        return $user;
28
    }
29
30
    public function checkUserByEmail(string $email)
31
    {
32
        $query = 'SELECT * FROM `users` WHERE `email` = :email';
33
        $statement = $this->database->prepare($query);
34
        $statement->bindParam('email', $email);
35
        $statement->execute();
36
        $user = $statement->fetchObject();
37
        if (empty(!$user)) {
38
            throw new UserException('Email already exists.', 400);
39
        }
40
    }
41
42
    public function getAll(): array
43
    {
44
        $query = 'SELECT `id`, `name`, `email` FROM `users` ORDER BY `id`';
45
        $statement = $this->database->prepare($query);
46
        $statement->execute();
47
48
        return $statement->fetchAll();
49
    }
50
51 View Code Duplication
    public function search(string $usersName): array
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...
52
    {
53
        $query = 'SELECT `id`, `name`, `email` FROM `users` WHERE `name` LIKE :name ORDER BY `id`';
54
        $name = '%' . $usersName . '%';
55
        $statement = $this->database->prepare($query);
56
        $statement->bindParam('name', $name);
57
        $statement->execute();
58
        $users = $statement->fetchAll();
59
        if (!$users) {
60
            throw new UserException('User name not found.', 404);
61
        }
62
63
        return $users;
64
    }
65
66 View Code Duplication
    public function loginUser(string $email, string $password)
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
        $query = 'SELECT * FROM `users` WHERE `email` = :email AND `password` = :password ORDER BY `id`';
69
        $statement = $this->database->prepare($query);
70
        $statement->bindParam('email', $email);
71
        $statement->bindParam('password', $password);
72
        $statement->execute();
73
        $user = $statement->fetchObject();
74
        if (empty($user)) {
75
            throw new UserException('Login failed: Email or password incorrect.', 400);
76
        }
77
78
        return $user;
79
    }
80
81 View Code Duplication
    public function create($user)
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...
82
    {
83
        $query = 'INSERT INTO `users` (`name`, `email`, `password`) VALUES (:name, :email, :password)';
84
        $statement = $this->database->prepare($query);
85
        $statement->bindParam('name', $user->name);
86
        $statement->bindParam('email', $user->email);
87
        $statement->bindParam('password', $user->password);
88
        $statement->execute();
89
90
        return $this->getUser((int) $this->database->lastInsertId());
91
    }
92
93 View Code Duplication
    public function update($user)
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...
94
    {
95
        $query = 'UPDATE `users` SET `name` = :name, `email` = :email WHERE `id` = :id';
96
        $statement = $this->database->prepare($query);
97
        $statement->bindParam('id', $user->id);
98
        $statement->bindParam('name', $user->name);
99
        $statement->bindParam('email', $user->email);
100
        $statement->execute();
101
102
        return $this->getUser((int) $user->id);
103
    }
104
105
    public function delete(int $userId): string
106
    {
107
        $query = 'DELETE FROM `users` WHERE `id` = :id';
108
        $statement = $this->database->prepare($query);
109
        $statement->bindParam('id', $userId);
110
        $statement->execute();
111
112
        return 'The user was deleted.';
113
    }
114
115
    public function deleteUserTasks(int $userId)
116
    {
117
        $query = 'DELETE FROM `tasks` WHERE `userId` = :userId';
118
        $statement = $this->database->prepare($query);
119
        $statement->bindParam('userId', $userId);
120
        $statement->execute();
121
    }
122
}
123