Passed
Push — task/user-api-endpoint ( 6e4649...f0b85b )
by Chris
04:32
created

JobPosterKeyTaskController::isUnsaved()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
nop 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
11
class JobPosterKeyTaskController extends Controller
12
{
13
    /**
14
     * Returns all Tasks by JobPoster ID.
15
     *
16
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
17
     * @return \Illuminate\Http\Response
18
     */
19
    public function indexByJob(JobPoster $jobPoster)
20
    {
21
        $tasksByJob = JobPosterKeyTask::where('job_poster_id', $jobPoster->id)->get();
22
        return JsonResource::collection($tasksByJob);
23
    }
24
25
    /**
26
     * Update the set of tasks associated with a Job.
27
     *
28
     * @param  \App\Http\Requests\BatchUpdateJobTask $request   Incoming form request.
29
     * @param  \App\Models\JobPoster                 $jobPoster Incoming Job Poster object.
30
     * @return \Illuminate\Http\Response
31
     */
32
    public function batchUpdate(BatchUpdateJobTask $request, JobPoster $jobPoster)
33
    {
34
        $newTasks = collect($request->validated()); // Collection of JobPosterKeyTasks.
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

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