1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace BristolSU\ControlDB\Repositories; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use BristolSU\ControlDB\Contracts\Models\Role as RoleModel; |
9
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Role as RoleContract; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Role |
14
|
|
|
*/ |
|
|
|
|
15
|
|
|
class Role implements RoleContract |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get a role by ID |
21
|
|
|
* |
22
|
|
|
* @param int $id |
|
|
|
|
23
|
|
|
* @return RoleModel |
|
|
|
|
24
|
|
|
*/ |
25
|
25 |
|
public function getById(int $id): \BristolSU\ControlDB\Contracts\Models\Role |
26
|
|
|
{ |
27
|
25 |
|
return \BristolSU\ControlDB\Models\Role::findOrFail($id); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get all roles |
32
|
|
|
* |
33
|
|
|
* @return Collection|\BristolSU\ControlDB\Contracts\Models\Role[] |
34
|
|
|
*/ |
35
|
2 |
|
public function all(): Collection |
36
|
|
|
{ |
37
|
2 |
|
return \BristolSU\ControlDB\Models\Role::all(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new role |
42
|
|
|
* |
43
|
|
|
* @param int $positionId |
|
|
|
|
44
|
|
|
* @param int $groupId |
|
|
|
|
45
|
|
|
* @param int $dataProviderId |
|
|
|
|
46
|
|
|
* @return RoleModel |
|
|
|
|
47
|
|
|
*/ |
48
|
4 |
|
public function create(int $positionId, int $groupId, int $dataProviderId): \BristolSU\ControlDB\Contracts\Models\Role |
49
|
|
|
{ |
50
|
4 |
|
return \BristolSU\ControlDB\Models\Role::create([ |
|
|
|
|
51
|
4 |
|
'position_id' => $positionId, |
52
|
4 |
|
'group_id' => $groupId, |
53
|
4 |
|
'data_provider_id' => $dataProviderId |
54
|
|
|
]); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Delete a role |
59
|
|
|
* |
60
|
|
|
* @param int $id |
|
|
|
|
61
|
|
|
*/ |
|
|
|
|
62
|
2 |
|
public function delete(int $id): void |
63
|
|
|
{ |
64
|
2 |
|
$this->getById($id)->delete(); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get a role by data provider ID |
69
|
|
|
* |
70
|
|
|
* @param int $dataProviderId |
|
|
|
|
71
|
|
|
* @return RoleModel |
|
|
|
|
72
|
|
|
*/ |
73
|
4 |
|
public function getByDataProviderId(int $dataProviderId): \BristolSU\ControlDB\Contracts\Models\Role { |
|
|
|
|
74
|
4 |
|
return \BristolSU\ControlDB\Models\Role::where('data_provider_id', $dataProviderId)->firstOrFail(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get all roles that belong to the given group |
79
|
|
|
* |
80
|
|
|
* @param \BristolSU\ControlDB\Contracts\Models\Group $group |
|
|
|
|
81
|
|
|
* @return Collection|RoleModel[] |
|
|
|
|
82
|
|
|
*/ |
83
|
4 |
|
public function allThroughGroup(\BristolSU\ControlDB\Contracts\Models\Group $group): Collection |
84
|
|
|
{ |
85
|
4 |
|
return \BristolSU\ControlDB\Models\Role::where('group_id', $group->id())->get(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get all roles that belong to the given position |
90
|
|
|
* @param \BristolSU\ControlDB\Contracts\Models\Position $position |
|
|
|
|
91
|
|
|
* @return Collection|RoleModel[] |
|
|
|
|
92
|
|
|
*/ |
93
|
4 |
|
public function allThroughPosition(\BristolSU\ControlDB\Contracts\Models\Position $position): Collection |
94
|
|
|
{ |
95
|
4 |
|
return \BristolSU\ControlDB\Models\Role::where('position_id', $position->id())->get(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Paginate through all the roles |
100
|
|
|
* |
101
|
|
|
* @param int $page The page number to return |
|
|
|
|
102
|
|
|
* @param int $perPage The number of results to return per page |
103
|
|
|
* |
104
|
|
|
* @return Collection|RoleModel[] |
105
|
|
|
*/ |
106
|
3 |
|
public function paginate(int $page, int $perPage): Collection |
107
|
|
|
{ |
108
|
3 |
|
return \BristolSU\ControlDB\Models\Role::paginate($perPage, ['*'], 'page', $page)->getCollection(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the number of roles |
113
|
|
|
* |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
3 |
|
public function count(): int |
117
|
|
|
{ |
118
|
3 |
|
return \BristolSU\ControlDB\Models\Role::count(); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|