ExecutesQueries   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 30
dl 0
loc 117
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getResults() 0 7 3
A chunk() 0 6 1
A simplePaginate() 0 11 1
A get() 0 11 2
A paginate() 0 11 1
A cursorPaginate() 0 11 1
1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep\Eloquent\Relations\Traits;
4
5
use Closure;
6
use Illuminate\Contracts\Pagination\Paginator;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Pagination\CursorPaginator;
9
10
trait ExecutesQueries
11
{
12
    /**
13
     * Get the results of the relationship.
14
     *
15
     * @return mixed
16
     */
17
    public function getResults()
18
    {
19
        if ($this->firstKey instanceof Closure || $this->localKey instanceof Closure) {
20
            return $this->get();
21
        }
22
23
        return parent::getResults();
24
    }
25
26
    /**
27
     * Execute the query as a "select" statement.
28
     *
29
     * @param array $columns
30
     * @return \Illuminate\Database\Eloquent\Collection
31
     */
32
    public function get($columns = ['*'])
33
    {
34
        $models = parent::get($columns);
35
36
        $this->hydrateIntermediateRelations($models->all());
0 ignored issues
show
Bug introduced by
It seems like hydrateIntermediateRelations() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        $this->/** @scrutinizer ignore-call */ 
37
               hydrateIntermediateRelations($models->all());
Loading history...
37
38
        foreach ($this->postGetCallbacks as $postGetCallback) {
39
            $postGetCallback($models);
40
        }
41
42
        return $models;
43
    }
44
45
    /**
46
     * Get a paginator for the "select" statement.
47
     *
48
     * @param int $perPage
49
     * @param array $columns
50
     * @param string $pageName
51
     * @param int $page
52
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
53
     */
54
    public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
55
    {
56
        $columns = array_filter(
57
            $this->shouldSelect($columns),
0 ignored issues
show
Bug introduced by
It seems like shouldSelect() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

57
            $this->/** @scrutinizer ignore-call */ 
58
                   shouldSelect($columns),
Loading history...
58
            fn ($column) => !str_contains($column, 'laravel_through_key')
59
        );
60
61
        $this->query->addSelect($columns);
62
63
        return tap($this->query->paginate($perPage, $columns, $pageName, $page), function (Paginator $paginator) {
64
            $this->hydrateIntermediateRelations($paginator->items());
65
        });
66
    }
67
68
    /**
69
     * Paginate the given query into a simple paginator.
70
     *
71
     * @param int $perPage
72
     * @param array $columns
73
     * @param string $pageName
74
     * @param int|null $page
75
     * @return \Illuminate\Contracts\Pagination\Paginator
76
     */
77
    public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
78
    {
79
        $columns = array_filter(
80
            $this->shouldSelect($columns),
81
            fn ($column) => !str_contains($column, 'laravel_through_key')
82
        );
83
84
        $this->query->addSelect($columns);
85
86
        return tap($this->query->simplePaginate($perPage, $columns, $pageName, $page), function (Paginator $paginator) {
87
            $this->hydrateIntermediateRelations($paginator->items());
88
        });
89
    }
90
91
    /**
92
     * Paginate the given query into a cursor paginator.
93
     *
94
     * @param  int|null  $perPage
95
     * @param  array  $columns
96
     * @param  string  $cursorName
97
     * @param  string|null  $cursor
98
     * @return \Illuminate\Contracts\Pagination\CursorPaginator
99
     */
100
    public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null)
101
    {
102
        $columns = array_filter(
103
            $this->shouldSelect($columns),
104
            fn ($column) => !str_contains($column, 'laravel_through_key')
105
        );
106
107
        $this->query->addSelect($columns);
108
109
        return tap($this->query->cursorPaginate($perPage, $columns, $cursorName, $cursor), function (CursorPaginator $paginator) {
110
            $this->hydrateIntermediateRelations($paginator->items());
111
        });
112
    }
113
114
    /**
115
     * Chunk the results of the query.
116
     *
117
     * @param int $count
118
     * @param callable $callback
119
     * @return bool
120
     */
121
    public function chunk($count, callable $callback)
122
    {
123
        return $this->prepareQueryBuilder()->chunk($count, function (Collection $results) use ($callback) {
0 ignored issues
show
Bug introduced by
It seems like prepareQueryBuilder() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

123
        return $this->/** @scrutinizer ignore-call */ prepareQueryBuilder()->chunk($count, function (Collection $results) use ($callback) {
Loading history...
124
            $this->hydrateIntermediateRelations($results->all());
125
126
            return $callback($results);
127
        });
128
    }
129
}
130