Passed
Push — feature/job-status-transitions ( 688dc7...931d64 )
by Tristan
11:00 queued 06:03
created

JobStatusController::legalTransition()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Exceptions\StateMachineException;
6
use App\Models\JobPoster;
7
use App\Models\JobPosterStatusHistory;
8
use App\Models\Lookup\JobPosterStatus;
9
use App\Services\JobStatusTransitions;
10
use Illuminate\Http\Request;
11
12
class JobStatusController extends Controller
13
{
14
    protected function transitionJobStatus(Request $request, JobPoster $job, string $to)
15
    {
16
        $transitions = new JobStatusTransitions();
17
18
        $user = $request->user();
19
        $fromStatus = $job->job_poster_status;
20
        $from = $fromStatus->name;
21
22
        // Ensure state transition is legal.
23
        if (!$transitions->canTransition($user, $from, $to)) {
24
            throw new StateMachineException('Illegal state transition');
25
        }
26
27
        // Save new status on job.
28
        $toStatus = JobPosterStatus::where('name', $to)->first();
29
        $job->job_poster_status_id = $toStatus->id;
30
        $job->save();
31
32
        // Save transition history.
33
        $transition = new JobPosterStatusHistory();
34
        $transition->job_poster_id = $job->id;
35
        $transition->user_id = $user->id;
36
        $transition->from_job_poster_status_id = $fromStatus->id;
37
        $transition->to_job_poster_status_id = $toStatus->id;
38
        $transition->save();
39
40
        return $request->ajax()
41
            ? response()->json(['status' => 'ok'])
0 ignored issues
show
Bug introduced by
The function response 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

41
            ? /** @scrutinizer ignore-call */ response()->json(['status' => 'ok'])
Loading history...
42
            : back();
0 ignored issues
show
Bug introduced by
The function back 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

42
            : /** @scrutinizer ignore-call */ back();
Loading history...
43
    }
44
45
    public function setJobStatus(Request $request, JobPoster $jobPoster, string $status)
46
    {
47
        // $status = $request->input('status');
48
        return $this->transitionJobStatus($request, $jobPoster, $status);
49
    }
50
}
51