Completed
Push — master ( 394c81...30eda1 )
by Mauro
02:19
created

UsersService::updateUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 9.4285
c 3
b 0
f 0
cc 1
eloc 13
nc 1
nop 2
1
<?php
2
3
namespace App\Service;
4
5
use App\Controller\Base;
6
use App\Repository\UsersRepository;
7
8
/**
9
 * Users Service.
10
 */
11
class UsersService extends Base
12
{
13
    /**
14
     * Constructor of the class.
15
     *
16
     * @param object $database
17
     */
18
    public function __construct(\PDO $database)
19
    {
20
        $this->database = $database;
21
    }
22
23
    /**
24
     * Check if the user exists.
25
     *
26
     * @param int $userId
27
     * @return object $user
28
     * @throws \Exception
29
     */
30
    public function checkUser($userId)
31
    {
32
        $repo = new UsersRepository;
33
        $stmt = $this->database->prepare($repo->getUserQuery());
34
        $stmt->bindParam('id', $userId);
35
        $stmt->execute();
36
        $user = $stmt->fetchObject();
37
        if (!$user) {
38
            throw new \Exception(self::USER_NOT_FOUND, 404);
39
        }
40
41
        return $user;
42
    }
43
44
    /**
45
     * Get all users.
46
     *
47
     * @return array
48
     */
49
    public function getUsers()
50
    {
51
        $repository = new UsersRepository;
52
        $query = $repository->getUsersQuery();
53
        $statement = $this->database->prepare($query);
54
        $statement->execute();
55
56
        return $statement->fetchAll();
57
    }
58
59
    /**
60
     * Get one user by id.
61
     *
62
     * @param int $userId
63
     * @return array
64
     */
65
    public function getUser($userId)
66
    {
67
        $user = $this->checkUser($userId);
68
69
        return $user;
70
    }
71
72
    /**
73
     * Search users by name.
74
     *
75
     * @param string $str
76
     * @return array
77
     * @throws \Exception
78
     */
79 View Code Duplication
    public function searchUsers($str)
80
    {
81
        $repo = new UsersRepository;
82
        $stmt = $this->database->prepare($repo->searchUsersQuery());
83
        $name = '%' . $str . '%';
84
        $stmt->bindParam('name', $name);
85
        $stmt->execute();
86
        $users = $stmt->fetchAll();
87
88
        if (!$users) {
89
            throw new \Exception(self::USER_NAME_NOT_FOUND, 404);
90
        }
91
92
        return $users;
93
    }
94
95
    /**
96
     * Create a user.
97
     *
98
     * @param array $input
99
     * @return array
100
     * @throws \Exception
101
     */
102
    public function createUser($input)
103
    {
104
        $data = $this->validateInput($input);
105
        $name = $data['name'];
106
        $email = $data['email'];
107
        $repository = new UsersRepository;
108
        $query = $repository->createUserQuery();
109
        $statement = $this->database->prepare($query);
110
        $statement->bindParam('name', $name);
111
        $statement->bindParam('email', $email);
112
        $statement->execute();
113
        $user = $this->checkUser($this->database->lastInsertId());
114
115
        return $user;
116
    }
117
118
    /**
119
     * Update a user.
120
     *
121
     * @param array $input
122
     * @param int $userId
123
     * @return array
124
     * @throws \Exception
125
     */
126
    public function updateUser($input, $userId)
127
    {
128
        $user = $this->checkUser($userId);
129
        $data = $this->validateInputUpdate($input, $user);
130
        $name = $data['name'];
131
        $email = $data['email'];
132
        $repository = new UsersRepository;
133
        $query = $repository->updateUserQuery();
134
        $statement = $this->database->prepare($query);
135
        $statement->bindParam('id', $userId);
136
        $statement->bindParam('name', $name);
137
        $statement->bindParam('email', $email);
138
        $statement->execute();
139
140
        return $this->checkUser($userId);
141
    }
142
143
    /**
144
     * Delete a user.
145
     *
146
     * @param int $userId
147
     * @return array
148
     */
149
    public function deleteUser($userId)
150
    {
151
        $this->checkUser($userId);
152
        $repository = new UsersRepository;
153
        $query = $repository->deleteUserQuery();
154
        $statement = $this->database->prepare($query);
155
        $statement->bindParam('id', $userId);
156
        $statement->execute();
157
158
        return self::USER_DELETED;
159
    }
160
}
161