Passed
Push — master ( 795d23...149f73 )
by Grant
06:52 queued 12s
created

JobStatusController::transitionJobStatus()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 30
rs 9.0777
cc 6
nc 10
nop 3
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Exceptions\StateMachineException;
6
use App\Models\JobPoster;
7
use App\Http\Resources\JobPoster as JobPosterResource;
8
use App\Models\Lookup\JobPosterStatus;
9
use App\Services\JobStatusTransitionManager;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Lang;
12
13
class JobStatusController extends Controller
14
{
15
    protected function transitionJobStatus(Request $request, JobPoster $job, string $to)
16
    {
17
        $fromStatus = $job->job_poster_status;
18
        $from = $fromStatus->key;
19
20
        // If the job already has the desired status, we can return without doing anything.
21
        if ($from !== $to) {
22
            $user = $request->user();
23
24
            $transitionManager = new JobStatusTransitionManager();
25
26
            // Ensure state transition is legal.
27
            if (!$transitionManager->isLegalTransition($from, $to)) {
28
                throw new StateMachineException(Lang::get('errors.illegal_status_transition', [
29
                    'from' => $from,
30
                    'to' => $to,
31
                ]));
32
            } elseif (!$transitionManager->userCanTransition($user, $from, $to)) {
33
                throw new StateMachineException(Lang::get('errors.user_must_own_status'));
34
            }
35
36
            // Save new status on job.
37
            $toStatus = JobPosterStatus::where('key', $to)->first();
38
            $job->job_poster_status_id = $toStatus->id;
39
            $job->save();
40
        }
41
42
        return ($request->ajax() || $request->wantsJson())
43
            ? new JobPosterResource($job->fresh())
44
            : 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

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