Passed
Push — feature/job-status-transitions ( 5d9982...db421e )
by Tristan
08:41 queued 04:46
created

JobSummaryController::show()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 112
Code Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 79
c 1
b 0
f 0
dl 0
loc 112
rs 7.8359
cc 6
nc 12
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\JobPoster;
7
use App\Models\Lookup\JobPosterStatusTransition;
8
use App\Services\JobStatusTransitionManager;
9
use Facades\App\Services\WhichPortal;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Lang;
13
14
class JobSummaryController extends Controller
15
{
16
    /**
17
     * Display the specified job summary.
18
     *
19
     * @param  \Illuminate\Http\Request $request Incoming request object.
20
     * @param  \App\Models\JobPoster $job Job Poster object.
21
     * @return \Illuminate\Http\Response
22
     */
23
    public function show(Request $request, JobPoster $job)
24
    {
25
        $user = Auth::user();
26
27
        $applications = $job->submitted_applications;
28
        $advisor = $user->hr_advisor;
29
        $jobIsClaimed = ($advisor !== null) &&
30
            $advisor->claimed_job_ids->contains($job->id);
31
        $hr_advisors = $job->hr_advisors;
32
33
        $summaryLang = Lang::get('hr_advisor/job_summary');
34
35
        $job_review_data = [
36
            'imgSrc' => '/images/job-process-summary-tool-edit.svg',
37
            'imgAlt' => "{$summaryLang['edit_poster_icon']} {$summaryLang['flat_icons']}",
38
            'text' => $summaryLang['edit_poster_button'],
39
            'url' => route(WhichPortal::prefixRoute('jobs.review'), $job),
0 ignored issues
show
Bug introduced by
The function route 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

39
            'url' => /** @scrutinizer ignore-call */ route(WhichPortal::prefixRoute('jobs.review'), $job),
Loading history...
40
            'disabled' => false,
41
        ];
42
43
        $job_preview_data = [
44
            'imgSrc' => '/images/job-process-summary-tool-view.svg',
45
            'imgAlt' => "{$summaryLang['view_poster_icon']} {$summaryLang['flat_icons']}",
46
            'text' => $summaryLang['view_poster_button'],
47
            'url' => route(WhichPortal::prefixRoute('jobs.preview'), $job),
48
            'disabled' => false,
49
        ];
50
51
        $screening_plan_data = [
52
            'imgSrc' => '/images/job-process-summary-tool-screen.svg',
53
            'imgAlt' => "{$summaryLang['screening_plan_icon']} {$summaryLang['flat_icons']}",
54
            'text' => $summaryLang['screening_plan_button'],
55
            'url' => route(WhichPortal::prefixRoute('jobs.screening_plan'), $job),
56
            'disabled' => false
57
        ];
58
59
        $view_applicants_data = [
60
            'imgSrc' => '/images/job-process-summary-tool-applicants.svg',
61
            'imgAlt' => "{$summaryLang['view_applicants_icon']} {$summaryLang['flat_icons']}",
62
            'text' => $summaryLang['view_applicants_button'],
63
            'url' => route(WhichPortal::prefixRoute('jobs.applications'), $job),
64
            'disabled' => !$job->isClosed(),
65
        ];
66
67
        $status = $job->job_poster_status->name;
68
        $status_description = $job->job_poster_status->description;
69
70
        $portal = '';
71
        if (WhichPortal::isHrPortal()) {
72
            $portal = 'hr';
73
        } elseif (WhichPortal::isManagerPortal()) {
74
            $portal = 'manager';
75
        }
76
77
        $transitionManager = new JobStatusTransitionManager();
78
        $transitionToButton = function (JobPosterStatusTransition $transition) use ($user, $job, $transitionManager) {
79
            return [
80
                'text' => $transition->name,
81
                'url' => route(WhichPortal::prefixRoute('jobs.setJobStatus'), [
0 ignored issues
show
Bug introduced by
The function route 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

81
                'url' => /** @scrutinizer ignore-call */ route(WhichPortal::prefixRoute('jobs.setJobStatus'), [
Loading history...
82
                    'jobPoster' => $job,
83
                    'status' => $transition->to->key
84
                ]),
85
                'style' => array_key_exists('button_style', $transition->metadata)
86
                    ? $transition->metadata['button_style']
87
                    : 'default',
88
                'disabled' => !$transitionManager->userCanTransition($user, $transition->from->key, $transition->to->key)
89
            ];
90
        };
91
        $unclaimButton = [
92
            'unclaim_job' => [
93
                'text' => Lang::get('hr_advisor/job_summary.relinquish_button'),
94
                'url' => route('hr_advisor.jobs.unclaim', $job),
95
                'style' => 'stop',
96
                'disabled' => !$jobIsClaimed,
97
            ]
98
        ];
99
100
        $buttonGroups = [];
101
102
        $transitionButtons = $transitionManager->legalTransitions($job->job_poster_status->key)
103
            ->map($transitionToButton);
104
        array_push($buttonGroups, $transitionButtons);
105
106
        if (WhichPortal::isHrPortal()) {
107
            array_push($buttonGroups, $unclaimButton);
108
        }
109
110
        $data = [
111
            // Localized strings.
112
            'summary' => $summaryLang,
113
            'job_status' => $status,
114
            'job_status_description' => $status_description,
115
            'is_claimed' => $jobIsClaimed,
116
            // User data.
117
            'user' => $user,
118
            // HR Advisor data.
119
            'hr_advisors' => $hr_advisors,
120
            // Job Poster data.
121
            'job' => $job,
122
            // Application data.
123
            'applications' => $applications,
124
            'side_button_groups' => $buttonGroups,
125
            'job_review_data' => $job_review_data,
126
            'job_preview_data' => $job_preview_data,
127
            'screening_plan_data' => $screening_plan_data,
128
            'view_applicants_data' => $view_applicants_data,
129
            'portal' => $portal,
130
        ];
131
132
        return view(
0 ignored issues
show
Bug introduced by
The function view 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

132
        return /** @scrutinizer ignore-call */ view(
Loading history...
133
            'common/job_summary/job_summary',
134
            $data
135
        );
136
    }
137
138
    /**
139
     * Unclaim a Job Poster.
140
     *
141
     * @param  \Illuminate\Http\Request $request Incoming request object.
142
     * @param  \App\Models\JobPoster  $job
143
     * @return \Illuminate\Http\Response
144
     */
145
    public function unclaimJob(Request $request, JobPoster $job)
146
    {
147
        $hrAdvisor = $request->user()->hr_advisor;
148
        $hrAdvisor->claimed_jobs()->detach($job);
149
150
        return redirect()->route('hr_advisor.jobs.index');
0 ignored issues
show
Bug introduced by
The function redirect 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

150
        return /** @scrutinizer ignore-call */ redirect()->route('hr_advisor.jobs.index');
Loading history...
151
    }
152
}
153