Test Setup Failed
Push — feature/word_counter ( e4aaca...45e6a8 )
by Yonathan
14:36
created

ApplicantProfileController::update()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 2
dl 0
loc 49
ccs 0
cts 31
cp 0
crap 30
rs 8.8016
c 0
b 0
f 0
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';
20
21
    /**
22
     * Display the specified resource.
23
     *
24
     * @param  Request               $request
25
     * @param  \App\Models\Applicant $applicant
26
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
27
     */
28 View Code Duplication
    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.

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

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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
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)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
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