Siblings::limit()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Eloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Staudenmeir\EloquentEagerLimit\Relations\HasLimit;
7
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings as Base;
8
9
class Siblings extends Base
10
{
11
    use HasLimit {
0 ignored issues
show
Bug introduced by
The trait Staudenmeir\EloquentEagerLimit\Relations\HasLimit requires the property $exists which is not provided by Staudenmeir\EloquentEage...uent\Relations\Siblings.
Loading history...
12
        limit as baseLimit;
13
    }
14
15
    /**
16
     * Match the eagerly loaded results to their parents.
17
     *
18
     * @param array $models
19
     * @param \Illuminate\Database\Eloquent\Collection $results
20
     * @param string $relation
21
     * @return array
22
     */
23 50
    public function match(array $models, Collection $results, $relation)
24
    {
25 50
        parent::match($models, $results, $relation);
26
27 50
        if (!$this->parent->exists && !$this->andSelf) {
28 30
            $limit = $this->query->getQuery()->groupLimit;
0 ignored issues
show
Bug introduced by
The property groupLimit does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
29
30 30
            if ($limit) {
31 10
                foreach ($models as $model) {
32 10
                    $model->setRelation(
33 10
                        $relation,
34 10
                        $model->getRelation($relation)->take($limit['value'] - 1)
35 10
                    );
36
                }
37
            }
38
        }
39
40 50
        return $models;
41
    }
42
43
    /**
44
     * Set the "limit" value of the query.
45
     *
46
     * @param int $value
47
     * @return $this
48
     */
49 30
    public function limit($value)
50
    {
51 30
        if (!$this->parent->exists && !$this->andSelf) {
52 10
            $value++;
53
        }
54
55 30
        return $this->baseLimit($value);
56
    }
57
}
58