Passed
Push — feature/job-builder/work-env-r... ( 3641b6...4a7aa4 )
by Tristan
22:01 queued 06:05
created

ApplicantProfileController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 63
c 1
b 0
f 0
dl 0
loc 155
ccs 0
cts 64
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A editAuthenticated() 0 4 1
A show() 0 23 1
A update() 0 49 5
A edit() 0 35 3
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 Illuminate\Support\Facades\Hash;
12
use App\Services\Validation\Requests\UpdateApplicationProfileValidator;
13
14
class ApplicantProfileController extends Controller
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $answerFormInputName = 'applicantProfileAnswer';
20
21
    /**
22
     * Display the specified resource.
23
     *
24
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
25
     * @param  \App\Models\Applicant    $applicant Incoming Applicant object.
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function show(Request $request, Applicant $applicant)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

28
    public function show(/** @scrutinizer ignore-unused */ Request $request, Applicant $applicant)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
    {
30
        /*
31
         * TODO:
32
         * Josh, to loop through answers&question data, leverage this data structure:
33
         * applicant
34
         *     [applicant_profile_answers]
35
         *         answer
36
         *         applicant_profile_question
37
         *             id
38
         *             value // The question text
39
         *             description // Question description text
40
         */
41
42
        return view(
43
            'manager/applicant_profile',
44
            [
45
                // Localization Strings.
46
                'profile' => Lang::get('manager/applicant_profile'), // Change text
47
                // User Data.
48
                'user' => $applicant->user,
49
                'applicant' => $applicant,
50
                'profile_photo_url' => '/images/user.png', // TODO: get real photos.
51
            ]
52
        );
53
    }
54
55
    /**
56
     * Show the form for editing the logged-in applicant's profile
57
     *
58
     * @param  \Illuminate\Http\Request $request Incoming request.
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function editAuthenticated(Request $request)
62
    {
63
        $applicant = $request->user()->applicant;
64
        return redirect(route('profile.about.edit', $applicant));
65
    }
66
67
    /**
68
     * Show the form for editing an applicant profile
69
     *
70
     * @param  \Illuminate\Http\Request $request   Incoming request object.
71
     * @param  \App\Models\Applicant    $applicant Applicant to view and edit.
72
     * @return \Illuminate\Http\Response
73
     */
74
    public function edit(Request $request, Applicant $applicant)
75
    {
76
        $profileQuestions = ApplicantProfileQuestion::all();
77
78
        $profileText = Lang::get('applicant/applicant_profile');
79
80
        $profileQuestionForms = [];
81
        foreach ($profileQuestions as $question) {
82
            $answerObj = $applicant->applicant_profile_answers
83
                ->where('applicant_profile_question_id', $question->id)->first();
84
            $answer = $answerObj ? $answerObj->answer : null;
85
86
            $formValues = [
87
                'id' => $question->id,
88
                'question' => $question->question,
89
                'description' => $question->description,
90
                'answer' => $answer,
91
                'answer_label' => $profileText['about_section']['answer_label'],
92
                'input_name' => $this->answerFormInputName.'['.$question->id.']'
93
            ];
94
            array_push($profileQuestionForms, $formValues);
95
        }
96
97
        return view(
98
            'applicant/profile_01_about',
99
            [
100
                // Localized strings.
101
                'profile' => $profileText,
102
                // Applicant Profile Questions.
103
                'applicant_profile_questions' => $profileQuestionForms,
104
                // User Data.
105
                'user' => $applicant->user,
106
                'applicant' => $applicant,
107
                'profile_photo_url' => '/images/user.png', // TODO: get real photos.
108
                'form_submit_action' => route('profile.about.update', $applicant)
109
            ]
110
        );
111
    }
112
113
    /**
114
     * Update the specified resource in storage.
115
     *
116
     * @param  \Illuminate\Http\Request $request   Incoming request.
117
     * @param  \App\Models\Applicant    $applicant Applicant object to update.
118
     * @return \Illuminate\Http\Response
119
     */
120
    public function update(Request $request, Applicant $applicant)
121
    {
122
        $questions = ApplicantProfileQuestion::all();
123
124
        $validator = new UpdateApplicationProfileValidator($applicant);
125
        $validator->validate($request->all());
126
127
        foreach ($questions as $question) {
128
            $answerName = $this->answerFormInputName . '.' . $question->id;
129
            if ($request->has($answerName)) {
130
                $answer = ApplicantProfileAnswer::where(
131
                    [
132
                        'applicant_id' => $applicant->id,
133
                        'applicant_profile_question_id' => $question->id
134
                    ]
135
                )->first();
136
                if ($answer == null) {
137
                    $answer = new ApplicantProfileAnswer();
138
                    $answer->applicant_id =$applicant->id;
139
                    $answer->applicant_profile_question_id = $question->id;
140
                }
141
                $answer->answer = $request->input($answerName);
142
                $answer->save();
143
            }
144
        }
145
146
        $input = $request->input();
147
        $applicant->fill(
148
            [
149
                'tagline' => $input['tagline'],
150
                'twitter_username' => $input['twitter_username'],
151
                'linkedin_url' => $input['linkedin_url'],
152
            ]
153
        );
154
        $applicant->save();
155
156
        $user = $applicant->user;
157
        $user->fill(
158
            [
159
                'name' => $input['profile_name'],
160
                'email' => $input['profile_email'], // TODO make changing email harder!
161
            ]
162
        );
163
        if ($input['new_password']) {
164
            $user->password =  Hash::make($input['new_password']); // TODO: change password in seperate form!
165
        }
166
        $user->save();
167
168
        return redirect()->route('profile.about.edit', $applicant);
169
    }
170
}
171