SourceCreator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateKey() 0 8 2
A create() 0 9 1
1
<?php
2
3
namespace App\Utils;
4
5
use App\Models\Source;
6
use Illuminate\Support\Str;
7
8
class SourceCreator
9
{
10
    const SOURCE_KEY_MAXLENGTH = 6;
11
12
    public static function create(array $data): Source
13
    {
14
        $source = new Source();
15
        $source->name = $data['name'];
16
        $source->recruitment_id = $data['recruitment_id'];
0 ignored issues
show
Bug introduced by
The property recruitment_id does not exist on App\Models\Source. Did you mean recruitment?
Loading history...
17
        $source->key = self::generateKey();
18
        $source->save();
19
20
        return Source::find($source->id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Source::find($source->id) could return the type null which is incompatible with the type-hinted return App\Models\Source. Consider adding an additional type-check to rule them out.
Loading history...
21
    }
22
23
    private static function generateKey()
24
    {
25
        do {
26
            $key = strtoupper(Str::random(self::SOURCE_KEY_MAXLENGTH));
0 ignored issues
show
Bug introduced by
The method random() does not exist on Illuminate\Support\Str. ( Ignorable by Annotation )

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

26
            $key = strtoupper(Str::/** @scrutinizer ignore-call */ random(self::SOURCE_KEY_MAXLENGTH));

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...
27
            $sources = Source::where('key', $key)->count();
28
        } while ($sources > 0);
29
30
        return $key;
31
    }
32
}
33