Passed
Push — master ( 0d1e3a...a8104a )
by Jonas
11:45
created

CompilesGroupLimit::compileSelect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
namespace Staudenmeir\EloquentEagerLimit\Grammars\Traits;
4
5
use Illuminate\Database\Query\Builder;
6
7
trait CompilesGroupLimit
8
{
9
    /**
10
     * Compile a select query into SQL.
11
     *
12
     * @param \Illuminate\Database\Query\Builder $query
13
     * @return string
14
     */
15 148
    public function compileSelect(Builder $query)
16
    {
17 148
        if (isset($query->groupLimit)) {
18 116
            if (is_null($query->columns)) {
0 ignored issues
show
introduced by
The condition is_null($query->columns) is always false.
Loading history...
19 20
                $query->columns = ['*'];
20
            }
21
22 116
            return $this->compileGroupLimit($query);
23
        }
24
25 132
        return parent::compileSelect($query);
26
    }
27
28
    /**
29
     * Compile a group limit clause.
30
     *
31
     * @param \Illuminate\Database\Query\Builder $query
32
     * @return string
33
     */
34 92
    protected function compileGroupLimit(Builder $query)
35
    {
36 92
        $selectBindings = array_merge($query->getRawBindings()['select'], $query->getRawBindings()['order']);
37
38 92
        $query->setBindings($selectBindings, 'select');
39 92
        $query->setBindings([], 'order');
40
41 92
        $limit = (int) $query->groupLimit['value'];
0 ignored issues
show
Bug introduced by
The property groupLimit does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
42
43 92
        $offset = $query->offset;
44
45 92
        if (isset($offset)) {
46 44
            $limit += (int) $offset;
47
48 44
            $query->offset = null;
49
        }
50
51 92
        $components = $this->compileComponents($query);
0 ignored issues
show
Bug introduced by
It seems like compileComponents() 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

51
        /** @scrutinizer ignore-call */ 
52
        $components = $this->compileComponents($query);
Loading history...
52
53 92
        $components['columns'] .= $this->compileRowNumber($query->groupLimit['column'], $components['orders'] ?? '');
54
55 92
        unset($components['orders']);
56
57 92
        $sql = $this->concatenate($components);
0 ignored issues
show
Bug introduced by
It seems like concatenate() 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
        /** @scrutinizer ignore-call */ 
58
        $sql = $this->concatenate($components);
Loading history...
58
59 92
        $sql = 'select * from ('.$sql.') as laravel_table where laravel_row <= '.$limit;
60
61 92
        if (isset($offset)) {
62 44
            $sql .= ' and laravel_row > '.(int) $offset;
63
        }
64
65 92
        return $sql.' order by laravel_row';
66
    }
67
68
    /**
69
     * Compile a row number clause.
70
     *
71
     * @param string $partition
72
     * @param string $orders
73
     * @return string
74
     */
75 92
    protected function compileRowNumber($partition, $orders)
76
    {
77 92
        $partition = 'partition by '.$this->wrap($partition);
0 ignored issues
show
Bug introduced by
It seems like wrap() 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

77
        $partition = 'partition by '.$this->/** @scrutinizer ignore-call */ wrap($partition);
Loading history...
78
79 92
        $over = trim($partition.' '.$orders);
80
81 92
        return ', row_number() over ('.$over.') as laravel_row';
82
    }
83
}
84