Passed
Push — feature/job-builder/tasks-redu... ( d8be92...c339d7 )
by Tristan
13:07
created

JobTaskController::toApiArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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