Completed
Push — master ( 5bb2ae...adea1c )
by Terzi
04:11
created

HasColumns   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 94
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0
eloc 28
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
B collectColumns() 0 44 9
A columns() 0 3 1
A scaffoldColumns() 0 4 1
A widgets() 0 3 1
A viewColumns() 0 3 1
1
<?php
2
3
namespace Terranet\Administrator\Traits\Module;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Terranet\Administrator\Collection\Group;
7
use Terranet\Administrator\Collection\Mutable as MutableCollection;
8
use Terranet\Administrator\Dashboard\Manager;
9
use Terranet\Administrator\Decorators\Grid as GridDecorator;
10
use Terranet\Administrator\Form\Collection\Mutable;
11
use Terranet\Translatable\Translatable;
12
13
trait HasColumns
14
{
15
    /**
16
     * Fetch scaffold columns.
17
     *
18
     * @return MutableCollection
19
     */
20
    public function columns()
21
    {
22
        return $this->scaffoldColumns();
23
    }
24
25
    /**
26
     * Define the list of attributes visible on View model page.
27
     *
28
     * @param Model $model
29
     *
30
     * @return Mutable
31
     */
32
    public function viewColumns()
33
    {
34
        return $this->scaffoldColumns();
35
    }
36
37
    /**
38
     * Scaffold columns.
39
     *
40
     * @return MutableCollection
41
     */
42
    protected function scaffoldColumns()
43
    {
44
        return $this->collectColumns(
45
            $this->model()
0 ignored issues
show
Bug introduced by
It seems like model() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

45
            $this->/** @scrutinizer ignore-call */ 
46
                   model()
Loading history...
46
        );
47
    }
48
49
    /**
50
     * @param Manager $dashboard
51
     * @return Manager
52
     */
53
    public function widgets(Manager $dashboard): Manager
54
    {
55
        return $dashboard;
56
    }
57
58
    /**
59
     * @param $model
60
     *
61
     * @return MutableCollection
62
     */
63
    protected function collectColumns(Model $model = null)
64
    {
65
        if (!$model) {
66
            return new MutableCollection([]);
67
        }
68
69
        $pk = $model->getKeyName();
70
71
        $fillable = array_merge(
72
            \is_array($pk) ? $pk : [$pk],
0 ignored issues
show
introduced by
The condition is_array($pk) is always false.
Loading history...
73
            $model->getFillable()
74
        );
75
        $hidden = $model->getHidden();
76
77
        if ($model instanceof Translatable && method_exists($model, 'getTranslatedAttributes')) {
78
            $fillable = array_merge($fillable, $model->getTranslatedAttributes());
0 ignored issues
show
Bug introduced by
It seems like $model->getTranslatedAttributes() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $array2 of array_merge() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

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

78
            $fillable = array_merge($fillable, /** @scrutinizer ignore-type */ $model->getTranslatedAttributes());
Loading history...
79
            $hidden = array_merge($hidden, $model->getTranslationModel()->getHidden());
80
        }
81
82
        $fillable = array_unique(array_diff($fillable, $hidden));
83
84
        /**
85
         * Create collection.
86
         */
87
        $elements = new MutableCollection($fillable);
88
89
        if (property_exists($this, 'includeDateColumns') && $this->includeDateColumns && \count($dates = $model->getDates())) {
90
            // allow setting specific timestamp: created_at
91
            if (\is_string($this->includeDateColumns)) {
92
                $dates = array_intersect($dates, [$this->includeDateColumns]);
93
94
                $elements = $elements->merge($dates);
95
            } else {
96
                // add timestamps group
97
                $elements->group('dates', function (Group $group) use ($dates) {
98
                    $group->merge($dates);
99
100
                    return $group;
101
                });
102
            }
103
        }
104
105
        return $elements->build(
106
            new GridDecorator($model)
107
        );
108
    }
109
}
110