Passed
Push — master ( 98eee9...aa1f26 )
by Jonas
09:56
created

addExpressionWhereConstraints()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\HasMany;
8
9
class HasManyOfDescendants extends HasMany
10
{
11
    use IsOfDescendantsRelation;
0 ignored issues
show
Bug introduced by
The trait Staudenmeir\LaravelAdjac...IsOfDescendantsRelation requires the property $from which is not provided by Staudenmeir\LaravelAdjac...ns\HasManyOfDescendants.
Loading history...
12
13
    /**
14
     * Create a new has many of descendants relationship instance.
15
     *
16
     * @param \Illuminate\Database\Eloquent\Builder $query
17
     * @param \Illuminate\Database\Eloquent\Model $parent
18
     * @param string $foreignKey
19
     * @param string $localKey
20
     * @param bool $andSelf
21
     * @return void
22
     */
23 76
    public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
24
    {
25 76
        $this->andSelf = $andSelf;
26
27 76
        parent::__construct($query, $parent, $foreignKey, $localKey);
28 76
    }
29
30
    /**
31
     * Set the where clause on the recursive expression query.
32
     *
33
     * @param \Illuminate\Database\Eloquent\Builder $query
34
     * @return void
35
     */
36 48
    public function addExpressionWhereConstraints(Builder $query)
37
    {
38 48
        $column = $this->andSelf ? $this->parent->getLocalKeyName() : $this->parent->getParentKeyName();
39
40 48
        $query->where(
41 48
            $column,
42 48
            '=',
43 48
            $this->parent->{$this->parent->getLocalKeyName()}
44 48
        )->whereNotNull($column);
45 48
    }
46
47
    /**
48
     * Get the local key name for an eager load of the relation.
49
     *
50
     * @return string
51
     */
52 16
    public function getEagerLoadingLocalKeyName()
53
    {
54 16
        return $this->getLocalKeyName();
55
    }
56
57
    /**
58
     * Get the foreign key name for an eager load of the relation.
59
     *
60
     * @return string
61
     */
62 16
    public function getEagerLoadingForeignKeyName()
63
    {
64 16
        return $this->getForeignKeyName();
65
    }
66
67
    /**
68
     * Get the local key name for the recursion expression.
69
     *
70
     * @return string
71
     */
72 68
    public function getExpressionLocalKeyName()
73
    {
74 68
        return $this->getLocalKeyName();
75
    }
76
77
    /**
78
     * Get the foreign key name for the recursion expression.
79
     *
80
     * @return string
81
     */
82 68
    public function getExpressionForeignKeyName()
83
    {
84 68
        return $this->foreignKey;
85
    }
86
}
87