Passed
Push — master ( c9b334...7badbc )
by Ryosuke
12:43 queued 02:43
created

Builder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 20
c 1
b 0
f 0
dl 0
loc 58
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 27 6
A groupLimit() 0 7 2
1
<?php
2
3
namespace Mpyw\ComposhipsEagerLimit\Database\Query;
4
5
use Awobaz\Compoships\Database\Query\Builder as ComposhipsBuilder;
6
7
/**
8
 * Class Builder
9
 *
10
 * @mixin \Staudenmeir\EloquentEagerLimit\Builder
11
 */
12
class Builder extends ComposhipsBuilder
13
{
14
    /**
15
     * The maximum number of records to return per group.
16
     *
17
     * @var array
18
     */
19
    public $groupLimit;
20
21
    /**
22
     * Add a "group limit" clause to the query.
23
     *
24
     * @param  int    $value
25
     * @param  string $column
26
     * @return $this
27
     */
28 32
    public function groupLimit($value, $column)
29
    {
30 32
        if ($value >= 0) {
31 32
            $this->groupLimit = compact('value', 'column');
32
        }
33
34 32
        return $this;
35
    }
36
37
    /**
38
     * Execute the query as a "select" statement.
39
     *
40
     * @param  array                          $columns
41
     * @return \Illuminate\Support\Collection
42
     */
43 84
    public function get($columns = ['*'])
44
    {
45 84
        $items = parent::get($columns);
46
47 84
        if (!$this->groupLimit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->groupLimit of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48 84
            return $items;
49
        }
50
51 32
        $keys = ['laravel_row'];
52
53 32
        if (is_array($this->groupLimit['column'])) {
54 20
            foreach ($this->groupLimit['column'] as $i => $column) {
55 20
                $keys[] = "@laravel_partition_$i := " . $this->grammar->wrap(last(explode('.', $column)));
56 20
                $keys[] = "@laravel_partition_$i := " . $this->grammar->wrap('pivot_' . last(explode('.', $column)));
57
            }
58
        } else {
59 32
            $keys[] = '@laravel_partition := ' . $this->grammar->wrap(last(explode('.', $this->groupLimit['column'])));
60 32
            $keys[] = '@laravel_partition := ' . $this->grammar->wrap('pivot_' . last(explode('.', $this->groupLimit['column'])));
61
        }
62
63 32
        foreach ($items as $item) {
64 28
            foreach ($keys as $key) {
65 28
                unset($item->$key);
66
            }
67
        }
68
69 32
        return $items;
70
    }
71
}
72