Completed
Push — master ( d58822...10e381 )
by Jonas
15:56
created

HasEagerLimit::limit()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 1
dl 0
loc 23
ccs 10
cts 10
cp 1
crap 5
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep\Eloquent\Relations\Traits;
4
5
use Illuminate\Database\Query\Grammars\MySqlGrammar;
6
use RuntimeException;
7
8
trait HasEagerLimit
9
{
10
    /**
11
     * Alias to set the "limit" value of the query.
12
     *
13
     * @param int $value
14
     * @return $this
15
     */
16 9
    public function take($value)
17
    {
18 9
        return $this->limit($value);
19
    }
20
21
    /**
22
     * Set the "limit" value of the query.
23
     *
24
     * @param int $value
25
     * @return $this
26
     */
27 11
    public function limit($value)
28
    {
29 11
        if ($this->farParent->exists) {
30 9
            $this->query->limit($value);
31
        } else {
32 2
            if (!class_exists('Staudenmeir\EloquentEagerLimit\Builder')) {
33
                $message = 'Please install staudenmeir/eloquent-eager-limit and add the HasEagerLimit trait as shown in the README.'; // @codeCoverageIgnore
34
35
                throw new RuntimeException($message); // @codeCoverageIgnore
36
            }
37
38 2
            $column = $this->getQualifiedFirstKeyName();
0 ignored issues
show
Bug introduced by
It seems like getQualifiedFirstKeyName() 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

38
            /** @scrutinizer ignore-call */ 
39
            $column = $this->getQualifiedFirstKeyName();
Loading history...
39
40 2
            $grammar = $this->query->getQuery()->getGrammar();
41
42 2
            if ($grammar instanceof MySqlGrammar && $grammar->useLegacyGroupLimit($this->query->getQuery())) {
43 2
                $column = 'laravel_through_key';
44
            }
45
46 2
            $this->query->groupLimit($value, $column);
47
        }
48
49 11
        return $this;
50
    }
51
}
52