Grid::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Terranet\Administrator\Decorators;
4
5
use Czim\Paperclip\Contracts\AttachableInterface;
0 ignored issues
show
Bug introduced by
The type Czim\Paperclip\Contracts\AttachableInterface 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 Terranet\Administrator\Chain;
8
use Terranet\Administrator\Field\Detectors\BooleanDetector;
9
use Terranet\Administrator\Field\Detectors\CastedEnumDetector;
10
use Terranet\Administrator\Field\Detectors\DateTimeDetector;
11
use Terranet\Administrator\Field\Detectors\EmailDetector;
12
use Terranet\Administrator\Field\Detectors\EnumDetector;
13
use Terranet\Administrator\Field\Detectors\LinkDetector;
14
use Terranet\Administrator\Field\Detectors\NumberDetector;
15
use Terranet\Administrator\Field\Detectors\PasswordDetector;
16
use Terranet\Administrator\Field\Detectors\PhoneDetector;
17
use Terranet\Administrator\Field\Detectors\PrimaryKeyDetector;
18
use Terranet\Administrator\Field\Detectors\RankableDetector;
19
use Terranet\Administrator\Field\Detectors\TextareaDetector;
20
use Terranet\Administrator\Field\Detectors\TextDetector;
21
use Terranet\Administrator\Field\Field;
22
use Terranet\Administrator\Field\File;
23
use Terranet\Administrator\Field\Image;
24
use Terranet\Administrator\Field\Text;
25
use Terranet\Translatable\Translatable;
26
27
class Grid
28
{
29
    /** @var Model */
30
    private $model;
31
32
    /**
33
     * Grid constructor.
34
     *
35
     * @param null|Model $model
36
     */
37
    public function __construct(Model $model = null)
38
    {
39
        $this->model = $model ?: app('scaffold.module')->model();
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

39
        $this->model = $model ?: /** @scrutinizer ignore-call */ app('scaffold.module')->model();
Loading history...
40
    }
41
42
    /**
43
     * @param $element
44
     *
45
     * @return mixed|\Terranet\Administrator\Field\Field
46
     */
47
    public function make($element)
48
    {
49
        // decorate attachment
50
        if ($this->model instanceof AttachableInterface
51
            && method_exists($this->model, 'getAttachedFiles')
52
            && array_key_exists($element, $this->model->getAttachedFiles())
53
        ) {
54
            $file = $this->model->getAttachedFiles()[$element];
55
            if ($file->variants()) {
56
                return Image::make($element, $element);
57
            }
58
59
            return File::make($element, $element);
60
        }
61
62
        // decorate translatable attachment
63
        if ($this->model instanceof Translatable) {
64
            $translation = $this->model->getTranslationModel();
0 ignored issues
show
Bug introduced by
The method getTranslationModel() does not exist on Terranet\Translatable\Translatable. ( Ignorable by Annotation )

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

64
            /** @scrutinizer ignore-call */ 
65
            $translation = $this->model->getTranslationModel();

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...
65
66
            if ($translation instanceof AttachableInterface
67
                && method_exists($translation, 'getAttachedFiles')
68
                && array_key_exists($element, $translation->getAttachedFiles())
69
            ) {
70
                return Image::make($element);
71
            }
72
        }
73
74
        // decorate table column
75
        if ($this->realColum($element)) {
76
            $field = $this->detectField($element);
77
78
            if (\is_object($field)) {
79
                return $field;
80
            }
81
82
            return forward_static_call_array([$field, 'make'], [$element, $element]);
83
        }
84
85
        return Text::make($element, $element);
86
    }
87
88
    /**
89
     * @param $column
90
     *
91
     * @return bool
92
     */
93
    protected function realColum($column)
94
    {
95
        return array_key_exists($column, $this->fetchTablesColumns());
96
    }
97
98
    /**
99
     * @param $column
100
     *
101
     * @return Field
102
     */
103
    protected function detectField($column)
104
    {
105
        return Chain::make([
106
            new PrimaryKeyDetector(),
107
            new RankableDetector(),
108
            new DateTimeDetector(),
109
            new CastedEnumDetector(),
110
            new EnumDetector(),
111
            new BooleanDetector(),
112
            new TextareaDetector(),
113
            new PasswordDetector(),
114
            new EmailDetector(),
115
            new LinkDetector(),
116
            new PhoneDetector(),
117
            new NumberDetector(),
118
            new TextDetector(),
119
        ])($column, $this->fetchTablesColumns()[$column], $this->model);
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    protected function fetchTablesColumns()
126
    {
127
        return \admin\db\table_columns($this->model, true);
128
    }
129
}
130