BelongsTo::onView()   A
last analyzed

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
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Illuminate\Database\Eloquent\Builder;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Builder 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 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...
7
use Illuminate\Support\Str;
8
use Terranet\Administrator\Architect;
9
use Terranet\Administrator\Field\Traits\HandlesRelation;
10
11
class BelongsTo extends Field
12
{
13
    use HandlesRelation;
14
15
    /** @var string */
16
    protected $column = 'name';
17
18
    /** @var bool */
19
    protected $searchable = true;
20
21
    /**
22
     * @param  string  $column
23
     * @return self
24
     */
25
    public function useForTitle(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
     * @param  bool  $flag
42
     * @return $this
43
     */
44
    public function searchable(bool $flag = false)
45
    {
46
        $this->searchable = (bool) $flag;
47
48
        return $this;
49
    }
50
51
    /**
52
     * @return string
53
     * @throws \Exception
54
     */
55
    public function name(): string
56
    {
57
        return $this->getForeignKey(
58
            $this->model->{$this->id}()
59
        );
60
    }
61
62
    /**
63
     * @param  Builder  $query
64
     * @param  Model  $model
65
     * @param  string  $direction
66
     * @return Builder
67
     * @throws \Exception
68
     */
69
    public function sortBy(Builder $query, Model $model, string $direction): Builder
70
    {
71
        $table = $model->getTable();
72
        $relation = $this->relation();
73
        $joinTable = $relation->getRelated()->getTable();
74
        $alias = Str::random(4);
75
76
        $ownerKey = $relation->getOwnerKey();
77
        $foreignKey = $this->getForeignKey($relation);
78
        $foreignColumn = $this->getColumn();
79
80
        return $query->leftJoin("{$joinTable} as {$alias}", "{$table}.{$foreignKey}", '=', "{$alias}.{$ownerKey}")
81
            ->orderBy("{$alias}.{$foreignColumn}", $direction);
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    protected function onIndex(): array
88
    {
89
        if ($related = $this->model->{$this->id}) {
90
            $title = $related->getAttribute($this->getColumn());
91
            $module = Architect::resourceByEntity($related);
92
        }
93
94
        return [
95
            'title' => $title ?? null,
96
            'related' => $related ?? null,
97
            'module' => $module ?? null,
98
        ];
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    protected function onView(): array
105
    {
106
        return $this->onIndex();
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    protected function onEdit(): array
113
    {
114
        if (method_exists($this->model, $this->id)) {
115
            $relation = $this->relation();
116
            $related = $this->model->{$this->id} ?: $relation->getRelated();
117
118
            $field = $this->getColumn();
119
120
            if ($this->searchable) {
121
                if ($value = $this->value()) {
122
                    $options = [
123
                        $value->getKey() => $value->getAttribute($field),
124
                    ];
125
                }
126
            } else {
127
                $options = $related::pluck($field, $related->getKeyName())->toArray();
128
            }
129
        }
130
131
        return [
132
            'options' => $options ?? [],
133
            'related' => $related ?? null,
134
            'searchIn' => isset($related) ? get_class($related) : null,
135
            'searchable' => $this->searchable,
136
            'searchBy' => $this->column,
137
        ];
138
    }
139
}
140