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