Test Failed
Push — master ( 180f30...aa27d2 )
by Terzi
04:24
created

Architect::castedEnumValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 3
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator;
4
5
use BenSampo\Enum\Traits\CastsEnums;
0 ignored issues
show
Bug introduced by
The type BenSampo\Enum\Traits\CastsEnums 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...
6
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...
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
use Zend\Code\Reflection\ClassReflection;
10
11
class Architect
12
{
13
    /**
14
     * Get the AdminArchitect URI path.
15
     *
16
     * @return \Illuminate\Config\Repository|mixed
0 ignored issues
show
Bug introduced by
The type Illuminate\Config\Repository 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...
17
     */
18
    public static function path()
19
    {
20
        return config('administrator.prefix', 'cms');
0 ignored issues
show
Bug introduced by
The function config 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

20
        return /** @scrutinizer ignore-call */ config('administrator.prefix', 'cms');
Loading history...
21
    }
22
23
    /**
24
     * Finds first module which uses a model.
25
     *
26
     * @param  Model|string  $model
27
     * @param  string  $urlKey
28
     *
29
     * @return mixed
30
     */
31
    public static function resourceByEntity($model, string $urlKey = null)
32
    {
33
        return app('scaffold.modules')->first(function ($module) use ($model, $urlKey) {
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

33
        return /** @scrutinizer ignore-call */ app('scaffold.modules')->first(function ($module) use ($model, $urlKey) {
Loading history...
34
            $urlEquals = $urlKey ? $module->url() === $urlKey : true;
35
36
            if (\is_string($model)) {
37
                return \get_class($module->model()) === $model && $urlEquals;
38
            }
39
40
            return \get_class($module->model()) === \get_class($model) && $urlEquals;
41
        });
42
    }
43
44
    /**
45
     * Humanize the given value into a proper name.
46
     *
47
     * @param  string  $value
48
     *
49
     * @return string
50
     */
51
    public static function humanize($value)
52
    {
53
        if (\is_object($value)) {
0 ignored issues
show
introduced by
The condition is_object($value) is always false.
Loading history...
54
            return static::humanize(class_basename(\get_class($value)));
55
        }
56
57
        return str_replace('_', ' ', Str::title(Str::snake($value, ' ')));
58
    }
59
60
    /**
61
     * @return ArchitectRoutes
62
     */
63
    public static function routes()
64
    {
65
        return new ArchitectRoutes();
66
    }
67
68
    /**
69
     * Ensure the column is of type Enum.
70
     *
71
     * @param  Model  $model
72
     * @param  string  $column
73
     * @return bool
74
     */
75
    public static function castedEnumType(Model $model, string $column)
76
    {
77
        return Arr::has(class_uses($model), CastsEnums::class) && $model->hasEnumCast($column);
78
    }
79
80
    /**
81
     * Extract values from Casted Enum type.
82
     *
83
     * @param  Model  $model
84
     * @param  string  $column
85
     * @param  bool  $nullable
86
     * @return array
87
     * @throws \ReflectionException
88
     */
89
    public static function castedEnumValues(Model $model, string $column, bool $nullable = false)
90
    {
91
        $reflection = new ClassReflection($model);
92
93
        $property = tap($reflection->getProperty('enumCasts'))->setAccessible(true);
94
95
        $enum = $property->getValue(new $model)[$column];
96
97
        $values = $enum::getKeys();
98
        if ($nullable) {
99
            $values = ['' => '----'] + $values;
100
        }
101
102
        return $values;
103
    }
104
}
105