Passed
Push — master ( 2ebb51...c237bf )
by Jonas
05:46
created

getRelationExistenceQueryForThroughSelfRelation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 2
b 0
f 0
nc 2
nop 3
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 2
rs 9.9332
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations\Relations\Postgres;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
trait HasOneOrManyThrough
8
{
9
    use IsPostgresRelation;
10
11
    /**
12
     * Set the join clause on the query.
13
     *
14
     * @param \Illuminate\Database\Eloquent\Builder|null $query
15
     * @return void
16
     */
17 12
    protected function performJoin(Builder $query = null)
18
    {
19 12
        $query = $query ?: $this->query;
20
21 12
        $farKey = $this->jsonColumn($query, $this->throughParent, $this->getQualifiedFarKeyName(), $this->secondLocalKey);
0 ignored issues
show
Bug introduced by
It seems like getQualifiedFarKeyName() 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

21
        $farKey = $this->jsonColumn($query, $this->throughParent, $this->/** @scrutinizer ignore-call */ getQualifiedFarKeyName(), $this->secondLocalKey);
Loading history...
22
23 12
        $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey);
0 ignored issues
show
Bug introduced by
It seems like getQualifiedParentKeyName() 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

23
        $query->join($this->throughParent->getTable(), $this->/** @scrutinizer ignore-call */ getQualifiedParentKeyName(), '=', $farKey);
Loading history...
24
25 12
        if ($this->throughParentSoftDeletes()) {
0 ignored issues
show
Bug introduced by
It seems like throughParentSoftDeletes() 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

25
        if ($this->/** @scrutinizer ignore-call */ throughParentSoftDeletes()) {
Loading history...
26 2
            $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
27
        }
28 12
    }
29
30
    /**
31
     * Add the constraints for a relationship query.
32
     *
33
     * @param \Illuminate\Database\Eloquent\Builder $query
34
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
35
     * @param array|mixed $columns
36
     * @return \Illuminate\Database\Eloquent\Builder
37
     */
38 6
    public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
39
    {
40 6
        if ($parentQuery->getQuery()->from === $query->getQuery()->from) {
41 2
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
42
        }
43
44 4
        if ($parentQuery->getQuery()->from === $this->throughParent->getTable()) {
45 2
            return $this->getRelationExistenceQueryForThroughSelfRelation($query, $parentQuery, $columns);
46
        }
47
48 2
        $this->performJoin($query);
49
50 2
        $firstKey = $this->jsonColumn($query, $this->farParent, $this->getQualifiedFirstKeyName(), $this->localKey);
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

50
        $firstKey = $this->jsonColumn($query, $this->farParent, $this->/** @scrutinizer ignore-call */ getQualifiedFirstKeyName(), $this->localKey);
Loading history...
51
52 2
        return $query->select($columns)->whereColumn(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->select($c...Name(), '=', $firstKey) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
53 2
            $this->getQualifiedLocalKeyName(),
0 ignored issues
show
Bug introduced by
It seems like getQualifiedLocalKeyName() 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

53
            $this->/** @scrutinizer ignore-call */ 
54
                   getQualifiedLocalKeyName(),
Loading history...
54 2
            '=',
55
            $firstKey
56
        );
57
    }
58
59
    /**
60
     * Add the constraints for a relationship query on the same table.
61
     *
62
     * @param \Illuminate\Database\Eloquent\Builder $query
63
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
64
     * @param array|mixed $columns
65
     * @return \Illuminate\Database\Eloquent\Builder
66
     */
67 2
    public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
68
    {
69 2
        $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());
0 ignored issues
show
Bug introduced by
It seems like getRelationCountHash() 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

69
        $query->from($query->getModel()->getTable().' as '.$hash = $this->/** @scrutinizer ignore-call */ getRelationCountHash());
Loading history...
70
71 2
        $farKey = $this->jsonColumn($query, $this->throughParent, $hash.'.'.$this->secondKey, $this->secondLocalKey);
72
73 2
        $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey);
74
75 2
        $query->getModel()->setTable($hash);
76
77 2
        $firstKey = $this->jsonColumn($query, $this->farParent, $this->getQualifiedFirstKeyName(), $this->localKey);
78
79 2
        return $query->select($columns)->whereColumn(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->select($c...calKey, '=', $firstKey) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
80 2
            $parentQuery->getQuery()->from.'.'.$this->localKey,
81 2
            '=',
82
            $firstKey
83
        );
84
    }
85
86
    /**
87
     * Add the constraints for a relationship query on the same table as the through parent.
88
     *
89
     * @param \Illuminate\Database\Eloquent\Builder $query
90
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
91
     * @param array|mixed $columns
92
     * @return \Illuminate\Database\Eloquent\Builder
93
     */
94 2
    public function getRelationExistenceQueryForThroughSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
95
    {
96 2
        $table = $this->throughParent->getTable().' as '.$hash = $this->getRelationCountHash();
97
98 2
        $farKey = $this->jsonColumn($query, $this->throughParent, $this->getQualifiedFarKeyName(), $this->secondLocalKey);
99
100 2
        $query->join($table, $hash.'.'.$this->secondLocalKey, '=', $farKey);
101
102 2
        if ($this->throughParentSoftDeletes()) {
103 2
            $query->whereNull($hash.'.'.$this->throughParent->getDeletedAtColumn());
104
        }
105
106 2
        $firstKey = $this->jsonColumn($query, $this->farParent, $hash.'.'.$this->firstKey, $this->localKey);
107
108 2
        return $query->select($columns)->whereColumn(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->select($c...calKey, '=', $firstKey) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
109 2
            $parentQuery->getQuery()->from.'.'.$this->localKey,
110 2
            '=',
111
            $firstKey
112
        );
113
    }
114
}
115