Completed
Push — master ( 2b5a5a...a3a238 )
by Mauro
02:35
created

UserService::deleteUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

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