Completed
Push — master ( 462e8a...a30981 )
by Terzi
03:46
created

HasColumns::cards()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
eloc 1
nc 1
nop 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
     * @param Manager $widgets
39
     *
40
     * @return Manager
41
     */
42
    public function widgets(Manager $widgets): Manager
43
    {
44
        return $widgets;
45
    }
46
47
    /**
48
     * @param Manager $cards
49
     *
50
     * @return Manager
51
     */
52
    public function cards(Manager $cards): Manager
53
    {
54
        return $cards;
55
    }
56
57
    /**
58
     * Scaffold columns.
59
     *
60
     * @return MutableCollection
61
     */
62
    protected function scaffoldColumns()
63
    {
64
        return $this->collectColumns(
65
            $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

65
            $this->/** @scrutinizer ignore-call */ 
66
                   model()
Loading history...
66
        );
67
    }
68
69
    /**
70
     * @param $model
71
     *
72
     * @return MutableCollection
73
     */
74
    protected function collectColumns(Model $model = null)
75
    {
76
        if (!$model) {
77
            return new MutableCollection([]);
78
        }
79
80
        $pk = $model->getKeyName();
81
82
        $fillable = array_merge(
83
            \is_array($pk) ? $pk : [$pk],
0 ignored issues
show
introduced by
The condition is_array($pk) is always false.
Loading history...
84
            $model->getFillable()
85
        );
86
        $hidden = $model->getHidden();
87
88
        if ($model instanceof Translatable && method_exists($model, 'getTranslatedAttributes')) {
89
            $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

89
            $fillable = array_merge($fillable, /** @scrutinizer ignore-type */ $model->getTranslatedAttributes());
Loading history...
90
            $hidden = array_merge($hidden, $model->getTranslationModel()->getHidden());
91
        }
92
93
        $fillable = array_unique(array_diff($fillable, $hidden));
94
95
        /**
96
         * Create collection.
97
         */
98
        $elements = new MutableCollection($fillable);
99
100
        if (property_exists($this, 'includeDateColumns') && $this->includeDateColumns && \count($dates = $model->getDates())) {
101
            // allow setting specific timestamp: created_at
102
            if (\is_string($this->includeDateColumns)) {
103
                $dates = array_intersect($dates, [$this->includeDateColumns]);
104
105
                $elements = $elements->merge($dates);
106
            } else {
107
                // add timestamps group
108
                $elements->group('dates', function (Group $group) use ($dates) {
109
                    $group->merge($dates);
110
111
                    return $group;
112
                });
113
            }
114
        }
115
116
        return $elements->build(
117
            new GridDecorator($model)
118
        );
119
    }
120
}
121