1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\ControlDB\Repositories; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Models\User as UserModel; |
6
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\User as UserContract; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Handles users |
11
|
|
|
*/ |
|
|
|
|
12
|
|
|
class User implements UserContract |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Get a user by ID |
17
|
|
|
* |
18
|
|
|
* @param int $id ID of the user |
|
|
|
|
19
|
|
|
* @return UserModel |
|
|
|
|
20
|
|
|
*/ |
21
|
37 |
|
public function getById(int $id): UserModel |
22
|
|
|
{ |
23
|
37 |
|
return \BristolSU\ControlDB\Models\User::findOrFail($id); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get all users |
28
|
|
|
* |
29
|
|
|
* @return Collection|UserModel[] |
30
|
|
|
*/ |
31
|
3 |
|
public function all(): Collection |
32
|
|
|
{ |
33
|
3 |
|
return \BristolSU\ControlDB\Models\User::all(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new user |
38
|
|
|
* |
39
|
|
|
* @param int $dataProviderId |
|
|
|
|
40
|
|
|
* @return UserModel |
|
|
|
|
41
|
|
|
*/ |
42
|
4 |
|
public function create(int $dataProviderId): UserModel |
43
|
|
|
{ |
44
|
4 |
|
return \BristolSU\ControlDB\Models\User::create([ |
|
|
|
|
45
|
4 |
|
'data_provider_id' => $dataProviderId |
46
|
|
|
]); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Delete a user by ID |
51
|
|
|
* |
52
|
|
|
* @param int $id |
|
|
|
|
53
|
|
|
*/ |
|
|
|
|
54
|
2 |
|
public function delete(int $id): void |
55
|
|
|
{ |
56
|
2 |
|
\BristolSU\ControlDB\Models\User::findOrFail($id)->delete(); |
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get a user by its data provider ID |
61
|
|
|
* |
62
|
|
|
* @param int $dataProviderId |
|
|
|
|
63
|
|
|
* @return UserModel |
|
|
|
|
64
|
|
|
*/ |
65
|
4 |
|
public function getByDataProviderId(int $dataProviderId): UserModel { |
|
|
|
|
66
|
4 |
|
return \BristolSU\ControlDB\Models\User::where('data_provider_id', $dataProviderId)->firstOrFail(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Paginate through all the users |
71
|
|
|
* |
72
|
|
|
* @param int $page The page number to return |
|
|
|
|
73
|
|
|
* @param int $perPage The number of results to return per page |
74
|
|
|
* |
75
|
|
|
* @return Collection|UserModel[] |
76
|
|
|
*/ |
77
|
3 |
|
public function paginate(int $page, int $perPage): Collection |
78
|
|
|
{ |
79
|
3 |
|
return \BristolSU\ControlDB\Models\User::paginate($perPage, ['*'], 'page', $page)->getCollection(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the number of users |
84
|
|
|
* |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
3 |
|
public function count(): int |
88
|
|
|
{ |
89
|
3 |
|
return \BristolSU\ControlDB\Models\User::count(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|