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