CandidateCreator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 40
c 2
b 1
f 0
dl 0
loc 62
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fakeCandidate() 0 10 1
B createCandidate() 0 48 8
1
<?php
2
3
namespace App\Utils\Candidates;
4
5
use App\Http\Requests\CandidatesCreateRequest;
6
use App\Models\Candidate;
7
use App\Models\Recruitment;
8
use App\Models\Source;
9
use App\Services\TenantManager;
10
use App\Utils\StageHelper;
11
12
class CandidateCreator
13
{
14
    public static function createCandidate(CandidatesCreateRequest $request, TenantManager $tenantManager)
15
    {
16
        $key = $request->get('key');
0 ignored issues
show
Bug introduced by
The method get() does not exist on App\Http\Requests\CandidatesCreateRequest. ( Ignorable by Annotation )

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

16
        /** @scrutinizer ignore-call */ 
17
        $key = $request->get('key');

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...
17
18
        if (!empty($key)) {
19
            $source = Source::where('key', $key)->firstOrFail();
20
            if ($source) {
0 ignored issues
show
introduced by
$source is of type App\Models\Source, thus it always evaluated to true.
Loading history...
21
                $recruitment = $source->recruitment;
22
            }
23
        } else {
24
            //TODO podanie rectruitment_id powinno być dozwolone tylko z poziomu panelu, a nie publicznej rekrutacji
25
            $recruitment_id = $request->get('recruitment_id');
26
            $recruitment = Recruitment::find($recruitment_id);
27
        }
28
29
        if (empty($recruitment)) {
30
            return false; //TODO walidacja powinna być wyżej (chyba chodzilo mi o to, ze gdzies na poziomie requestu albo controllera)
31
        }
32
33
        if ($request->file) {
0 ignored issues
show
Bug introduced by
The property file does not seem to exist on App\Http\Requests\CandidatesCreateRequest.
Loading history...
34
            $path_to_cv = $request->file->store($tenantManager->getTenant()->subdomain.'/'.$recruitment->id, 's3');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $recruitment does not seem to be defined for all execution paths leading up to this point.
Loading history...
35
        } else {
36
            $path_to_cv = '';
37
        }
38
39
        $candidate = new Candidate();
40
        $candidate->name = $request->get('name');
41
        $candidate->email = $request->get('email');
42
        $candidate->phone_number = $request->get('phone_number');
43
        $candidate->future_agreement = (bool) data_get($request->validated(), 'future_agreement');
44
        $candidate->source_recruitment_id = $request->get('source_recruitment_id');
45
        $candidate->stage_id = StageHelper::getFirstStage($recruitment->id)->id;
46
        $candidate->path_to_cv = $path_to_cv;
47
        $candidate->source_id = !empty($source) ? $source->id : null;
48
        $candidate->recruitment_id = $recruitment->id;
0 ignored issues
show
Bug introduced by
The property recruitment_id does not exist on App\Models\Candidate. Did you mean recruitment?
Loading history...
49
50
        $customData = [];
51
        foreach ($recruitment->formFields as $field) {
52
            if (!$field->system) {
53
                $customData[] = ['id' => $field->id, 'label' => $field->label, 'value' => $request->get($field->name)];
54
            }
55
        }
56
        $candidate->custom_fields = json_encode($customData);
57
58
        $candidate->save();
59
        $candidate = Candidate::find($candidate->id);
60
61
        return $candidate;
62
    }
63
64
    public static function fakeCandidate($recruitment)
65
    {
66
        $candidate = new Candidate();
67
        $candidate->name = 'Jan Nowak';
68
        $candidate->email = '[email protected]';
69
        $candidate->phone_number = '+48 600 600 600';
70
        $candidate->future_agreement = false;
71
        $candidate->recruitment_id = $recruitment->id;
0 ignored issues
show
Bug introduced by
The property recruitment_id does not exist on App\Models\Candidate. Did you mean recruitment?
Loading history...
72
73
        return $candidate;
74
    }
75
}
76