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

ApplicantProfileController::edit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 46
rs 9.44
ccs 0
cts 24
cp 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Http\Request;
7
use App\Models\Lookup\ApplicantProfileQuestion;
8
use App\Models\Applicant;
9
use App\Models\ApplicantProfileAnswer;
10
use App\Http\Controllers\Controller;
11
use App\Models\JobPoster;
12
use App\Services\Validation\Requests\UpdateApplicationProfileValidator;
13
use App\Services\Validation\Rules\LinkedInUrlRule;
14
use App\Services\Validation\Rules\TwitterHandleRule;
15
use Facades\App\Services\WhichPortal;
16
17
class ApplicantProfileController extends Controller
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $answerFormInputName = 'applicantProfileAnswer';
23
24
    /**
25
     * Display the specified resource.
26
     *
27
     * @param  \App\Models\Applicant    $applicant Incoming Applicant object.
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function profile(Applicant $applicant)
31
    {
32
        $custom_breadcrumbs = [
33
            'home' => route('home'),
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

33
            'home' => /** @scrutinizer ignore-call */ route('home'),
Loading history...
34
            $applicant->user->full_name => '',
35
        ];
36
37
        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

37
        return /** @scrutinizer ignore-call */ view(
Loading history...
38
            'manager/applicant_profile',
39
            [
40
                // Localized strings.
41
                'profile' => Lang::get('manager/applicant_profile'), // Change text
42
                // Applicant data.
43
                'applicant' => $applicant,
44
                'profile_photo_url' => '/images/user.png', // TODO: get real photos.
45
                'custom_breadcrumbs' => $custom_breadcrumbs,
46
            ]
47
        );
48
    }
49
50
    /**
51
     * Display the specified resource.
52
     *
53
     * @param  \App\Models\JobPoster    $jobPoster Incoming JobPoster object.
54
     * @param  \App\Models\Applicant    $applicant Incoming Applicant object.
55
     * @return \Illuminate\Http\Response
56
     */
57
    public function showWithJob(JobPoster $jobPoster, Applicant $applicant)
58
    {
59
60
        // Viewing this page is only possible if the applicant has applied to the specified job.
61
        if ($jobPoster->submitted_applications->firstWhere('applicant_id', '==', $applicant->id) === null) {
62
            return abort(404);
0 ignored issues
show
Bug introduced by
The function abort 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

62
            return /** @scrutinizer ignore-call */ abort(404);
Loading history...
63
        }
64
        $custom_breadcrumbs = [
65
            'home' => route('home'),
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

65
            'home' => /** @scrutinizer ignore-call */ route('home'),
Loading history...
66
            'jobs' => route(WhichPortal::prefixRoute('jobs.index')),
67
            $jobPoster->title => route(WhichPortal::prefixRoute('jobs.summary'), $jobPoster),
68
            'applications' =>  route(WhichPortal::prefixRoute('jobs.applications'), $jobPoster),
69
            'profile' => '',
70
        ];
71
72
        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

72
        return /** @scrutinizer ignore-call */ view(
Loading history...
73
            'manager/applicant_profile',
74
            [
75
                // Localized strings.
76
                'profile' => Lang::get('manager/applicant_profile'), // Change text
77
                // Applicant data.
78
                'applicant' => $applicant,
79
                'profile_photo_url' => '/images/user.png', // TODO: get real photos.
80
                'custom_breadcrumbs' => $custom_breadcrumbs,
81
            ]
82
        );
83
    }
84
85
    /**
86
     * Show the form for editing the logged-in applicant's profile
87
     *
88
     * @param  \Illuminate\Http\Request $request Incoming request.
89
     * @return \Illuminate\Http\Response
90
     */
91
    public function editAuthenticated(Request $request)
92
    {
93
        $applicant = $request->user()->applicant;
94
        return redirect(route('profile.about.edit', $applicant));
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

94
        return /** @scrutinizer ignore-call */ redirect(route('profile.about.edit', $applicant));
Loading history...
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

94
        return redirect(/** @scrutinizer ignore-call */ route('profile.about.edit', $applicant));
Loading history...
95
    }
