Test Setup Failed
Branch feature/job-builder/work-env-r... (305963)
by Grant
52:57 queued 34:52
created

ReferencesController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 1
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Http\Request;
7
use App\Http\Controllers\Controller;
8
use App\Models\Applicant;
9
use App\Models\Reference;
10
use App\Models\Project;
11
use App\Services\Validation\Requests\UpdateReferenceValidator;
12
13
class ReferencesController extends Controller
14
{
15
16
    /**
17
     * Show the form for editing the logged-in applicant's references
18
     *
19
     * @param  \Illuminate\Http\Request $request Incoming request object.
20
     * @return \Illuminate\Http\Response
21
     */
22 1
    public function editAuthenticated(Request $request)
23
    {
24 1
        $applicant = $request->user()->applicant;
25 1
        return redirect(route('profile.references.edit', $applicant));
26
    }
27
28
    /**
29
     * Show the form for editing the applicant's references
30
     *
31
     * @param  \Illuminate\Http\Request $request   Incoming request object.
32
     * @param  \App\Models\Applicant    $applicant Incoming applicant object.
33
     * @return \Illuminate\Http\Response
34
     */
35 1
    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...
36
    {
37 1
        $applicant->load([
38 1
            'references.projects',
39
            'skill_declarations.skill',
40
        ]);
41
42 1
        return view('applicant/profile_04_references', [
43 1
            'applicant' => $applicant,
44 1
            'profile' => Lang::get('applicant/profile_references'),
45
        ]);
46
    }
47
48
    /**
49
     * Update or create a reference with the supplied data.
50
     *
51
     * @param  \Illuminate\Http\Request   $request   The incoming request object.
52
     * @param  \App\Models\Reference|null $reference The reference to update. If null, a new one should be created.
53
     * @return \Illuminate\Http\Response
54
     */
55 2
    public function update(Request $request, ?Reference $reference = null)
56
    {
57 2
        $validator = new UpdateReferenceValidator();
58 2
        $validator->validate($request->input());
0 ignored issues
show
Documentation introduced by
$request->input() is of type string|array|null, but the function expects a array<integer,*>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
60 2
        if ($reference === null) {
61 1
            $reference = new Reference();
62 1
            $reference->applicant_id = $request->user()->applicant->id;
63
        }
64 2
        $reference->fill([
65 2
            'name' => $request->input('name'),
66 2
            'email' => $request->input('email'),
67 2
            'relationship_id' => $request->input('relationship_id'),
68 2
            'description' => $request->input('description'),
69
        ]);
70 2
        $reference->save();
71
72 2
        $reference->load('projects');
73
74
        // TODO: As soon as you can interact with projects outside of references,
75
        // this will become a dangerous operation.
76 2
        $reference->projects()->delete();
77
78 2
        $newProjects = [];
79 2
        if ($request->input('projects')) {
80 2
            foreach ($request->input('projects') as $projectInput) {
0 ignored issues
show
Bug introduced by
The expression $request->input('projects') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
81 2
                $project = new Project();
82 2
                $project->applicant_id = $reference->applicant_id;
83 2
                $project->fill([
84 2
                    'name' => $projectInput['name'],
85 2
                    'start_date' => $projectInput['start_date'],
86 2
                    'end_date' => $projectInput['end_date'],
87
                ]);
88 2
                $project->save();
89 2
                $newProjects[] = $project->id;
90
            }
91
        }
92 2
        $reference->projects()->sync($newProjects);
93
94
        // Attach relatives.
95 2
        $skillIds = $this->getRelativeIds($request->input(), 'skills');
96 2
        $reference->skill_declarations()->sync($skillIds);
97
98
        // If an ajax request, return the new object.
99 2
        if ($request->ajax()) {
100
            $reference->load('relationship');
101
            $reference->load('projects');
102
            return $reference->toJson();
103
        } else {
104 2
            return redirect()->back();
105
        }
106
    }
107
108
    /**
109
     * Delete the particular reference from storage.
110
     *
111
     * @param  \Illuminate\Http\Request $request   Incoming Request.
112
     * @param  \App\Models\Reference    $reference Incoming Reference.
113
     * @return \Illuminate\Http\Response
114
     */
115 1
    public function destroy(Request $request, Reference $reference)
116
    {
117 1
        $this->authorize('delete', $reference);
118
119
        // TODO: when projects exist independently on profile, delete separately.
120 1
        $reference->projects()->delete();
121
122 1
        $reference->delete();
123
124 1
        if ($request->ajax()) {
125
            return [
126
                'message' => 'Reference deleted'
127
            ];
128
        }
129
130 1
        return redirect()->back();
131
    }
132
}
133