Passed
Push — master ( b113bd...f36913 )
by Jonas
02:07
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 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 23
ccs 10
cts 10
cp 1
crap 5
rs 9.5555
1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep;
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 5
    public function take($value)
17
    {
18 5
        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 6
    public function limit($value)
28
    {
29 6
        if ($this->farParent->exists) {
30 5
            $this->query->limit($value);
31
        } else {
32 1
            if (!class_exists('Staudenmeir\EloquentEagerLimit\Builder')) {
33
                $message = 'Please install staudenmeir/eloquent-eager-limit to limit eager loading queries.'; // @codeCoverageIgnore
34
35
                throw new RuntimeException($message); // @codeCoverageIgnore
36
            }
37
38 1
            $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 1
            $grammar = $this->query->getQuery()->getGrammar();
41
42 1
            if ($grammar instanceof MySqlGrammar && $grammar->useLegacyGroupLimit($this->query->getQuery())) {
43 1
                $column = 'laravel_through_key';
44
            }
45
46 1
            $this->query->groupLimit($value, $column);
47
        }
48
49 6
        return $this;
50
    }
51
}
52