| 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 |  | View Code Duplication |     public function updateUser($input, $userId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $user = $this->checkUser($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         if (empty($input['name']) && empty($input['email'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             throw new \Exception(self::USER_INFO_REQUIRED, 400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $username = isset($input['name']) ? $input['name'] : $user->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $email = $user->email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         if (isset($input['email'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $email = $this->validateEmail($input['email']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $repository = new UsersRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $query = $repository->updateUserQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $statement = $this->database->prepare($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $statement->bindParam('id', $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $statement->bindParam('name', $username); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $statement->bindParam('email', $email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return $this->checkUser($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * Delete a user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param int $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     public function deleteUser($userId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->checkUser($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $repository = new UsersRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $query = $repository->deleteUserQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $statement = $this->database->prepare($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $statement->bindParam('id', $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         return self::USER_DELETED; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  |