bristol-su /
control
| 1 | <?php |
||
| 2 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 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 | */ |
||
|
0 ignored issues
–
show
|
|||
| 15 | class Role implements RoleContract |
||
| 16 | { |
||
| 17 | |||
| 18 | |||
| 19 | /** |
||
| 20 | * Get a role by ID |
||
| 21 | * |
||
| 22 | * @param int $id |
||
|
0 ignored issues
–
show
|
|||
| 23 | * @return RoleModel |
||
|
0 ignored issues
–
show
|
|||
| 24 | */ |
||
| 25 | 30 | public function getById(int $id): \BristolSU\ControlDB\Contracts\Models\Role |
|
| 26 | { |
||
| 27 | 30 | 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 |
||
|
0 ignored issues
–
show
|
|||
| 44 | * @param int $groupId |
||
|
0 ignored issues
–
show
|
|||
| 45 | * @param int $dataProviderId |
||
|
0 ignored issues
–
show
|
|||
| 46 | * @return RoleModel |
||
|
0 ignored issues
–
show
|
|||
| 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([ |
|
|
0 ignored issues
–
show
|
|||
| 51 | 4 | 'position_id' => $positionId, |
|
| 52 | 4 | 'group_id' => $groupId, |
|
| 53 | 4 | 'data_provider_id' => $dataProviderId |
|
| 54 | ]); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Delete a role |
||
| 59 | * |
||
| 60 | * @param int $id |
||
|
0 ignored issues
–
show
|
|||
| 61 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 71 | * @return RoleModel |
||
|
0 ignored issues
–
show
|
|||
| 72 | */ |
||
| 73 | 4 | public function getByDataProviderId(int $dataProviderId): \BristolSU\ControlDB\Contracts\Models\Role { |
|
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 81 | * @return Collection|RoleModel[] |
||
|
0 ignored issues
–
show
|
|||
| 82 | */ |
||
| 83 | 5 | public function allThroughGroup(\BristolSU\ControlDB\Contracts\Models\Group $group): Collection |
|
| 84 | { |
||
| 85 | 5 | 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 |
||
|
0 ignored issues
–
show
|
|||
| 91 | * @return Collection|RoleModel[] |
||
|
0 ignored issues
–
show
|
|||
| 92 | */ |
||
| 93 | 5 | public function allThroughPosition(\BristolSU\ControlDB\Contracts\Models\Position $position): Collection |
|
| 94 | { |
||
| 95 | 5 | 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 |
||
|
0 ignored issues
–
show
|
|||
| 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(); |
|
|
0 ignored issues
–
show
|
|||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Update the role model |
||
| 123 | * |
||
| 124 | * @param int $id |
||
|
0 ignored issues
–
show
|
|||
| 125 | * @param int $positionId |
||
|
0 ignored issues
–
show
|
|||
| 126 | * @param int $groupId |
||
|
0 ignored issues
–
show
|
|||
| 127 | * @param int $dataProviderId New data provider ID |
||
| 128 | * @return RoleModel |
||
|
0 ignored issues
–
show
|
|||
| 129 | */ |
||
| 130 | 7 | public function update(int $id, int $positionId, int $groupId, int $dataProviderId): RoleModel |
|
| 131 | { |
||
| 132 | 7 | $role = $this->getById($id)->fill(['position_id' => $positionId, 'group_id' => $groupId, 'data_provider_id' => $dataProviderId]); |
|
| 133 | 7 | $role->save(); |
|
| 134 | 7 | return $role; |
|
| 135 | } |
||
| 136 | } |
||
| 137 |