EnumDetector   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 10
c 2
b 1
f 0
dl 0
loc 48
ccs 0
cts 9
cp 0
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enumValues() 0 3 1
A authorize() 0 6 4
A detect() 0 9 2
1
<?php
2
3
namespace Terranet\Administrator\Field\Detectors;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Types\StringType;
7
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Terranet\Administrator\Field\Enum;
9
use function admin\db\connection;
10
use function admin\db\enum_values;
11
use function admin\db\translated_values;
12
13
class EnumDetector extends AbstractDetector
14
{
15
    /** @var array */
16
    protected $values = [];
17
18
    /**
19
     * @param  string  $column
20
     * @param  Model  $model
21
     * @return mixed
22
     */
23
    protected function enumValues(string $column, Model $model)
24
    {
25
        return $this->values = enum_values($model->getTable(), $column);
26
    }
27
28
    /**
29
     * Authorize execution.
30
     *
31
     * @param  string  $column
32
     * @param  Column  $metadata
33
     * @param  Model  $model
34
     * @return bool
35
     */
36
    protected function authorize(string $column, Column $metadata, Model $model): bool
37
    {
38
        return connection('mysql')
39
            && $metadata->getType() instanceof StringType
40
            && 0 === (int) $metadata->getLength()
41
            && !empty($this->enumValues($column, $model));
42
    }
43
44
    /**
45
     * Detect field class.
46
     *
47
     * @param  string  $column
48
     * @param  Column  $metadata
49
     * @param  Model  $model
50
     * @return mixed
51
     */
52
    protected function detect(string $column, Column $metadata, Model $model)
53
    {
54
        $values = translated_values($this->values, app('scaffold.module')->url(), $column);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

54
        $values = translated_values($this->values, /** @scrutinizer ignore-call */ app('scaffold.module')->url(), $column);
Loading history...
55
56
        if (!$metadata->getNotNull()) {
57
            $values = ['' => '----'] + $values;
58
        }
59
60
        return Enum::make($column, $column)->setOptions($values);
61
    }
62
}
63