HasMany::withQuery()   A
last analyzed

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 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
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\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 Illuminate\Support\Str;
7
use Terranet\Administrator\Architect;
8
use Terranet\Administrator\Field\Traits\HandlesRelation;
9
use Terranet\Administrator\Modules\Faked;
10
11
class HasMany extends Field
12
{
13
    use HandlesRelation;
14
15
    /** @var string */
16
    protected $icon = 'list-ul';
17
18
    /** @var null|\Closure */
19
    protected $query;
20
21
    /**
22
     * @param \Closure $query
23
     *
24
     * @return $this
25
     */
26
    public function withQuery(\Closure $query)
27
    {
28
        $this->query = $query;
29
30
        return $this;
31
    }
32
33
    /**
34
     * @param string $icon
35
     *
36
     * @return self
37
     */
38
    public function setIcon(string $icon): self
39
    {
40
        $this->icon = $icon;
41
42
        return $this;
43
    }
44
45
    /**
46
     * @param \Illuminate\Database\Eloquent\Builder $query
47
     * @param Model $model
48
     * @param string $direction
49
     *
50
     * @return \Illuminate\Database\Eloquent\Builder
51
     */
52
    public function sortBy(
53
        \Illuminate\Database\Eloquent\Builder $query,
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...
54
        Model $model,
55
        string $direction
56
    ): \Illuminate\Database\Eloquent\Builder {
57
        return $query->withCount($this->id())->orderBy("{$this->id()}_count", $direction);
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    protected function onIndex(): array
64
    {
65
        $relation = $this->relation();
66
        $related = $relation->getRelated();
67
68
        // apply a query
69
        if ($this->query instanceof \Closure) {
70
            $relation = \call_user_func_array($this->query, [$relation]);
71
        }
72
73
        if ($module = Architect::resourceByEntity($related)) {
74
            $url = route('scaffold.index', [
0 ignored issues
show
Bug introduced by
The function route 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

74
            $url = /** @scrutinizer ignore-call */ route('scaffold.index', [
Loading history...
75
                'module' => $module->url(),
76
                $related->getKeyName() => $related->getKey(),
77
                'viaResource' => is_a($this, BelongsToMany::class)
78
                    ? app('scaffold.module')->url()
0 ignored issues
show
Bug introduced by
The function app 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

78
                    ? /** @scrutinizer ignore-call */ app('scaffold.module')->url()
Loading history...
79
                    : Str::singular(app('scaffold.module')->url()),
80
                'viaResourceId' => $this->model->getKey(),
81
            ]);
82
        }
83
84
        return [
85
            'icon' => $this->icon,
86
            'module' => $module,
87
            'count' => $relation->count(),
88
            'url' => $url ?? null,
89
        ];
90
    }
91
92
    /**
93
     * @return array
94
     * @throws \Terranet\Administrator\Exception
95
     *
96
     */
97
    protected function onView(): array
98
    {
99
        $relation = $this->relation();
100
        $related = $relation->getRelated();
101
102
        // apply a query
103
        if ($this->query instanceof \Closure) {
104
            $relation = \call_user_func_array($this->query, [$relation]);
105
        }
106
107
        if (!$module = $this->relationModule()) {
108
            // Build a runtime module
109
            $module = Faked::make($related);
110
        }
111
        $columns = $module->columns()->each->disableSorting();
112
        $actions = $module->actions();
113
114
        return [
115
            'module' => $module ?? null,
116
            'columns' => $columns ?? null,
117
            'actions' => $actions ?? null,
118
            'relation' => $relation ?? null,
119
            'items' => $relation ? $relation->getResults() : null,
120
        ];
121
    }
122
}
123