Passed
Push — task/update-ci-configuration ( 69fb1d )
by Chris
66:16 queued 49:17
created

JobTaskController::indexByJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Models\JobPosterKeyTask;
6
use App\Models\JobPoster;
7
use App\Http\Controllers\Controller;
8
use App\Http\Requests\BatchUpdateJobTask;
9
10
class JobTaskController extends Controller
11
{
12
    /**
13
     * Converts a JobPosterKeyTask to an array appropriate for the api.
14
     *
15
     * @param  JobPosterKeyTask $model Incoming Job Poster Key Task object.
16
     * @return array
17
     */
18
    public function toApiArray(JobPosterKeyTask $model)
19
    {
20
        return array_merge($model->toArray(), $model->getTranslationsArray());
21
    }
22
23
    public function indexByJob(JobPoster $jobPoster)
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function indexByJob()
Loading history...
24
    {
25
        $toApiArray = array($this, 'toApiArray');
26
        $taskArray = JobPosterKeyTask::where('job_poster_id', $jobPoster->id)->get()->map($toApiArray);
27
        return response()->json($taskArray);
28
    }
29
30
    /**
31
     * Update the set of tasks associated with a Job.
32
     *
33
     * @param  \App\Http\Requests\BatchUpdateJobTask $request   Incoming form request.
34
     * @param  \App\Models\JobPoster                 $jobPoster Incoming Job Poster object.
35
     * @return \Illuminate\Http\Response
36
     */
37
    public function batchUpdate(BatchUpdateJobTask $request, JobPoster $jobPoster)
38
    {
39
        $toApiArray = array($this, 'toApiArray');
40
41
        $newTasks = collect($request->validated()); // Collection of JobPosterKeyTasks.
42
        $oldTasks = $jobPoster->job_poster_key_tasks;
43
44
        $savedNewTaskIds = [];
45
46
        // First, delete old tasks that weren't resubmitted, and update those that were.
47
        foreach ($oldTasks as $task) {
48
            $newTask = $newTasks->firstWhere('id', $task['id']);
49
            if ($newTask) {
50
                $savedNewTaskIds[] = $newTask['id'];
51
                $task->fill(collect($newTask)->only(['en', 'fr'])->toArray());
52
                $task->save();
53
            } else {
54
                $task->delete();
55
            }
56
        }
57
58
        // Now, save any new tasks that remain.
59
        foreach ($newTasks as $task) {
60
            if ($this->isUnsaved($task, $savedNewTaskIds)) {
61
                $jobPosterTask = new JobPosterKeyTask();
62
                $jobPosterTask->job_poster_id = $jobPoster->id;
63
                $jobPosterTask->fill(collect($task)->only(['en', 'fr'])->toArray());
64
                $jobPosterTask->save();
65
            }
66
        }
67
68
        $taskArray = $jobPoster->fresh()->job_poster_key_tasks->map($toApiArray);
69
        return response()->json($taskArray);
70
    }
71
72
    /**
73
     * Helper function to determine whether a task is unsaved.
74
     *
75
     * @param mixed    $task         Single collection item from new tasks array.
76
     * @param number[] $savedTaskIds Array of saved task IDs.
77
     * @return boolean
78
     */
79
    private function isUnsaved($task, array $savedTaskIds): bool
80
    {
81
        return !array_key_exists('id', $task) || !in_array($task['id'], $savedTaskIds);
82
    }
83
}
84