Passed
Push — task/talent-dot-test ( 0ef1e8...074f28 )
by Grant
08:57 queued 03:14
created

ApplicantProfileController::editAuthenticated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Auth\GuardHelpers;
8
use Illuminate\Http\Request;
9
use App\Models\Lookup\ApplicantProfileQuestion;
10
use App\Models\Applicant;
11
use App\Models\ApplicantProfileAnswer;
12
use App\Http\Controllers\Controller;
13
use Illuminate\Support\Facades\Hash;
14
use App\Services\Validation\Requests\UpdateApplicationProfileValidator;
15
16
class ApplicantProfileController extends Controller
17
{
18
19
    protected $answerFormInputName = 'applicantProfileAnswer';
1 ignored issue
show
introduced by
Property \App\Http\Controllers\ApplicantProfileController::$answerFormInputName does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
20
21
    /**
22
     * Display the specified resource.
23
     *
24
     * @param  Request               $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
25
     * @param  \App\Models\Applicant $applicant
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
26
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
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
        //TODO:
31
        //Josh, to loop through answers&question data, leverage this data structure:
32
        // applicant
33
        //     [applicant_profile_answers]
34
        //         answer
35
        //         applicant_profile_question
36
        //             id
37
        //             value // The question text
38
        //             description // Question description text
39
40
        return view(
41
            'manager/applicant_profile',
42
            [
43
                /* Localization Strings*/
44
                'profile' => Lang::get('manager/applicant_profile'), // Change text
45
46
                /* User Data */
47
                'user' => $applicant->user,
48
                'applicant' => $applicant,
49
                'profile_photo_url' => '/images/user.png', //TODO: get real photos
50
            ]
51
        );
52
    }
53
54
    /**
55
     * Show the form for editing the logged-in applicant's profile
56
     *
57
     * @param  Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
58
     * @return \Illuminate\Http\RedirectResponse
59
     */
60
    public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse
61
    {
62
        $applicant = $request->user()->applicant;
63
        return redirect(route('profile.about.edit', $applicant));
64
    }
65
66
    /**
67
     * Show the form for editing an applicant profile
68
     *
69
     * @param  Request               $request   Incoming request object.
70
     * @param  \App\Models\Applicant $applicant Applicant to view and edit.
71
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
72
     */
73
    public function edit(Request $request, Applicant $applicant)
74
    {
75
        $profileQuestions = ApplicantProfileQuestion::all();
76
77
        $profileText = Lang::get('applicant/applicant_profile');
78
79
        $profileQuestionForms = [];
80
        foreach ($profileQuestions as $question) {
81
            $answerObj = $applicant->applicant_profile_answers
82
                ->where('applicant_profile_question_id', $question->id)->first();
83
            $answer = $answerObj ? $answerObj->answer : null;
84
85
            $formValues = [
86
                'id' => $question->id,
87
                'question' => $question->question,
88
                'description' => $question->description,
89
                'answer' => $answer,
90
                'answer_label' => $profileText['about_section']['answer_label'],
91
                'input_name' => $this->answerFormInputName.'['.$question->id.']'
92
            ];
93
            array_push($profileQuestionForms, $formValues);
94
        }
95
96
        return view(
97
            'applicant/profile_01_about',
98
            [
99
            /* Localized strings*/
100
            'profile' => $profileText,
101
            /* Applicant Profile Questions */
102
            'applicant_profile_questions' => $profileQuestionForms,
103
            /* User Data */
104
            'user' => $applicant->user,
105
            'applicant' => $applicant,
106
            'profile_photo_url' => '/images/user.png', //TODO: get real photos
107
108
            'form_submit_action' => route('profile.about.update', $applicant)
109
            ]
110
        );
111
    }
112
113
    /**
114
     * Update the specified resource in storage.
115
     *
116
     * @param  Request               $request   Incoming request.
117
     * @param  \App\Models\Applicant $applicant Applicant object to update.
118
     * @return \Illuminate\Http\RedirectResponse
119
     */
120
    public function update(Request $request, Applicant $applicant): \Illuminate\Http\RedirectResponse
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
                    ['applicant_id' => $applicant->id,
132
                    'applicant_profile_question_id' => $question->id]
133
                )
134
                            ->first();
135
                if ($answer == null) {
136
                    $answer = new ApplicantProfileAnswer();
137
                    $answer->applicant_id =$applicant->id;
138
                    $answer->applicant_profile_question_id = $question->id;
139
                }
140
                $answer->answer = $request->input($answerName);
141
                $answer->save();
142
            }
143
        }
144
145
        $input = $request->input();
146
        $applicant->fill(
147
            [
148
            'tagline' => $input['tagline'],
149
            'twitter_username' => $input['twitter_username'],
150
            'linkedin_url' => $input['linkedin_url'],
151
            ]
152
        );
153
        $applicant->save();
154
155
        $user = $applicant->user;
156
        $user->fill(
157
            [
158
            'name' => $input['profile_name'],
159
            'email' => $input['profile_email'], //TODO make changing email harder!
160
            ]
161
        );
162
        if ($input['new_password']) {
163
            $user->password =  Hash::make($input['new_password']); //TODO: change password in seperate form!
164
        }
165
        $user->save();
166
167
        return redirect()->route('profile.about.edit', $applicant);
168
    }
169
}
170