Test Setup Failed
Push — master ( 81f5ad...e23419 )
by Terzi
04:23
created

HasMany::onEdit()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 2
nop 0
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Illuminate\Database\Query\Builder;
8
use Illuminate\Support\Facades\View;
9
use Terranet\Administrator\Contracts\Module;
10
use Terranet\Administrator\Field\Traits\HandlesRelation;
11
use Terranet\Administrator\Field\Traits\WorksWithModules;
12
use Terranet\Administrator\Modules\Faked;
13
use Terranet\Administrator\Scaffolding;
14
use Terranet\Administrator\Services\CrudActions;
15
use Terranet\Administrator\Traits\Module\HasColumns;
16
17
class HasMany extends Generic
18
{
19
    use WorksWithModules, HandlesRelation;
20
21
    /** @var string */
22
    protected $icon = 'list-ul';
23
24
    /** @var Builder */
25
    protected $query;
26
27
    /**
28
     * @param \Closure $query
29
     *
30
     * @return $this
31
     */
32
    public function withQuery(\Closure $query)
33
    {
34
        $this->query = $query;
0 ignored issues
show
Documentation Bug introduced by
It seems like $query of type Closure is incompatible with the declared type Illuminate\Database\Query\Builder of property $query.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
36
        return $this;
37
    }
38
39
    /**
40
     * @param string $icon
41
     *
42
     * @return self
43
     */
44
    public function setIcon(string $icon): self
45
    {
46
        $this->icon = $icon;
47
48
        return $this;
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    protected function onIndex(): array
55
    {
56
        $relation = $this->relation();
57
        $module = $this->firstWithModel($related = $relation->getRelated());
0 ignored issues
show
Unused Code introduced by
The assignment to $module is dead and can be removed.
Loading history...
58
59
        // apply a query
60
        if ($this->query) {
61
            $relation = call_user_func_array($this->query, [$relation]);
0 ignored issues
show
Bug introduced by
$this->query of type Illuminate\Database\Query\Builder is incompatible with the type callable expected by parameter $function of call_user_func_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            $relation = call_user_func_array(/** @scrutinizer ignore-type */ $this->query, [$relation]);
Loading history...
62
        }
63
64
        if ($module = $this->firstWithModel($related)) {
65
            $url = route('scaffold.view', [
66
                'module' => $module->url(),
67
                $related->getKeyName() => $related->getKey(),
68
                $relation->getForeignKeyName() => $this->model->getKey(),
69
            ]);
70
        }
71
72
        return [
73
            'icon' => $this->icon,
74
            'module' => $module,
75
            'count' => $relation->count(),
76
            'url' => $url ?? null,
77
        ];
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    protected function onView(): array
84
    {
85
        $relation = $this->relation();
86
        $related = $relation->getRelated();
87
88
        if (!$module = $this->relationModule()) {
89
            // Build a runtime module
90
            $module = Faked::make($related);
91
        }
92
        $columns = $module->columns()->each->disableSorting();
93
        $actions = $module->actionsManager();
94
95
        return [
96
            'module' => $module ?? null,
97
            'columns' => $columns ?? null,
98
            'actions' => $actions ?? null,
99
            'relation' => $relation ?? null,
100
            'items' => $relation ? $relation->getResults() : null,
101
        ];
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function onEdit(): array
108
    {
109
        $relation = $this->relation();
110
111
        if (static::MODE_CHECKBOXES === $this->editMode && $this->completeList) {
0 ignored issues
show
Bug introduced by
The constant Terranet\Administrator\F...asMany::MODE_CHECKBOXES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
112
            $values = $relation->getRelated()->all();
113
        } else {
114
            $values = $this->value();
115
        }
116
117
        return [
118
            'relation' => $relation,
119
            'searchable' => get_class($relation->getRelated()),
120
            'values' => $values,
121
            'completeList' => $this->completeList,
122
            'titleField' => $this->titleField,
123
            'editMode' => $this->editMode,
124
        ];
125
    }
126
}
127