RecruitmentCreator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 1
A defaultSourceName() 0 3 1
A reopen() 0 7 1
A close() 0 7 2
A updateRecruitment() 0 7 1
A seedData() 0 14 1
1
<?php
2
3
namespace App\Utils\Recruitments;
4
5
use App\Http\Requests\RecruitmentCloseRequest;
6
use App\Http\Requests\RecruitmentUpdateRequest;
7
use App\Models\Recruitment;
8
use App\Utils\SourceCreator;
9
10
class RecruitmentCreator
11
{
12
    const DEFAULT_SOURCE_NAME = 'Corporate website';
13
14
    public static function create(array $data): Recruitment
15
    {
16
        $recruitment = new Recruitment();
17
        $recruitment->name = $data['name'];
18
        $recruitment->job_title = $data['job_title'];
19
        $recruitment->notification_email = $data['notification_email'];
20
        $recruitment->is_draft = $data['is_draft'] ?? true;
21
        $recruitment->save();
22
23
        self::seedData($recruitment);
24
25
        return Recruitment::with('sources')->with('predefinedMessages')->where('id', $recruitment->id)->get()->first();
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Recrui...nt->id)->get()->first() could return the type null which is incompatible with the type-hinted return App\Models\Recruitment. Consider adding an additional type-check to rule them out.
Loading history...
26
    }
27
28
    private static function seedData(Recruitment $recruitment)
29
    {
30
        SourceCreator::create(['recruitment_id' => $recruitment->id, 'name' => self::defaultSourceName()]);
31
32
        $connection = 'tenant';
33
34
        $seeder = new \StagesSeeder($connection);
35
        $seeder->setRecruitment($recruitment)->run();
36
37
        $seeder = new \PredefinedMessagesSeeder($connection);
38
        $seeder->setRecruitment($recruitment)->run();
39
40
        $seeder = new \FormFieldsSeeder($connection);
41
        $seeder->setRecruitment($recruitment)->run();
42
    }
43
44
    public static function defaultSourceName(): string
45
    {
46
        return self::DEFAULT_SOURCE_NAME;
47
    }
48
49
    public static function updateRecruitment($recruitmentId, RecruitmentUpdateRequest $request)
50
    {
51
        $recruitment = Recruitment::findOrFail($recruitmentId);
52
        $input = $request->validated();
53
        $recruitment->fill($input)->save();
54
55
        return $recruitment;
56
    }
57
58
    public static function close($recruitmentId, RecruitmentCloseRequest $request)
59
    {
60
        $recruitment = Recruitment::findOrFail($recruitmentId);
61
        $recruitment->state = $request->get('keep_form_open') ? Recruitment::STATE_FINISHED : Recruitment::STATE_CLOSED;
0 ignored issues
show
Bug introduced by
The method get() does not exist on App\Http\Requests\RecruitmentCloseRequest. ( Ignorable by Annotation )

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

61
        $recruitment->state = $request->/** @scrutinizer ignore-call */ get('keep_form_open') ? Recruitment::STATE_FINISHED : Recruitment::STATE_CLOSED;

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...
Bug introduced by
The constant App\Models\Recruitment::STATE_FINISHED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant App\Models\Recruitment::STATE_CLOSED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The property state does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
62
        $recruitment->save();
63
64
        return $recruitment;
65
    }
66
67
    public static function reopen($recruitmentId)
68
    {
69
        $recruitment = Recruitment::findOrFail($recruitmentId);
70
        $recruitment->state = Recruitment::STATE_PUBLISHED;
0 ignored issues
show
Bug introduced by
The property state does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
Bug introduced by
The constant App\Models\Recruitment::STATE_PUBLISHED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
71
        $recruitment->save();
72
73
        return $recruitment;
74
    }
75
}
76