Test Failed
Push — master ( c7f62f...19668e )
by Terzi
05:55
created

HasOne::withColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
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...
6
use Terranet\Administrator\Collection\Mutable;
7
use Terranet\Administrator\Traits\Module\HasColumns;
8
9
class HasOne extends BelongsTo
10
{
11
    use HasColumns;
0 ignored issues
show
Bug introduced by
The trait Terranet\Administrator\Traits\Module\HasColumns requires the property $includeDateColumns which is not provided by Terranet\Administrator\Field\HasOne.
Loading history...
12
13
    /** @var null|array */
14
    protected $only;
15
16
    /** @var null|array */
17
    protected $except;
18
19
    /** @var null|\Closure */
20
    protected $withColumnsCallback;
21
22
    /** @var null|Mutable */
23
    protected $columns;
24
25
    /**
26
     * Fetch related columns.
27
     *
28
     * @return null|Mutable
29
     */
30
    protected function getColumns(): ?Mutable
31
    {
32
        if (null === $this->columns) {
33
            $relation = $this->model->{$this->id()}();
34
35
            $this->columns = $this->applyColumnsCallback(
36
                $this->relatedColumns($related = $relation->getRelated())
37
                    ->each->setModel($this->model->{$this->id()} ?: $related)
38
            );
39
        }
40
41
        return $this->columns;
42
    }
43
44
    /**
45
     * @param  array  $only
46
     *
47
     * @return self
48
     */
49
    public function only(array $only): self
50
    {
51
        $this->only = $only;
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param  array  $except
58
     *
59
     * @return self
60
     */
61
    public function except(array $except): self
62
    {
63
        $this->except = $except;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    protected function onEdit(): array
72
    {
73
        $columns = $this->getColumns()->each(function ($field) {
74
            $field->setId(
75
                "{$this->id()}.{$field->id()}"
76
            );
77
        });
78
79
        dd('edit',$columns);
0 ignored issues
show
Bug introduced by
The function dd 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

79
        /** @scrutinizer ignore-call */ 
80
        dd('edit',$columns);
Loading history...
80
        return [
81
            'columns' => $columns,
82
        ];
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    protected function onIndex(): array
89
    {
90
        $columns = $this->getColumns()->filter(function ($field) {
91
            return !$field instanceof Textarea;
92
        });
93
94
        return [
95
            'columns' => $columns,
96
            'related' => $this->model->{$this->id()},
97
        ];
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    protected function onView(): array
104
    {
105
        return [
106
            'columns' => $this->getColumns(),
107
            'related' => $this->model->{$this->id()},
108
        ];
109
    }
110
111
    /**
112
     * @param $related
113
     *
114
     * @return \Terranet\Administrator\Collection\Mutable
115
     */
116
    protected function relatedColumns($related): Mutable
117
    {
118
        return $this->collectColumns($related)
119
            ->except(array_merge([$related->getKeyName()], $this->except ?? []))
120
            ->only($this->only);
121
    }
122
123
    /**
124
     * @param  \Closure  $callback
125
     * @return $this
126
     */
127
    public function withColumns(\Closure $callback): self
128
    {
129
        $this->withColumnsCallback = $callback;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Apply callback function to all columns, including those added during callback execution.
136
     *
137
     * @param  Mutable  $collection
138
     * @return mixed|Mutable
139
     */
140
    protected function applyColumnsCallback(Mutable $collection)
141
    {
142
        if ($this->withColumnsCallback) {
143
            $collection = call_user_func($this->withColumnsCallback, $collection);
144
        }
145
146
        $this->assignModel(
147
            $collection,
148
            $this->model->{$this->id()} ?: $this->model->{$this->id()}()->getRelated()
149
        );
150
151
        return $collection;
152
    }
153
154
    /**
155
     * @param  Mutable  $collection
156
     * @param $model
157
     * @return mixed
158
     */
159
    protected function assignModel(Mutable $collection, $model)
160
    {
161
        return $collection->each->setModel($model);
162
    }
163
}
164