Passed
Push — feature/hr-admin-panel ( ceb1d1...dc17ad )
by Grant
09:09 queued 10s
created

JobPosterKeyTaskController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 27
dl 0
loc 71
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A indexByJob() 0 4 1
B batchUpdate() 0 38 6
A isUnsaved() 0 3 2
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\BatchUpdateJobTask;
7
use App\Models\JobPoster;
8
use App\Models\JobPosterKeyTask;
9
use Illuminate\Http\Resources\Json\JsonResource;
10
use Illuminate\Support\Facades\Log;
11
12
class JobPosterKeyTaskController extends Controller
13
{
14
    /**
15
     * Returns all Tasks by JobPoster ID.
16
     *
17
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
18
     * @return \Illuminate\Http\Response
19
     */
20
    public function indexByJob(JobPoster $jobPoster)
21
    {
22
        $tasksByJob = JobPosterKeyTask::where('job_poster_id', $jobPoster->id)->orderBy('order', 'asc')->get();
23
        return JsonResource::collection($tasksByJob);
24
    }
25
26
    /**
27
     * Update the set of tasks associated with a Job.
28
     *
29
     * @param  \App\Http\Requests\BatchUpdateJobTask $request   Incoming form request.
30
     * @param  \App\Models\JobPoster                 $jobPoster Incoming Job Poster object.
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function batchUpdate(BatchUpdateJobTask $request, JobPoster $jobPoster)
34
    {
35
        $order = 1;
36
        // Collection of JobPosterKeyTasks. Update the
37
        // order here so the frontend doesn't have to worry about it.
38
        $newTasks = $request->validated();
39
        foreach ($newTasks as &$singleTask) {
40
            $singleTask['order'] = $order;
41
            $order++;
42
        }
43
        $newTasks = collect($newTasks);
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $newTasks = /** @scrutinizer ignore-call */ collect($newTasks);
Loading history...
44
        $oldTasks = $jobPoster->job_poster_key_tasks;
45
46
        $savedNewTaskIds = [];
47
48
        // First, delete old tasks that weren't resubmitted, and update those that were.
49
        foreach ($oldTasks as $task) {
50
            $newTask = $newTasks->firstWhere('id', $task['id']);
51
            if ($newTask) {
52
                $savedNewTaskIds[] = $newTask['id'];
53
                $task->fill($newTask);
54
                $task->save();
55
            } else {
56
                $task->delete();
57
            }
58
        }
59
60
        // Now, save any new tasks that remain.
61
        foreach ($newTasks as $task) {
62
            if ($this->isUnsaved($task, $savedNewTaskIds)) {
63
                $jobPosterTask = new JobPosterKeyTask();
64
                $jobPosterTask->job_poster_id = $jobPoster->id;
65
                $jobPosterTask->fill($task);
66
                $jobPosterTask->save();
67
            }
68
        }
69
70
        return JsonResource::collection($jobPoster->fresh()->job_poster_key_tasks);
71
    }
72
73
    /**
74
     * Helper function to determine whether a task is unsaved.
75
     *
76
     * @param mixed    $task         Single collection item from new tasks array.
77
     * @param number[] $savedTaskIds Array of saved task IDs.
78
     * @return boolean
79
     */
80
    private function isUnsaved($task, array $savedTaskIds): bool
81
    {
82
        return !array_key_exists('id', $task) || !in_array($task['id'], $savedTaskIds);
83
    }
84
}
85