Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| QuestionController | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| index | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| indexWithResponse | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| unAnsweredBy | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Api\V4; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Resources\Api\V4\QuestionsResource; |
| 7 | use App\Models\Answer; |
| 8 | use App\Models\Customer; |
| 9 | use App\Models\Question; |
| 10 | use App\Models\Service; |
| 11 | use Illuminate\Http\Resources\Json\JsonResource; |
| 12 | use Illuminate\Http\Resources\Json\ResourceCollection; |
| 13 | |
| 14 | class QuestionController extends Controller |
| 15 | { |
| 16 | /** |
| 17 | * Returns all service questions. |
| 18 | * |
| 19 | * @param \App\Models\Service $service |
| 20 | * @return \Illuminate\Http\Resources\Json\ResourceCollection |
| 21 | */ |
| 22 | public function index(Service $service): ResourceCollection |
| 23 | { |
| 24 | return new QuestionsResource($service->brand->questions); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Returns all service questions with user answers for all of them. |
| 29 | * |
| 30 | * @param \App\Models\Service $service |
| 31 | * @param \App\Models\Customer $customer |
| 32 | * @return \Illuminate\Http\Resources\Json\ResourceCollection |
| 33 | */ |
| 34 | public function indexWithResponse(Service $service, Customer $customer): ResourceCollection |
| 35 | { |
| 36 | // * Make sure we are accessing a customer that belongs to our service. |
| 37 | $this->authorize('customerBelongsToService', $customer); |
| 38 | |
| 39 | $customerAnswers = $customer->getAnswers($service); |
| 40 | return new QuestionsResource($service->brand->questions, $customerAnswers); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns all service questions that was not answered by a user. |
| 45 | * |
| 46 | * @param \App\Models\Service $service |
| 47 | * @param \App\Models\Customer $customer |
| 48 | * @return \Illuminate\Http\Resources\Json\JsonResource |
| 49 | */ |
| 50 | public function unAnsweredBy(Service $service, Customer $customer): JsonResource |
| 51 | { |
| 52 | // * Make sure we are accessing a customer that belongs to our service. |
| 53 | $this->authorize('customerBelongsToService', $customer); |
| 54 | |
| 55 | $questions = Question::query()->whereHas('brand', function ($q) use ($service) { |
| 56 | $q->where('id', $service->brand->id); |
| 57 | })->whereDoesntHave('answers', function ($q) use ($customer) { |
| 58 | $q->whereIn('id', Answer::query()->whereHas('customers', function ($q) use ($customer) { |
| 59 | $q->where('id', $customer->id); |
| 60 | })->pluck('id')); |
| 61 | })->with('answers')->get(); |
| 62 | return new QuestionsResource($questions); |
| 63 | } |
| 64 | } |