Passed
Push — main ( 36e409...b1e3f6 )
by Yaroslav
02:51
created

CardRequest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 97.44%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
eloc 36
c 2
b 0
f 0
dl 0
loc 89
ccs 38
cts 39
cp 0.9744
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B prepareQuery() 0 40 11
A cardResource() 0 3 1
A authorize() 0 3 1
A card() 0 14 4
A rules() 0 3 1
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>|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Resource>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Resource>|null.
Loading history...
55
     */
56 4
    public function cardResource(): ?string
57
    {
58 4
        return $this->card()?->resource;
59
    }
60
61 3
    public function prepareQuery(): Builder
62
    {
63 3
        $resource = $this->cardResource();
64 3
        if(!$resource) {
65
            throw new \Exception('Resource not found');
66
        }
67
68
        /** @var Builder $query */
69 3
        $query = $resource::newModel()->query();
70
71 3
        $card = $this->card();
72
73 3
        if ($card->relationship) {
74 2
            $query = match ($card->aggregate) {
75 2
                'count' => $query->withCount($card->relationship),
76 2
                'sum'   => $query->withSum($card->relationship, $card->aggregateColumn ?: $card->valueColumn),
77 2
                'avg'   => $query->withAvg($card->relationship, $card->aggregateColumn ?: $card->valueColumn),
78 2
                'min'   => $query->withMin($card->relationship, $card->aggregateColumn ?: $card->valueColumn),
79 2
                'max'   => $query->withMax($card->relationship, $card->aggregateColumn ?: $card->valueColumn),
80 2
                default => $query
81 2
            };
82
        }
83
84
85 3
        if ($card->orderColumn) {
86 2
            $query->orderBy($card->orderColumn, $card->orderDirection);
87
        }
88
89
        if (
90 3
            ($limit = $card->limit) &&
91 3
            $limit > 0
92
        ) {
93 3
            $query->take($limit);
94
        }
95
96 3
        if (is_callable($card->queryCallback)) {
97 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

97
            call_user_func(/** @scrutinizer ignore-type */ $card->queryCallback, $query);
Loading history...
98
        }
99
100 3
        return $query;
101
    }
102
}
103