HasOne::relatedColumns()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 2
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
    /**
23
     * Fetch related columns.
24
     *
25
     * @return null|Mutable
26
     */
27
    protected function getColumns(): ?Mutable
28
    {
29
        $relation = $this->model->{$this->id()}();
30
31
        return $this->applyColumnsCallback(
32
            $this->relatedColumns($related = $relation->getRelated())
33
                ->each->setModel($this->model->{$this->id()} ?: $related)
34
        );
35
    }
36
37
    /**
38
     * @param  array  $only
39
     * @return self
40
     */
41
    public function only(array $only): self
42
    {
43
        $this->only = $only;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param  array  $except
50
     * @return self
51
     */
52
    public function except(array $except): self
53
    {
54
        $this->except = $except;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    protected function onEdit(): array
63
    {
64
        $columns = $this->getColumns()->each(function ($field) {
65
            $field->setId(
66
                "{$this->id()}.{$field->id()}"
67
            );
68
        });
69
70
        return [
71
            'columns' => $columns,
72
        ];
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    protected function onIndex(): array
79
    {
80
        $columns = $this->getColumns()->filter(function ($field) {
81
            return !$field instanceof Textarea;
82
        });
83
84
        if ($model = $this->model->{$this->id()}) {
85
            $columns->each->setModel($model);
86
        };
87
88
        return [
89
            'columns' => $columns,
90
        ];
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    protected function onView(): array
97
    {
98
        return [
99
            'columns' => $this->getColumns(),
100
            'related' => $this->model->{$this->id()},
101
        ];
102
    }
103
104
    /**
105
     * @param $related
106
     * @return \Terranet\Administrator\Collection\Mutable
107
     */
108
    protected function relatedColumns($related): Mutable
109
    {
110
        return $this->collectColumns($related)
111
            ->except(array_merge([$related->getKeyName()], $this->except ?? []))
112
            ->only($this->only);
113
    }
114
115
    /**
116
     * @param  \Closure  $callback
117
     * @return $this
118
     */
119
    public function withColumns(\Closure $callback): self
120
    {
121
        $this->withColumnsCallback = $callback;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Apply callback function to all columns, including those added during callback execution.
128
     *
129
     * @param  Mutable  $collection
130
     * @return mixed|Mutable
131
     */
132
    protected function applyColumnsCallback(Mutable $collection)
133
    {
134
        if ($this->withColumnsCallback) {
135
            $collection = call_user_func_array($this->withColumnsCallback, [$collection, $this->model]);
136
        }
137
138
        $this->assignModel(
139
            $collection,
140
            $this->model->{$this->id()} ?: $this->model->{$this->id()}()->getRelated()
141
        );
142
143
        return $collection;
144
    }
145
146
    /**
147
     * @param  Mutable  $collection
148
     * @param $model
149
     * @return mixed
150
     */
151
    protected function assignModel(Mutable $collection, $model)
152
    {
153
        return $collection->each->setModel($model);
154
    }
155
}
156