Completed
Push — bug/accordion ( 6550f4...474aef )
by Grant
12:54 queued 07:32
created

ApplicantProfileController::update()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 48
ccs 0
cts 31
cp 0
rs 9.1288
c 0
b 0
f 0
cc 5
nc 8
nop 2
crap 30
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ApplicantProfileController
Loading history...
17
{
18
19
    protected $answerFormInputName = 'applicantProfileAnswer';
20
21
    /**
22
     * Display the specified resource.
23
     *
24
     * @param  Request               $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
25
     * @param  \App\Models\Applicant $applicant
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
26
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
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(
1 ignored issue
show
Bug Best Practice introduced by
The expression return view('manager/app...=> '/images/user.png')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
41
            'manager/applicant_profile',
42
            [
43
                /* Localized 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 user's applicant profile
56
     *
57
     * @param  Request               $request   Incoming request object.
58
     * @param  \App\Models\Applicant $applicant Applicant to view and edit.
59
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
60
     */
61
    public function edit(Request $request, Applicant $applicant)
62
    {
63
        $profileQuestions = ApplicantProfileQuestion::all();
64
65
        $profileText = Lang::get('applicant/applicant_profile');
66
67
        $profileQuestionForms = [];
68
        foreach ($profileQuestions as $question) {
69
            $answerObj = $applicant->applicant_profile_answers
70
                ->where('applicant_profile_question_id', $question->id)->first();
71
            $answer = $answerObj ? $answerObj->answer : null;
72
73
            $formValues = [
74
                'id' => $question->id,
75
                'question' => $question->question,
76
                'description' => $question->description,
77
                'answer' => $answer,
78
                'answer_label' => $profileText['about_section']['answer_label'],
79
                'input_name' => $this->answerFormInputName.'['.$question->id.']'
80
            ];
81
            array_push($profileQuestionForms, $formValues);
82
        }
83
84
        return view(
1 ignored issue
show
Bug Best Practice introduced by
The expression return view('applicant/p....update', $applicant))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
85
            'applicant/profile_01_about', [
86
            /* Localized strings*/
87
            'profile' => $profileText,
88
            /* Applicant Profile Questions */
89
            'applicant_profile_questions' => $profileQuestionForms,
90
            /* User Data */
91
            'user' => $applicant->user,
92
            'applicant' => $applicant,
93
            'profile_photo_url' => '/images/user.png', //TODO: get real photos
94
95
            'form_submit_action' => route('profile.about.update', $applicant)
96
            ]
97
        );
98
    }
99
100
    /**
101
     * Update the specified resource in storage.
102
     *
103
     * @param  Request               $request   Incoming request.
104
     * @param  \App\Models\Applicant $applicant Applicant object to update.
105
     * @return \Illuminate\Http\RedirectResponse
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
106
     */
107
    public function update(Request $request, Applicant $applicant)
108
    {
109
        $questions = ApplicantProfileQuestion::all();
110
111
        $validator = new UpdateApplicationProfileValidator($applicant);
112
        $validator->validate($request->all());
113
114
        foreach ($questions as $question) {
115
            $answerName = $this->answerFormInputName . '.' . $question->id;
116
            if ($request->has($answerName)) {
117
                $answer = ApplicantProfileAnswer::where(
118
                    ['applicant_id' => $applicant->id,
119
                    'applicant_profile_question_id' => $question->id]
120
                )
121
                            ->first();
122
                if ($answer == null) {
123
                    $answer = new ApplicantProfileAnswer();
124
                    $answer->applicant_id =$applicant->id;
125
                    $answer->applicant_profile_question_id = $question->id;
126
                }
127
                $answer->answer = $request->input($answerName);
128
                $answer->save();
129
            }
130
        }
131
132
        $input = $request->input();
133
        $applicant->fill(
134
            [
135
            'tagline' => $input['tagline'],
136
            'twitter_username' => $input['twitter_username'],
137
            'linkedin_url' => $input['linkedin_url'],
138
            ]
139
        );
140
        $applicant->save();
141
142
        $user = $applicant->user;
143
        $user->fill(
144
            [
145
            'name' => $input['profile_name'],
146
            'email' => $input['profile_email'], //TODO make changing email harder!
147
            ]
148
        );
149
        if ($input['new_password']) {
150
            $user->password =  Hash::make($input['new_password']); //TODO: change password in seperate form!
151
        }
152
        $user->save();
153
154
        return redirect()->route('profile.about.edit', $applicant);
155
    }
156
}
157