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 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
||||
| 8 | use Illuminate\Support\Collection; |
||||
| 9 | |||||
| 10 | class DataRole implements \BristolSU\ControlDB\Contracts\Repositories\DataRole |
||||
|
0 ignored issues
–
show
|
|||||
| 11 | { |
||||
| 12 | |||||
| 13 | /** |
||||
| 14 | * Get a data role by ID |
||||
| 15 | * |
||||
| 16 | * @param int $id |
||||
|
0 ignored issues
–
show
|
|||||
| 17 | * @return \BristolSU\ControlDB\Contracts\Models\DataRole |
||||
|
0 ignored issues
–
show
|
|||||
| 18 | */ |
||||
| 19 | 22 | public function getById(int $id): \BristolSU\ControlDB\Contracts\Models\DataRole |
|||
| 20 | { |
||||
| 21 | 22 | return \BristolSU\ControlDB\Models\DataRole::findOrFail($id); |
|||
|
0 ignored issues
–
show
|
|||||
| 22 | } |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * Get a data role with the given attributes, including additional attributes. |
||||
| 26 | * |
||||
| 27 | * @param array $attributes |
||||
|
0 ignored issues
–
show
|
|||||
| 28 | * @return \BristolSU\ControlDB\Contracts\Models\DataRole |
||||
|
0 ignored issues
–
show
|
|||||
| 29 | */ |
||||
| 30 | 3 | public function getWhere($attributes = []): \BristolSU\ControlDB\Contracts\Models\DataRole |
|||
| 31 | { |
||||
| 32 | 3 | $roles = $this->getAllWhere($attributes); |
|||
| 33 | |||||
| 34 | 3 | if($roles->count() > 0) { |
|||
|
0 ignored issues
–
show
|
|||||
| 35 | 2 | return $roles->first(); |
|||
| 36 | } |
||||
| 37 | 1 | throw (new ModelNotFoundException())->setModel(DataRole::class); |
|||
| 38 | } |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * Create a new data role |
||||
| 42 | * |
||||
| 43 | * @param string|null $roleName |
||||
|
0 ignored issues
–
show
|
|||||
| 44 | * @param string|null $email |
||||
|
0 ignored issues
–
show
|
|||||
| 45 | * @return \BristolSU\ControlDB\Contracts\Models\DataRole |
||||
|
0 ignored issues
–
show
|
|||||
| 46 | */ |
||||
| 47 | 5 | public function create(?string $roleName = null, ?string $email = null): \BristolSU\ControlDB\Contracts\Models\DataRole |
|||
| 48 | { |
||||
| 49 | 5 | return \BristolSU\ControlDB\Models\DataRole::create([ |
|||
|
0 ignored issues
–
show
|
|||||
| 50 | 5 | 'role_name' => $roleName, |
|||
| 51 | 5 | 'email' => $email, |
|||
| 52 | ]); |
||||
|
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...
|
|||||
| 53 | } |
||||
| 54 | |||||
| 55 | /** |
||||
| 56 | * Get all data roles where the given attributes match, including additional attributes. |
||||
| 57 | * |
||||
| 58 | * @param array $attributes |
||||
|
0 ignored issues
–
show
|
|||||
| 59 | * @return Collection |
||||
|
0 ignored issues
–
show
|
|||||
| 60 | */ |
||||
| 61 | 6 | public function getAllWhere($attributes = []): Collection |
|||
| 62 | { |
||||
| 63 | 6 | $baseAttributes = $attributes; |
|||
| 64 | 6 | $additionalAttributes = []; |
|||
| 65 | 6 | foreach (\BristolSU\ControlDB\Models\DataRole::getAdditionalAttributes() as $property) { |
|||
| 66 | 2 | if (array_key_exists($property, $baseAttributes)) { |
|||
| 67 | 1 | $additionalAttributes[$property] = $baseAttributes[$property]; |
|||
| 68 | 1 | unset($baseAttributes[$property]); |
|||
| 69 | } |
||||
| 70 | } |
||||
| 71 | return \BristolSU\ControlDB\Models\DataRole::where(function($query) use ($baseAttributes) { |
||||
|
0 ignored issues
–
show
|
|||||
| 72 | 6 | foreach($baseAttributes as $key => $value) { |
|||
|
0 ignored issues
–
show
|
|||||
| 73 | 6 | $query = $query->where($key, 'LIKE', '%' . $value . '%'); |
|||
| 74 | } |
||||
| 75 | 6 | return $query; |
|||
| 76 | })->get()->filter(function (\BristolSU\ControlDB\Models\DataRole $dataRole) use ($additionalAttributes) { |
||||
|
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...
|
|||||
| 77 | 5 | foreach ($additionalAttributes as $additionalAttribute => $value) { |
|||
| 78 | 1 | if ($dataRole->getAdditionalAttribute($additionalAttribute) !== $value) { |
|||
| 79 | 1 | return false; |
|||
| 80 | } |
||||
| 81 | } |
||||
| 82 | 5 | return true; |
|||
| 83 | 6 | })->values(); |
|||
|
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...
|
|||||
| 84 | } |
||||
| 85 | |||||
| 86 | /** |
||||
| 87 | * Update a data position with the given attributes |
||||
| 88 | * |
||||
| 89 | * @param int $id |
||||
|
0 ignored issues
–
show
|
|||||
| 90 | * @param string|null $roleName Custom name for the role |
||||
| 91 | * @param string|null $email Email of the role |
||||
|
0 ignored issues
–
show
|
|||||
| 92 | * @return \BristolSU\ControlDB\Contracts\Models\DataRole |
||||
|
0 ignored issues
–
show
|
|||||
| 93 | */ |
||||
| 94 | 6 | public function update(int $id, ?string $roleName = null, ?string $email = null): \BristolSU\ControlDB\Contracts\Models\DataRole |
|||
| 95 | { |
||||
| 96 | 6 | $dataRole = $this->getById($id)->fill([ |
|||
|
0 ignored issues
–
show
The method
fill() does not exist on BristolSU\ControlDB\Contracts\Models\DataRole. Since it exists in all sub-types, consider adding an abstract or default implementation to BristolSU\ControlDB\Contracts\Models\DataRole.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 97 | 6 | 'role_name' => $roleName, 'email' => $email |
|||
| 98 | ]); |
||||
|
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...
|
|||||
| 99 | 6 | $dataRole->save(); |
|||
| 100 | 6 | return $dataRole; |
|||
| 101 | } |
||||
| 102 | } |