Completed
Push — master ( 1fff09...65b094 )
by Terzi
05:17
created

BelongsTo::onEdit()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 28
rs 8.6666
c 0
b 0
f 0
cc 7
nc 12
nop 0
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Illuminate\Support\Facades\URL;
6
use Illuminate\Support\Facades\View;
7
use Terranet\Administrator\Field\Traits\WorksWithModules;
8
use Terranet\Administrator\Scaffolding;
9
10
class BelongsTo extends Generic
11
{
12
    use WorksWithModules;
13
14
    /** @var string */
15
    protected $column = 'name';
16
17
    /** @var bool */
18
    protected $searchable = true;
19
20
    /**
21
     * @param string $column
22
     *
23
     * @return self
24
     */
25
    public function showAs(string $column): self
26
    {
27
        $this->column = $column;
28
29
        return $this;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getColumn()
36
    {
37
        return $this->column;
38
    }
39
40
    /**
41
     * @return $this
42
     */
43
    public function searchable()
44
    {
45
        $this->searchable = true;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @return null|\Illuminate\Contracts\View\View
52
     */
53
    protected function onIndex()
54
    {
55
        $method = $this->id;
56
57
        if ($hasRelation = method_exists($this->model, $method)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $hasRelation is dead and can be removed.
Loading history...
58
            $relation = $this->model->{$method};
59
            if ($relation) {
60
                $module = $this->firstWithModel($relation);
61
                $title = $relation->{$this->getColumn()};
62
            }
63
        }
64
65
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('title' => ...le' => $module ?? null) returns the type array<string,null|mixed> which is incompatible with the documented return type Illuminate\Contracts\View\View|null.
Loading history...
66
            'title' => $title ?? null,
67
            'relation' => $relation ?? null,
68
            'module' => $module ?? null,
69
        ];
70
    }
71
72
    /**
73
     * @return null|\Illuminate\Contracts\View\View
74
     */
75
    protected function onView()
76
    {
77
        return $this->onIndex();
78
    }
79
80
    /**
81
     * @return \Illuminate\Contracts\View\View
82
     */
83
    protected function onEdit()
84
    {
85
        $method = $this->relation ?: $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The property relation does not exist on Terranet\Administrator\Field\BelongsTo. Did you maybe forget to declare it?
Loading history...
86
        $hasRelation = method_exists($this->model, $method);
87
88
        if ($hasRelation && $relation = $this->model->{$method}()) {
89
            $related = $relation->getRelated();
90
91
            if ($this->searchable) {
92
                $searchable = $this->firstWithModel($related);
93
                $attributes = [
94
                    'data-type' => 'livesearch',
95
                    'data-url' => route('scaffold.search', ['searchable' => get_class($related)]),
96
                    'placeholder' => 'Search...',
97
                ];
98
                $options = [$this->value()->getKey() => $this->value()->getAttribute($searchable::$title)];
99
            } else {
100
                $options = $related::pluck($this->getColumn(), $related->getKeyName())->toArray();
101
            }
102
        }
103
104
        return [
105
            'name' => $relation ? $relation->getForeignKey() : $this->id(),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $relation does not seem to be defined for all execution paths leading up to this point.
Loading history...
106
            'attributes' => $attributes ?? [],
107
            'options' => $options ?? [],
108
            'related' => $related ? get_class($related) : null,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $related does not seem to be defined for all execution paths leading up to this point.
Loading history...
109
            'searchable' => $searchable ?? null,
110
            'value' => $this->value()->getKey(),
111
        ];
112
    }
113
}
114