Passed
Push — feature/screening-plan-redux ( 285239...e0f683 )
by Tristan
08:22
created

AssessmentPlanController::getForJob()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 21
ccs 0
cts 15
cp 0
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 12
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Gate;
6
use App\Models\JobPoster;
7
use App\Models\Criteria;
8
use App\Models\Assessment;
9
use App\Models\RatingGuideQuestion;
10
use App\Models\RatingGuideAnswer;
11
12
class AssessmentPlanController extends Controller
13
{
14
    /**
15
     * Return all the criteria, Assessments, Assessments,
16
     * RatingGuideQuestions, and RatingGuideAnswers associated with a job,
17
     * as an array.
18
     *
19
     * @param JobPoster $jobPoster Job Poster to retrieve assessment plan for.
20
     * @return mixed[]
21
     */
22
    public function getForJob(JobPoster $jobPoster)
0 ignored issues
show
introduced by
Method \App\Http\Controllers\AssessmentPlanController::getForJob() does not have return type hint for its return value but it should be possible to add it based on @return annotation "mixed[]".
Loading history...
23
    {
24
        if (Gate::denies('view-assessment-plan', $jobPoster)) {
25
            abort(403);
26
        }
27
28
        $criteria = Criteria::where('job_poster_id', $jobPoster->id)->get();
29
        $criteriaTranslated = [];
30
        foreach ($criteria as $criterion) {
31
            // TODO: getTranslationsArray probably makes DB calls every loop. Find a way to profile & optimize.
32
            $criteriaTranslated[] = array_merge($criterion->toArray(), $criterion->getTranslationsArray());
33
        }
34
        $criteriaIds = $criteria->pluck("id");
35
        $assessments = Assessment::whereIn("criterion_id", $criteriaIds)->get();
36
        $questions = RatingGuideQuestion::where('job_poster_id', $jobPoster->id)->get();
37
        $answers = RatingGuideAnswer::whereIn('rating_guide_question_id', $questions->pluck('id'))->get();
38
        return [
39
            'criteria' => $criteriaTranslated,
40
            'assessments' => $assessments->toArray(),
41
            'rating_guide_questions' => $questions->toArray(),
42
            'rating_guide_answers' => $answers->toArray()
43
        ];
44
    }
45
}
46