EloquentCollectionQuery   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 11
eloc 34
dl 0
loc 103
rs 10
c 2
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 3 1
A name() 0 7 2
A args() 0 17 3
A resolve() 0 32 5
1
<?php
2
3
namespace Bakery\Queries;
4
5
use Bakery\Utils\Utils;
6
use Bakery\Support\Arguments;
7
use Bakery\Traits\OrdersQueries;
8
use Bakery\Traits\FiltersQueries;
9
use Bakery\Traits\SearchesQueries;
10
use Bakery\Types\Definitions\RootType;
11
use GraphQL\Type\Definition\ResolveInfo;
12
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
13
use Bakery\Exceptions\PaginationMaxCountExceededException;
14
15
class EloquentCollectionQuery extends EloquentQuery
16
{
17
    use FiltersQueries;
18
    use OrdersQueries;
19
    use SearchesQueries;
20
21
    /**
22
     * The fields to be fulltext searched on.
23
     *
24
     * @var array
25
     */
26
    protected $tsFields;
27
28
    /**
29
     * Get the name of the CollectionQuery.
30
     *
31
     * @return string
32
     */
33
    public function name(): string
34
    {
35
        if (isset($this->name)) {
36
            return $this->name;
37
        }
38
39
        return Utils::plural($this->modelSchema->getTypename());
40
    }
41
42
    /**
43
     * The type of the CollectionQuery.
44
     *
45
     * @return RootType
46
     */
47
    public function type(): RootType
48
    {
49
        return $this->registry->type($this->modelSchema->typename().'Collection');
50
    }
51
52
    /**
53
     * The arguments for the CollectionQuery.
54
     *
55
     * @return array
56
     */
57
    public function args(): array
58
    {
59
        $args = collect([
60
            'page' => $this->registry->int()->nullable(),
61
            'count' => $this->registry->int()->nullable(),
62
            'filter' => $this->registry->type($this->modelSchema->typename().'Filter')->nullable(),
63
        ]);
64
65
        if ($this->modelSchema->isSearchable()) {
66
            $args->put('search', $this->registry->type($this->modelSchema->typename().'RootSearch')->nullable());
67
        }
68
69
        if ($this->modelSchema->isSortable()) {
70
            $args->put('orderBy', $this->registry->type($this->modelSchema->typename().'OrderBy')->nullable());
71
        }
72
73
        return $args->toArray();
74
    }
75
76
    /**
77
     * Resolve the CollectionQuery.
78
     *
79
     * @param Arguments $args
80
     * @param mixed $root
81
     * @param mixed $context
82
     * @param \GraphQL\Type\Definition\ResolveInfo $info
83
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
84
     * @throws PaginationMaxCountExceededException
85
     */
86
    public function resolve(Arguments $args, $root, $context, ResolveInfo $info): LengthAwarePaginator
87
    {
88
        $page = $args->page ?? 1;
0 ignored issues
show
Bug introduced by
The property page does not seem to exist on Bakery\Support\Arguments.
Loading history...
89
        $count = $args->count ?? 15;
0 ignored issues
show
Bug introduced by
The property count does not seem to exist on Bakery\Support\Arguments.
Loading history...
90
91
        $maxCount = config('bakery.security.paginationMaxCount');
92
93
        if ($count > $maxCount) {
94
            throw new PaginationMaxCountExceededException($maxCount);
95
        }
96
97
        $query = $this->scopeQuery($this->modelSchema->getQuery());
98
99
        $fields = $info->getFieldSelection(config('bakery.security.eagerLoadingMaxDepth'));
100
        $this->eagerLoadRelations($query, $fields['items'], $this->modelSchema);
0 ignored issues
show
Bug introduced by
$fields['items'] of type boolean is incompatible with the type array expected by parameter $fields of Bakery\Queries\EloquentQuery::eagerLoadRelations(). ( Ignorable by Annotation )

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

100
        $this->eagerLoadRelations($query, /** @scrutinizer ignore-type */ $fields['items'], $this->modelSchema);
Loading history...
101
102
        // Select all columns from the table.
103
        $query->addSelect($this->model->getTable().'.*');
104
105
        if ($args->filter) {
0 ignored issues
show
Bug introduced by
The property filter does not seem to exist on Bakery\Support\Arguments.
Loading history...
106
            $query = $this->applyFilters($query, $args->filter);
107
        }
108
109
        if ($args->search) {
0 ignored issues
show
Bug introduced by
The property search does not seem to exist on Bakery\Support\Arguments.
Loading history...
110
            $query = $this->applySearch($query, $args->search);
111
        }
112
113
        if ($args->orderBy) {
0 ignored issues
show
Bug introduced by
The property orderBy does not seem to exist on Bakery\Support\Arguments.
Loading history...
114
            $query = $this->applyOrderBy($query, $args->orderBy);
115
        }
116
117
        return $query->paginate($count, ['*'], 'page', $page);
118
    }
119
}
120