CandidatesController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 40
c 5
b 2
f 0
dl 0
loc 119
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A changeStage() 0 5 1
A cv() 0 15 3
A delete() 0 5 1
A create() 0 5 1
A get() 0 3 1
A update() 0 5 1
A __construct() 0 3 1
A hasBeenSeen() 0 8 2
A list() 0 24 4
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\CandidateDeleteRequest;
6
use App\Http\Requests\CandidatesCreateRequest;
7
use App\Http\Requests\CandidatesListRequest;
8
use App\Http\Requests\CandidateUpdateRequest;
9
use App\Http\Requests\ChangeStageRequest;
10
use App\Http\Resources\CandidateResource;
11
use App\Http\Resources\TruncatedCandidateResource;
12
use App\Models\Candidate;
13
use App\Repositories\CandidatesRepository;
14
use App\Services\TenantManager;
15
use App\Utils\Candidates\CandidateCreator;
16
use App\Utils\Candidates\CandidateDeleter;
17
use App\Utils\Candidates\CandidateUpdater;
18
use App\Utils\Candidates\StageChanger;
19
use Illuminate\Http\Request;
20
use Illuminate\Support\Facades\Auth;
21
use Illuminate\Support\Facades\Storage;
22
23
class CandidatesController extends Controller
24
{
25
    /**
26
     * @var TenantManager
27
     */
28
    protected $tenantManager;
29
30
    /**
31
     * Create a new controller instance.
32
     *
33
     * @param TenantManager $tenantManager
34
     *
35
     * @return void
36
     */
37
    public function __construct(TenantManager $tenantManager)
38
    {
39
        $this->tenantManager = $tenantManager;
40
    }
41
42
    public function create(CandidatesCreateRequest $request)
43
    {
44
        $candidate = CandidateCreator::createCandidate($request, $this->tenantManager);
45
46
        return response()->json($candidate, 201);
47
    }
48
49
    public function list(CandidatesListRequest $request)
50
    {
51
        $recruitmentId = $request->get('recruitmentId');
0 ignored issues
show
Bug introduced by
The method get() does not exist on App\Http\Requests\CandidatesListRequest. ( Ignorable by Annotation )

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

51
        /** @scrutinizer ignore-call */ 
52
        $recruitmentId = $request->get('recruitmentId');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        $user = Auth::user();
53
54
        if ($request->get('search')) {
55
            $candidates = CandidatesRepository::search($request->validated());
56
        } elseif ($recruitmentId) {
57
            $candidates = Candidate::where('recruitment_id', $recruitmentId)->with('recruitment')->orderBy('created_at', 'DESC')->get();
58
        } else {
59
            $candidates = Candidate::with('recruitment')->orderBy('created_at', 'DESC')->get();
60
        }
61
62
        if (!$user->can('read all recruitments')) {
63
            $filtered = $candidates->filter(
64
                function ($candidate, $key) use ($user) {
0 ignored issues
show
Unused Code introduced by
The parameter $key 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

64
                function ($candidate, /** @scrutinizer ignore-unused */ $key) use ($user) {

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...
65
                    return $user->can('view', $candidate);
66
                }
67
            );
68
69
            $candidates = $filtered;
70
        }
71
72
        return TruncatedCandidateResource::collection($candidates);
73
    }
74
75
//    DEPRECATED
76
//    public function names(Request $request)
77
//    {
78
//        $search = $request->get('search');
79
//        $columns = ['id', 'name'];
80
//
81
//        if ($search) {
82
//            $candidates = CandidatesRepository::search(['search' => $search], $columns);
83
//        } else {
84
//            $candidates = Candidate::select($columns)->get();
85
//        }
86
//        return response()->json($candidates);
87
//    }
88
89
    public function get(Candidate $candidate)
90
    {
91
        return new CandidateResource($candidate);
92
    }
93
94
    public function update(CandidateUpdateRequest $request, Candidate $candidate)
95
    {
96
        $candidate = CandidateUpdater::updateCandidate($candidate->id, $request);
97
98
        return response()->json($candidate, 200, ['Location' => '/candidates/'.$candidate->id]);
99
    }
100
101
    public function delete(CandidateDeleteRequest $request, Candidate $candidate)
102
    {
103
        CandidateDeleter::deleteCandidate($request, $candidate->id);
104
105
        return response()->json(null, 200);
106
    }
107
108
    public function hasBeenSeen(Candidate $candidate)
109
    {
110
        if (!$candidate->seen_at) {
111
            $candidate->seen_at = new \DateTime();
112
            $candidate->save();
113
        }
114
115
        return response()->json($candidate, 200, ['Location' => '/candidates/'.$candidate->id]);
116
    }
117
118
    //TODO: route nie chroniony - docelowo zrobić zabezpieczenie z użyciem jednorazowych tokenów, a także nie przekazywać do klienta pola path_to_cv
119
    public function cv(Request $request, $candidateId)
120
    {
121
        $download = $request->get('download');
0 ignored issues
show
Bug introduced by
The method get() does not exist on Illuminate\Http\Request. ( Ignorable by Annotation )

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

121
        /** @scrutinizer ignore-call */ 
122
        $download = $request->get('download');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
        $candidate = Candidate::find($candidateId);
123
124
        if (!Storage::disk('s3')->exists($candidate->path_to_cv)) {
0 ignored issues
show
Bug introduced by
It seems like $candidate->path_to_cv can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation and Illuminate\Support\HigherOrderCollectionProxy; however, parameter $path of Illuminate\Filesystem\FilesystemAdapter::exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

124
        if (!Storage::disk('s3')->exists(/** @scrutinizer ignore-type */ $candidate->path_to_cv)) {
Loading history...
125
            return response('File not found', 404);
126
        }
127
128
        $fileName = str_replace(' ', '_', $candidate->name).'-CV.pdf'; //TODO sanitize
129
130
        if ($download) {
131
            return Storage::disk('s3')->download($candidate->path_to_cv, $fileName);
0 ignored issues
show
Bug introduced by
It seems like $candidate->path_to_cv can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation and Illuminate\Support\HigherOrderCollectionProxy; however, parameter $path of Illuminate\Filesystem\Fi...stemAdapter::download() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

131
            return Storage::disk('s3')->download(/** @scrutinizer ignore-type */ $candidate->path_to_cv, $fileName);
Loading history...
132
        } else {
133
            return Storage::disk('s3')->response($candidate->path_to_cv, $fileName);
0 ignored issues
show
Bug introduced by
It seems like $candidate->path_to_cv can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation and Illuminate\Support\HigherOrderCollectionProxy; however, parameter $path of Illuminate\Filesystem\Fi...stemAdapter::response() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

133
            return Storage::disk('s3')->response(/** @scrutinizer ignore-type */ $candidate->path_to_cv, $fileName);
Loading history...
134
        }
135
    }
136
137
    public function changeStage(ChangeStageRequest $request)
138
    {
139
        $candidate = StageChanger::changeStage($request);
140
141
        return response()->json($candidate, 200, ['Location' => '/candidates/'.$candidate->id]);
142
    }
143
}
144