Test Failed
Pull Request — master (#77)
by Terzi
05:49
created

Architect::resourceForKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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 Terranet\Administrator\Services\Template;
10
use Zend\Code\Reflection\ClassReflection;
11
12
class Architect
13
{
14
    /**
15
     * Get the AdminArchitect URI path.
16
     *
17
     * @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...
18
     */
19
    public static function path()
20
    {
21
        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

21
        return /** @scrutinizer ignore-call */ config('administrator.prefix', 'cms');
Loading history...
22
    }
23
24
    /**
25
     * Finds first module which uses a model.
26
     *
27
     * @param  Model|string  $model
28
     * @param  string  $urlKey
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
     * Finds resource by a key.
46
     *
47
     * @param  string  $url
48
     * @return mixed
49
     */
50
    public static function resourceForKey(string $url)
51
    {
52
        return app('scaffold.modules')->first(function ($module) use ($url) {
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

52
        return /** @scrutinizer ignore-call */ app('scaffold.modules')->first(function ($module) use ($url) {
Loading history...
53
            return $module->url() === $url;
54
        });
55
    }
56
57
    /**
58
     * Humanize the given value into a proper name.
59
     *
60
     * @param  string  $value
61
     * @return string
62
     */
63
    public static function humanize($value)
64
    {
65
        if (\is_object($value)) {
0 ignored issues
show
introduced by
The condition is_object($value) is always false.
Loading history...
66
            return static::humanize(class_basename(\get_class($value)));
67
        }
68
69
        return str_replace('_', ' ', Str::title(Str::snake($value, ' ')));
70
    }
71
72
    /**
73
     * @return ArchitectRoutes
74
     */
75
    public static function routes()
76
    {
77
        return new ArchitectRoutes();
78
    }
79
80
    /**
81
     * Ensure the column is of type Enum.
82
     *
83
     * @param  Model  $model
84
     * @param  string  $column
85
     * @return bool
86
     */
87
    public static function castedEnumType(Model $model, string $column)
88
    {
89
        return Arr::has(class_uses($model), CastsEnums::class) && $model->hasEnumCast($column);
90
    }
91
92
    /**
93
     * Extract values from Casted Enum type.
94
     *
95
     * @param  Model  $model
96
     * @param  string  $column
97
     * @param  bool  $nullable
98
     * @return array
99
     * @throws \ReflectionException
100
     */
101
    public static function castedEnumValues(Model $model, string $column, bool $nullable = false)
102
    {
103
        $reflection = new ClassReflection($model);
104
105
        $property = tap($reflection->getProperty('enumCasts'))->setAccessible(true);
106
107
        $enum = $property->getValue(new $model)[$column];
108
109
        $values = $enum::toSelectArray();
110
        if ($nullable) {
111
            $values = ['' => '----'] + $values;
112
        }
113
114
        return $values;
115
    }
116
117
    public static function template()
118
    {
119
        return new Template();
120
    }
121
}
122