Passed
Push — task/common-translation-packag... ( 852212...2abece )
by Grant
07:25
created

CriteriaController::toApiArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Resources\CriteriaResource;
7
use App\Models\Assessment;
8
use App\Models\AssessmentPlanNotification;
9
use App\Models\Criteria;
10
use App\Models\JobPoster;
11
use Illuminate\Http\Request;
12
13
class CriteriaController extends Controller
14
{
15
    /**
16
     * Returns all criteria by JobPoster ID.
17
     *
18
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
19
     * @return \Illuminate\Http\Response
20
     */
21
    public function indexByJob(JobPoster $jobPoster)
22
    {
23
        $criteriaByJob = Criteria::where('job_poster_id', $jobPoster->id)->get();
24
        return CriteriaResource::collection($criteriaByJob);
25
    }
26
27
    /**
28
     * Update the set of criteria associated with a Job.
29
     *
30
     * @param  Request $request
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
31
     * @param  JobPoster $jobPoster
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
32
     * @return \Illuminate\Http\Response
33
     */
34
    public function batchUpdate(Request $request, JobPoster $jobPoster)
35
    {
36
        $newCriteria = collect($request->input()); // TODO: switch to validated
37
        $oldCriteria = $jobPoster->criteria;
38
39
        $updatedIds = [];
40
41
        // First, delete old criteria that weren't resubmitted, and update those that were.
42
        foreach ($oldCriteria as $criteria) {
43
            $newData = $newCriteria->firstWhere('id', $criteria['id']);
44
            if ($newData) {
45
                $updatedIds[] = $criteria->id;
46
                $this->updateCriteria($criteria, $newData);
47
            } else {
48
                $this->deleteCriteria($criteria);
49
            }
50
        }
51
52
        $isUnsaved = function ($criteria, $savedIds): bool {
53
            return !array_key_exists('id', $criteria) || !in_array($criteria['id'], $savedIds);
54
        };
55
56
        // Now, save any new criteria that remain.
57
        foreach ($newCriteria as $criteriaData) {
58
            if ($isUnsaved($criteriaData, $updatedIds)) {
59
                $criteria = new Criteria();
60
                $criteria->job_poster_id = $jobPoster->id;
61
                $fillableData = collect($criteriaData)->except(['id', 'job_poster_id'])->toArray();
62
                $criteria->fill($fillableData);
63
                $this->createCriteria($criteria);
64
            }
65
        }
66
67
        return CriteriaResource::collection($jobPoster->fresh()->criteria);
68
    }
69
70
    /**
71
     * Save a new criteria and create a notification.
72
     *
73
     * @param Criteria $criteria
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
74
     * @return void
1 ignored issue
show
Coding Style introduced by
Function return type is void, but function contains return statement
Loading history...
75
     */
76
    protected function createCriteria(Criteria $criteria)
77
    {
78
        $criteria->save();
79
        $notification = $this->makeAssessmentPlanNotification(
80
            'CREATE',
81
            $criteria
82
        );
83
        $notification->save();
84
85
        return $criteria;
86
    }
87
88
    /**
89
     * Update an existing Job Criteria and create a notification if necessary.
90
     *
91
     * @param  \App\Models\Criteria $oldCriteria Existing Criteria.
92
     * @param  mixed[] $newData Updated version of the Criteria.
2 ignored issues
show
Coding Style introduced by
Expected 14 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
93
     * @return void
94
     */
95
    protected function updateCriteria(Criteria $oldCriteria, $newData): void
1 ignored issue
show
Coding Style introduced by
Type hint "array" missing for $newData
Loading history...
96
    {
97
        // We only need to create a notification when the non-descriptive fields change.
98
        if ($oldCriteria->skill_level_id != $newData['skill_level_id']
99
            || $oldCriteria->skill_id != $newData['skill_id']
100
        ) {
101
            $notification = $this->makeAssessmentPlanNotification(
102
                'UPDATE',
103
                $oldCriteria,
104
                $newData['skill_id'],
105
                $newData['skill_level_id'],
106
                $newData['criteria_type_id']
107
            );
108
            $notification->save();
109
        }
110
        // Get just the data that can be changed.
111
        $fillableData = collect($newData)->except(['id', 'job_poster_id'])->toArray();
112
        $oldCriteria->fill($fillableData);
113
        $oldCriteria->save();
114
    }
115
116
    /**
117
     * Delete existing Job Criteria and create a notification.
118
     *
119
     * @param  \App\Models\Criteria $criteria Incoming Criteria.
120
     * @return void
121
     */
122
    protected function deleteCriteria(Criteria $criteria): void
123
    {
124
        $notification = $notification = $this->makeAssessmentPlanNotification(
0 ignored issues
show
Unused Code introduced by
The assignment to $notification is dead and can be removed.
Loading history...
125
            'DELETE',
126
            $criteria
127
        );
128
        $notification->save();
129
130
        // Delete assessments related to this criteria.
131
        Assessment::where('criterion_id', $criteria->id)->delete();
132
        $criteria->delete();
133
    }
134
135
    /**
136
     * Create a new AssessmentPlanNotification for a modification to a Criteria
137
     *
138
     * @param  string               $type            Can be CREATE, UPDATE or DELETE.
1 ignored issue
show
Coding Style introduced by
Expected 14 spaces after parameter name; 12 found
Loading history...
139
     * @param  \App\Models\Criteria $criteria        The Criteria (the OLD criteria if updating or deleting)
2 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter name; 8 found
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
140
     * @param  integer|null         $newSkillId      Only used for UPDATE type notifications.
1 ignored issue
show
Coding Style introduced by
Expected 8 spaces after parameter name; 6 found
Loading history...
141
     * @param  integer|null         $newSkillLevelId Only used for UPDATE type notifications.
1 ignored issue
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
142
     * @param  integer|null         $newCriteriaTypeId Only used for UPDATE type notifications.
143
     * @return \App\Models\AssessmentPlanNotification
144
     */
145
    protected function makeAssessmentPlanNotification(
146
        string $type,
147
        Criteria $criteria,
148
        $newSkillId = null,
149
        $newSkillLevelId = null,
150
        $newCriteriaTypeId = null
151
    ) {
152
        $notification = new AssessmentPlanNotification();
153
        $notification->job_poster_id = $criteria->job_poster_id;
154
        $notification->type = $type;
155
        $notification->criteria_id = $criteria->id;
156
        $notification->skill_id = $criteria->skill_id;
157
        $notification->criteria_type_id = $criteria->criteria_type_id;
158
        $notification->skill_level_id = $criteria->skill_level_id;
159
        $notification->skill_id_new = $newSkillId;
160
        $notification->skill_level_id_new = $newSkillLevelId;
161
        $notification->criteria_type_id_new = $newCriteriaTypeId;
162
        $notification->acknowledged = false;
163
        return $notification;
164
    }
165
}
166