CardRequest::cardResource()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace NovaListCard\Http\Requests;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Collection;
7
use Laravel\Nova\Http\Requests\NovaRequest;
8
use Laravel\Nova\Nova;
9
use Laravel\Nova\Resource;
10
use NovaListCard\ListCard;
11
12
class CardRequest extends NovaRequest
13
{
14
    protected ?ListCard $_card = null;
15
16
    /**
17
     * Determine if the user is authorized to make this request.
18
     *
19
     * @return bool
20
     */
21 4
    public function authorize()
22
    {
23 4
        return true;
24
    }
25
26
    /**
27
     * Get the validation rules that apply to the request.
28
     *
29
     * @return array
30
     */
31 4
    public function rules()
32
    {
33 4
        return [];
34
    }
35
36
37 4
    public function card(): ?ListCard
38
    {
39 4
        if (!$this->_card) {
40 4
            $allCards = new Collection();
41 4
            collect(Nova::$dashboards)
0 ignored issues
show
Bug introduced by
Laravel\Nova\Nova::dashboards of type array<integer,Laravel\Nova\Dashboard> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

41
            collect(/** @scrutinizer ignore-type */ Nova::$dashboards)
Loading history...
42 4
                ->filter(fn ($dashboard) => method_exists($dashboard, 'cards') && is_array($dashboard->cards()))
43 4
                ->each(fn ($dashboard) => $allCards->push(...$dashboard->cards()));
44
45 4
            $this->_card = $allCards
46 4
                ->filter(fn ($card) => (method_exists($card, 'uriKey') && $card->uriKey() == $this->route('key')))
47 4
                ->first();
48
        }
49
50 4
        return $this->_card;
51
    }
52
53
    /**
54
     * @return class-string<Resource>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Resource> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Resource>.
Loading history...
55
     */
56 4
    public function cardResource(): string
57
    {
58 4
        $resource = $this->card()?->resource;
59 4
        if (!$resource) {
60 1
            throw new \Exception('Resource not found');
61
        }
62
63 3
        return $resource;
64
    }
65
66
    /**
67
     * @return Builder
68
     * @throws \Exception
69
     */
70 3
    public function prepareQuery(): Builder
71
    {
72 3
        $resource = $this->cardResource();
73
74
        /** @var Builder $query */
75 3
        $query = $resource::newModel()->query();
76
77 3
        $card = $this->card();
78
79 3
        if ($card->relationship) {
80 2
            $query = match ($card->aggregate) {
81 2
                'count' => $query->withCount($card->relationship),
82 2
                'sum'   => $query->withSum($card->relationship, $card->aggregateColumn ?: $card->valueColumn),
83 2
                'avg'   => $query->withAvg($card->relationship, $card->aggregateColumn ?: $card->valueColumn),
84 2
                'min'   => $query->withMin($card->relationship, $card->aggregateColumn ?: $card->valueColumn),
85 2
                'max'   => $query->withMax($card->relationship, $card->aggregateColumn ?: $card->valueColumn),
86 2
                default => $query
87 2
            };
88
        }
89
90
91 3
        if ($card->orderColumn) {
92 2
            $query->orderBy($card->orderColumn, $card->orderDirection);
93
        }
94
95
        if (
96 3
            ($limit = $card->limit) &&
97 3
            $limit > 0
98
        ) {
99 3
            $query->take($limit);
100
        }
101
102 3
        if (is_callable($card->queryCallback)) {
103 1
            call_user_func($card->queryCallback, $query);
0 ignored issues
show
Bug introduced by
It seems like $card->queryCallback can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

103
            call_user_func(/** @scrutinizer ignore-type */ $card->queryCallback, $query);
Loading history...
104
        }
105
106 3
        return $query;
107
    }
108
}
109