1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repositories; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\AdminStoreRequest; |
6
|
|
|
use App\Models\User; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Illuminate\Support\Facades\Hash; |
9
|
|
|
|
10
|
|
|
class UserRepository implements UserRepositoryInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get all the users. |
15
|
|
|
* |
16
|
|
|
* @param int|null $recordsPerPage |
17
|
|
|
* @param array|null $searchParameters |
18
|
|
|
* |
19
|
|
|
* @return iterable|\Illuminate\Contracts\Pagination\LengthAwarePaginator |
20
|
|
|
*/ |
21
|
1 |
|
public function users(int $recordsPerPage = null, array $searchParameters = null) |
22
|
|
|
{ |
23
|
1 |
|
$query = User::orderBy('email', 'asc'); |
24
|
1 |
|
$query->with('profile'); |
25
|
|
|
|
26
|
1 |
|
if (!is_null($searchParameters)) { |
27
|
|
|
if (!empty($searchParameters['name'])) { |
28
|
|
|
$query->whereHas('profile', function ($q) use ($searchParameters) { |
29
|
|
|
$q->where('name', 'like', '%' . $searchParameters['name'] . '%'); |
30
|
|
|
}); |
31
|
|
|
} |
32
|
|
|
if (!empty($searchParameters['surname'])) { |
33
|
|
|
$query->whereHas('profile', function ($q) use ($searchParameters) { |
34
|
|
|
$q->where('surname', 'like', '%' . $searchParameters['surname'] . '%'); |
35
|
|
|
}); |
36
|
|
|
} |
37
|
|
|
if (!empty($searchParameters['email'])) { |
38
|
|
|
$query->where('email', 'like', '%' . $searchParameters['email'] . '%'); |
39
|
|
|
} |
40
|
|
|
if (!empty($searchParameters['countryId'])) { |
41
|
|
|
$query->whereHas('profile', function ($q) use ($searchParameters) { |
42
|
|
|
$q->where('country_id', $searchParameters['countryId']); |
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
if (!empty($searchParameters['role'])) { |
46
|
|
|
$query->role($searchParameters['role']); |
47
|
|
|
} |
48
|
|
|
if (!empty($searchParameters['team'])) { |
49
|
|
|
$query->role($searchParameters['team']); |
50
|
|
|
} |
51
|
|
|
if (!empty($searchParameters['status'])) { |
52
|
|
|
$query->currentStatus($searchParameters['status']); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
if ($recordsPerPage) { |
|
|
|
|
57
|
1 |
|
return $query->paginate($recordsPerPage)->withQueryString(); |
58
|
|
|
} |
59
|
|
|
return $query->get(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get user by id |
64
|
|
|
* |
65
|
|
|
* @param int $userId |
66
|
|
|
* @return User |
67
|
|
|
*/ |
68
|
4 |
|
public function getById(int $userId): User |
69
|
|
|
{ |
70
|
4 |
|
return User::findOrFail($userId); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Store User |
75
|
|
|
* |
76
|
|
|
* @param array $data |
77
|
|
|
* @return User |
78
|
|
|
*/ |
79
|
1 |
|
public function storeUser(array $data): User |
80
|
|
|
{ |
81
|
1 |
|
$user = new User(); |
82
|
|
|
|
83
|
1 |
|
$user->email = $data['email']; |
84
|
1 |
|
$user->password = (array_key_exists('password', $data)) ? Hash::make($data['password']) : null; |
85
|
|
|
|
86
|
1 |
|
$user->save(); |
87
|
|
|
|
88
|
1 |
|
$user->setStatus('enabled'); |
89
|
|
|
|
90
|
1 |
|
return $user->fresh(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Update User |
95
|
|
|
* |
96
|
|
|
* @param array $data |
97
|
|
|
* @param int $userId |
98
|
|
|
* @return User |
99
|
|
|
*/ |
100
|
1 |
|
public function update(array $data, int $userId): User |
101
|
|
|
{ |
102
|
1 |
|
$user = $this->getById($userId); |
103
|
|
|
|
104
|
1 |
|
$user->email = $data['email']; |
105
|
1 |
|
if (array_key_exists('password', $data)) { |
106
|
1 |
|
$user->password = Hash::make($data['password']); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$user->update(); |
110
|
|
|
|
111
|
1 |
|
$status = (isset($data['status'])) ? 'enabled' : 'disabled'; |
112
|
1 |
|
if ($user->status() != $status) { |
113
|
1 |
|
$user->setStatus($status); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
return $user; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Delete User |
121
|
|
|
* |
122
|
|
|
* @param int $userId |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
1 |
|
public function delete(int $userId): void |
126
|
|
|
{ |
127
|
1 |
|
User::destroy($userId); |
128
|
1 |
|
} |
129
|
|
|
} |
130
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: