Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.00% |
23 / 25 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| StoreCustomerAnswers | |
92.00% |
23 / 25 |
|
0.00% |
0 / 1 |
3.00 | |
0.00% |
0 / 1 |
| rules | |
92.00% |
23 / 25 |
|
0.00% |
0 / 1 |
3.00 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Requests\Api\V4; |
| 4 | |
| 5 | use App\Models\Answer; |
| 6 | use App\Models\Question; |
| 7 | use App\Models\Service; |
| 8 | |
| 9 | class StoreCustomerAnswers extends BaseApiRequest |
| 10 | { |
| 11 | /** |
| 12 | * Get the validation rules that apply to the request. |
| 13 | * |
| 14 | * @return array |
| 15 | */ |
| 16 | public function rules(): array |
| 17 | { |
| 18 | // * Resolve Service. |
| 19 | $service = app()->make(Service::class); |
| 20 | $brandId = $service->brand->id; |
| 21 | $question = Question::query()->findOrFail($this->question); |
| 22 | $questionTableName = (new Question())->getTable(); |
| 23 | $answerTableName = (new Answer())->getTable(); |
| 24 | return [ |
| 25 | 'question' => [ |
| 26 | 'required', |
| 27 | "exists:$questionTableName,id", |
| 28 | // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed |
| 29 | function ($attribute, $value, $fail) use ($question, $brandId) { |
| 30 | if ($question->brand->id !== $brandId) { |
| 31 | $fail("$attribute does not belong to brand $brandId"); |
| 32 | } |
| 33 | } |
| 34 | ], |
| 35 | 'answers' => 'required|array', |
| 36 | 'answers.*' => [ |
| 37 | 'int', |
| 38 | 'distinct', |
| 39 | "exists:$answerTableName,id", |
| 40 | function ($attribute, $value, $fail) use ($question) { |
| 41 | if (Answer::query()->findOrFail($value)->question->id !== $question->id) { |
| 42 | $fail("$attribute does not belong to question $question->id"); |
| 43 | } |
| 44 | } |
| 45 | ] |
| 46 | ]; |
| 47 | } |
| 48 | } |