Passed
Push — feature/job-builder/tasks-redu... ( c339d7...5b5e8a )
by Tristan
12:46
created

JobTaskController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 27
c 1
b 0
f 0
dl 0
loc 64
ccs 27
cts 28
cp 0.9643
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A indexByJob() 0 5 1
A toApiArray() 0 3 1
B batchUpdate() 0 37 6
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
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 3
    public function toApiArray(JobPosterKeyTask $model)
19
    {
20 3
        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  App\Http\Requests\BatchUpdateJobTask  $request
2 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Api...ests\BatchUpdateJobTask was not found. Did you mean App\Http\Requests\BatchUpdateJobTask? If so, make sure to prefix the type with \.
Loading history...
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 16 spaces after parameter type; 2 found
Loading history...
35
     * @return \Illuminate\Http\Response
36
     */
37 2
    public function batchUpdate(BatchUpdateJobTask $request, JobPoster $jobPoster)
38
    {
39 2
        $toApiArray = array($this, 'toApiArray');
40
41 2
        $newTasks = collect($request->validated()); // Collection of JobPosterKeyTasks
42 2
        $oldTasks = $jobPoster->job_poster_key_tasks;
43
44 2
        $savedNewTaskIds = [];
45
46
        // First, delete old tasks that weren't resubmitted, and update those that were
47 2
        foreach ($oldTasks as $task) {
48 1
            $newTask = $newTasks->firstWhere('id', $task['id']);
49 1
            if ($newTask) {
50 1
                $savedNewTaskIds[] = $newTask['id'];
51 1
                $task->fill(collect($newTask)->only(['en', 'fr'])->toArray());
52 1
                $task->save();
53
            } else {
54
                $task->delete();
55
            }
56
        }
57
58
        $isUnsaved = function ($task, $savedTaskIds): bool {
59 2
            return !array_key_exists('id', $task) || !in_array($task['id'], $savedTaskIds);
60 2
        };
61
62
        // Now, save any new tasks that remain
63 2
        foreach ($newTasks as $task) {
64 2
            if ($isUnsaved($task, $savedNewTaskIds)) {
65 1
                $jobPosterTask = new JobPosterKeyTask();
66 1
                $jobPosterTask->job_poster_id = $jobPoster->id;
67 1
                $jobPosterTask->fill(collect($task)->only(['en', 'fr'])->toArray());
68 1
                $jobPosterTask->save();
69
            }
70
        }
71
72 2
        $taskArray = $jobPoster->fresh()->job_poster_key_tasks->map($toApiArray);
73 2
        return response()->json($taskArray);
74
    }
75
}
76