96
97
    /**
98
     * Show the form for editing an applicant profile
99
     *
100
     * @param  \Illuminate\Http\Request $request   Incoming request object.
101
     * @param  \App\Models\Applicant    $applicant Applicant to view and edit.
102
     * @return \Illuminate\Http\Response
103
     */
104
    public function edit(Request $request, Applicant $applicant)
105
    {
106
        $profileQuestions = ApplicantProfileQuestion::all();
107
108
        $profileText = Lang::get('applicant/applicant_profile');
109
110
        $profileQuestionForms = [];
111
        foreach ($profileQuestions as $question) {
112
            $answerObj = $applicant->applicant_profile_answers
113
                ->where('applicant_profile_question_id', $question->id)->first();
114
            $answer = $answerObj ? $answerObj->answer : null;
115
116
            $formValues = [
117
                'id' => $question->id,
118
                'question' => $question->question,
119
                'description' => $question->description,
120
                'answer' => $answer,
121
                'answer_label' => $profileText['about_section']['answer_label'],
122
                'input_name' => $this->answerFormInputName . '[' . $question->id . ']'
123
            ];
124
            array_push($profileQuestionForms, $formValues);
125
        }
126
127
        $linkedInUrlPattern = LinkedInUrlRule::PATTERN;
128
        $twitterHandlePattern = TwitterHandleRule::PATTERN;
129
130
        $custom_breadcrumbs = [
131
            'home' => route('home'),
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

131
            'home' => /** @scrutinizer ignore-call */ route('home'),
Loading history...
132
            'profile' => '',
133
        ];
134
135
        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

135
        return /** @scrutinizer ignore-call */ view(
Loading history...
136
            'applicant/profile_01_about',
137
            [
138
                // Localized strings.
139
                'profile' => $profileText,
140
                // Applicant data.
141
                'applicant' => $applicant,
142
                'profile_photo_url' => '/images/user.png', // TODO: get real photos.
143
                // Applicant Profile Questions.
144
                'applicant_profile_questions' => $profileQuestionForms,
145
                // Update route.
146
                'form_submit_action' => route('profile.about.update', $applicant),
147
                'linkedInUrlPattern' => $linkedInUrlPattern,
148
                'twitterHandlePattern' => $twitterHandlePattern,
149
                'custom_breadcrumbs' => $custom_breadcrumbs,
150
            ]
151
        );
152
    }
153
154
    /**
155
     * Update the specified resource in storage.
156
     *
157
     * @param  \Illuminate\Http\Request $request   Incoming request.
158
     * @param  \App\Models\Applicant    $applicant Applicant object to update.
159
     * @return \Illuminate\Http\Response
160
     */
161
    public function update(Request $request, Applicant $applicant)
162
    {
163
        $questions = ApplicantProfileQuestion::all();
164
165
        $validator = new UpdateApplicationProfileValidator($applicant);
166
        $validator->validate($request->all());
167
168
        foreach ($questions as $question) {
169
            $answerName = $this->answerFormInputName . '.' . $question->id;
170
            if ($request->has($answerName)) {
171
                $answer = ApplicantProfileAnswer::where(
172
                    [
173
                        'applicant_id' => $applicant->id,
174
                        'applicant_profile_question_id' => $question->id
175
                    ]
176
                )->first();
177
                if ($answer == null) {
178
                    $answer = new ApplicantProfileAnswer();
179
                    $answer->applicant_id = $applicant->id;
180
                    $answer->applicant_profile_question_id = $question->id;
181
                }
182
                $answer->answer = $request->input($answerName);
183
                $answer->save();
184
            }
185
        }
186
187
        $input = $request->input();
188
        $applicant->fill(
189
            [
190
                'tagline' => $input['tagline'],
191
                'twitter_username' => $input['twitter_username'],
192
                'linkedin_url' => $input['linkedin_url'],
193
            ]
194
        );
195
        $applicant->save();
196
197
        return redirect()->route('profile.about.edit', $applicant);
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

197
        return /** @scrutinizer ignore-call */ redirect()->route('profile.about.edit', $applicant);
Loading history...
198
    }
199
}
200