Completed
Push — master ( 52970f...c6d773 )
by Tristan
24:57 queued 10:40
created

ScreeningPlanController::createForJob()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 28
ccs 0
cts 20
cp 0
rs 9.3222
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 30
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Models\JobPoster;
7
use Illuminate\Support\Facades\Lang;
8
use Illuminate\View\View;
9
use App\Models\Skill;
10
use App\Models\Lookup\SkillLevel;
11
use App\Models\Lookup\AssessmentType;
12
use App\Models\ScreeningPlan;
13
use App\Models\Assessment;
14
15
class ScreeningPlanController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ScreeningPlanController
Loading history...
16
{
17
    /**
18
     * Show the form for creating a new resource.
19
     *
20
     * @param JobPoster $jobPoster The job to create a screening plan for.
21
     *
22
     * @return View|\Illuminate\Contracts\View\Factory
23
     */
24
    public function createForJob(JobPoster $jobPoster)
25
    {
26
        $jobPoster->load('criteria');
27
        $skills = Skill::all();
0 ignored issues
show
Unused Code introduced by
The assignment to $skills is dead and can be removed.
Loading history...
28
29
        $assessment_plans = [];
30
        foreach ($jobPoster->screening_plans as $plan) {
31
            $assessment_plan = [];
32
            foreach (AssessmentType::all() as $type) {
33
                $plan_type_assessments = $plan->assessments->where('assessment_type_id', $type->id);
34
                if (!$plan_type_assessments->isEmpty()) {
35
                    $assessment_plan[$type->name] = [];
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on App\Models\Lookup\AssessmentType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
36
                    foreach ($plan_type_assessments as $assessment) {
37
                        $assessment_plan[$type->name][] = $assessment->criterion->skill->skill;
38
                    }
39
                }
40
            }
41
            $assessment_plans[$plan->id] = $assessment_plan;
42
        }
43
44
        return view(
45
            'manager/screening-plan',
46
            [
47
                'screening' => Lang::get('manager/screening-plan'),
48
                'job' => $jobPoster,
49
                'skill_template' => Lang::get('common/skills'),
50
                'assessment_types' => AssessmentType::all(),
51
                'assessment_plans' => $assessment_plans,
52
            ]
53
        );
54
    }
55
56
    /**
57
     * Save the screening plan.
58
     *
59
     * @param Request   $request   The incoming request object.
60
     * @param JobPoster $jobPoster The job to create a screen plan for.
61
     *
62
     * @return \Illuminate\Routing\Redirector|Illuminate\Http\RedirectResponse
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Ill...e\Http\RedirectResponse was not found. Did you mean Illuminate\Http\RedirectResponse? If so, make sure to prefix the type with \.
Loading history...
63
     */
64
    public function store(Request $request, JobPoster $jobPoster)
65
    {
66
        //TODO: Validate every job criteria is represented
67
        //TODO: Validate every job criteria has at least one assessment (?)
68
        //TODO: Validate every assessment is correct and complete
69
70
        $plan = new ScreeningPlan();
71
        $plan->job_poster_id = $jobPoster->id;
72
        $plan->version = ScreeningPlan::max('version') + 1;
73
        $plan->save();
74
75
        if ($request->input('criteria')) {
76
            foreach ($request->input('criteria') as $criteriaId => $assessments) {
77
                foreach ($assessments['assessment'] as $assessmentData) {
78
                    $assessment = new Assessment();
79
                    $assessment->screening_plan_id = $plan->id;
80
                    $assessment->criterion_id = $criteriaId;
81
                    $assessment->assessment_type_id = $assessmentData['assessment_type'];
82
                    $assessment->save();
83
                }
84
            }
85
        }
86
87
        return redirect(route('manager.jobs.screening_plan', $jobPoster));
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect(route('m...ing_plan', $jobPoster)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Ill...nate\Routing\Redirector.
Loading history...
88
    }
89
90
    /**
91
     * Delete the particular skill declaration in storage.
92
     *
93
     * @param \Illuminate\Http\Request  $request       The request object.
94
     * @param \App\Models\ScreeningPlan $screeningPlan The screening plan to delete.
95
     *
96
     * @return \Illuminate\Http\RedirectResponse
97
     */
98
    public function destroy(Request $request, ScreeningPlan $screeningPlan) // phpcs:ignore
99
    {
100
        $this->authorize('delete', $screeningPlan);
101
        $screeningPlan->delete();
102
103
        if ($request->ajax()) {
104
            $content = ['message' => 'Screening Plan deleted'];
105
            return $this->formatAjaxResponse($content);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->formatAjaxResponse($content) returns the type Illuminate\Http\Response which is incompatible with the documented return type Illuminate\Http\RedirectResponse.
Loading history...
106
        }
107
108
        return redirect()->back();
109
    }
110
}
111