Completed
Push — master ( 394e5b...f94bf1 )
by Mauro
02:27
created

UserRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Repository;
4
5
use App\Message\UserMessage;
6
use App\Exception\UserException;
7
use App\Repository\Query\UserQuery;
8
9
/**
10
 * Users Repository.
11
 */
12 View Code Duplication
class UserRepository extends BaseRepository
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * @param object $database
16
     */
17
    public function __construct(\PDO $database = null)
18
    {
19
        $this->database = $database;
20
    }
21
22
    /**
23
     * Check if the user exists.
24
     *
25
     * @param int $userId
26
     * @return array $user
27
     * @throws \Exception
28
     */
29
    public function checkUser($userId)
30
    {
31
        $statement = $this->database->prepare(UserQuery::GET_USER_QUERY);
32
        $statement->bindParam('id', $userId);
33
        $statement->execute();
34
        $user = $statement->fetchObject();
35
        if (empty($user)) {
36
            throw new UserException(UserException::USER_NOT_FOUND, 404);
37
        }
38
39
        return $user;
40
    }
41
42
    /**
43
     * Get all users.
44
     *
45
     * @return array
46
     */
47
    public function getUsers()
48
    {
49
        $statement = $this->database->prepare(UserQuery::GET_USERS_QUERY);
50
        $statement->execute();
51
52
        return $statement->fetchAll();
53
    }
54
55
    /**
56
     * Search users by name.
57
     *
58
     * @param string $usersName
59
     * @return array
60
     * @throws \Exception
61
     */
62
    public function searchUsers($usersName)
63
    {
64
        $statement = $this->database->prepare(UserQuery::SEARCH_USERS_QUERY);
65
        $query = '%' . $usersName . '%';
66
        $statement->bindParam('query', $query);
67
        $statement->execute();
68
        $users = $statement->fetchAll();
69
        if (!$users) {
70
            throw new UserException(UserException::USER_NOT_FOUND, 404);
71
        }
72
73
        return $users;
74
    }
75
76
    /**
77
     * Create a user.
78
     *
79
     * @param array $data
80
     * @return array
81
     * @throws \Exception
82
     */
83
    public function createUser($data)
84
    {
85
        $statement = $this->database->prepare(UserQuery::CREATE_USER_QUERY);
86
        $statement->bindParam('name', $data['name']);
87
        $statement->bindParam('email', $data['email']);
88
        $statement->execute();
89
        $user = $this->checkUser($this->database->lastInsertId());
90
91
        return $user;
92
    }
93
94
    /**
95
     * Update a user.
96
     *
97
     * @param array $data
98
     * @param int $userId
99
     * @return array
100
     */
101
    public function updateUser($data, $userId)
102
    {
103
        $statement = $this->database->prepare(UserQuery::UPDATE_USER_QUERY);
104
        $statement->bindParam('id', $userId);
105
        $statement->bindParam('name', $data['name']);
106
        $statement->bindParam('email', $data['email']);
107
        $statement->execute();
108
        $user = $this->checkUser($userId);
109
110
        return $user;
111
    }
112
113
    /**
114
     * Delete a user.
115
     *
116
     * @param int $userId
117
     * @return string
118
     */
119
    public function deleteUser($userId)
120
    {
121
        $statement = $this->database->prepare(UserQuery::DELETE_USER_QUERY);
122
        $statement->bindParam('id', $userId);
123
        $statement->execute();
124
125
        return UserMessage::USER_DELETED;
126
    }
127
}
128