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 DataPosition implements \BristolSU\ControlDB\Contracts\Repositories\DataPosition |
||||
|
0 ignored issues
–
show
|
|||||
| 11 | { |
||||
| 12 | |||||
| 13 | /** |
||||
| 14 | * Get a data position by ID |
||||
| 15 | * |
||||
| 16 | * @param int $id |
||||
|
0 ignored issues
–
show
|
|||||
| 17 | * @return \BristolSU\ControlDB\Contracts\Models\DataPosition |
||||
|
0 ignored issues
–
show
|
|||||
| 18 | */ |
||||
| 19 | 17 | public function getById(int $id): \BristolSU\ControlDB\Contracts\Models\DataPosition |
|||
| 20 | { |
||||
| 21 | 17 | return \BristolSU\ControlDB\Models\DataPosition::findOrFail($id); |
|||
|
0 ignored issues
–
show
|
|||||
| 22 | } |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * Get a data position with the given attributes, including additional attributes. |
||||
| 26 | * |
||||
| 27 | * @param array $attributes |
||||
|
0 ignored issues
–
show
|
|||||
| 28 | * @return \BristolSU\ControlDB\Contracts\Models\DataPosition |
||||
|
0 ignored issues
–
show
|
|||||
| 29 | */ |
||||
| 30 | 3 | public function getWhere($attributes = []): \BristolSU\ControlDB\Contracts\Models\DataPosition |
|||
| 31 | { |
||||
| 32 | 3 | $positions = $this->getAllWhere($attributes); |
|||
| 33 | |||||
| 34 | 3 | if($positions->count() > 0) { |
|||
|
0 ignored issues
–
show
|
|||||
| 35 | 2 | return $positions->first(); |
|||
| 36 | } |
||||
| 37 | 1 | throw (new ModelNotFoundException())->setModel(DataPosition::class); |
|||
| 38 | } |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * Create a new data position |
||||
| 42 | * |
||||
| 43 | * @param string|null $name |
||||
|
0 ignored issues
–
show
|
|||||
| 44 | * @param string|null $description |
||||
|
0 ignored issues
–
show
|
|||||
| 45 | * @return \BristolSU\ControlDB\Contracts\Models\DataPosition |
||||
|
0 ignored issues
–
show
|
|||||
| 46 | */ |
||||
| 47 | 5 | public function create(?string $name = null, ?string $description = null): \BristolSU\ControlDB\Contracts\Models\DataPosition |
|||
| 48 | { |
||||
| 49 | 5 | return \BristolSU\ControlDB\Models\DataPosition::create([ |
|||
|
0 ignored issues
–
show
|
|||||
| 50 | 5 | 'name' => $name, |
|||
| 51 | 5 | 'description' => $description, |
|||
| 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 positions 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\DataPosition::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\DataPosition::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\DataPosition $dataPosition) 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 ($dataPosition->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 $name Name of the position |
||||
|
0 ignored issues
–
show
|
|||||
| 91 | * @param string|null $description Description of the position |
||||
| 92 | * @return \BristolSU\ControlDB\Contracts\Models\DataPosition |
||||
|
0 ignored issues
–
show
|
|||||
| 93 | */ |
||||
| 94 | 6 | public function update(int $id, ?string $name = null, ?string $description = null): \BristolSU\ControlDB\Contracts\Models\DataPosition |
|||
| 95 | { |
||||
| 96 | 6 | $dataPosition = $this->getById($id)->fill([ |
|||
|
0 ignored issues
–
show
The method
fill() does not exist on BristolSU\ControlDB\Contracts\Models\DataPosition. Since it exists in all sub-types, consider adding an abstract or default implementation to BristolSU\ControlDB\Contracts\Models\DataPosition.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 97 | 6 | 'name' => $name, 'description' => $description |
|||
| 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 | $dataPosition->save(); |
|||
| 100 | 6 | return $dataPosition; |
|||
| 101 | } |
||||
| 102 | } |