Passed
Push — master ( af341e...2b2ec9 )
by Jonas
07:18 queued 05:16
created

appendToDeepRelationship()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 30
ccs 16
cts 16
cp 1
crap 3
rs 9.7333
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations\Relations\Traits\Concatenation;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Query\JoinClause;
8
9
trait IsConcatenableBelongsToJsonRelation
10
{
11
    use IsConcatenableRelation;
12
13
    /**
14
     * Append the relation's through parents, foreign and local keys to a deep relationship.
15
     *
16
     * @param \Illuminate\Database\Eloquent\Model[] $through
17
     * @param array $foreignKeys
18
     * @param array $localKeys
19
     * @param int $position
20
     * @return array
21
     */
22 71
    public function appendToDeepRelationship(array $through, array $foreignKeys, array $localKeys, int $position): array
23
    {
24 71
        if ($position === 0) {
25 23
            $foreignKeys[] = $this->ownerKey;
26
27 23
            $localKeys[] = function (Builder $query, Builder $parentQuery = null) {
28 11
                if ($parentQuery) {
29 5
                    $this->getRelationExistenceQuery($this->query, $parentQuery);
0 ignored issues
show
Bug introduced by
It seems like getRelationExistenceQuery() 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

29
                    $this->/** @scrutinizer ignore-call */ 
30
                           getRelationExistenceQuery($this->query, $parentQuery);
Loading history...
30
                }
31
32 11
                $this->mergeWhereConstraints($query, $this->query);
33
            };
34
        } else {
35 48
            $foreignKeys[] = function (Builder $query, JoinClause $join) {
36 48
                $ownerKey = $this->query->qualifyColumn($this->ownerKey);
37
38 48
                [$sql, $bindings] = $this->relationExistenceQueryOwnerKey($query, $ownerKey);
0 ignored issues
show
Bug introduced by
It seems like relationExistenceQueryOwnerKey() 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
                [$sql, $bindings] = $this->relationExistenceQueryOwnerKey($query, $ownerKey);
Loading history...
39
40 48
                $query->addBinding($bindings, 'join');
41
42 48
                $join->whereJsonContains(
43 48
                    $this->getQualifiedPath(),
0 ignored issues
show
Bug introduced by
It seems like getQualifiedPath() 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

43
                    $this->/** @scrutinizer ignore-call */ 
44
                           getQualifiedPath(),
Loading history...
44 48
                    $query->getQuery()->connection->raw($sql)
45
                );
46
            };
47
48 48
            $localKeys[] = null;
49
        }
50
51 71
        return [$through, $foreignKeys, $localKeys];
52
    }
53
54
    /**
55
     * Match the eagerly loaded results for a deep relationship to their parents.
56
     *
57
     * @param array $models
58
     * @param \Illuminate\Database\Eloquent\Collection $results
59
     * @param string $relation
60
     * @return array
61
     */
62 12
    public function matchResultsForDeepRelationship(array $models, Collection $results, string $relation): array
63
    {
64 12
        $dictionary = $this->buildDictionaryForDeepRelationship($results);
65
66 12
        foreach ($models as $model) {
67 12
            $matches = [];
68
69 12
            foreach ($this->getForeignKeys($model) as $id) {
0 ignored issues
show
Bug introduced by
It seems like getForeignKeys() 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
            foreach ($this->/** @scrutinizer ignore-call */ getForeignKeys($model) as $id) {
Loading history...
70 12
                if (isset($dictionary[$id])) {
71 12
                    $matches = array_merge($matches, $dictionary[$id]);
72
                }
73
            }
74
75 12
            $collection = $this->related->newCollection($matches);
76
77 12
            $model->setRelation($relation, $collection);
78
        }
79
80 12
        return $models;
81
    }
82
83
    /**
84
     * Build the model dictionary for a deep relation.
85
     *
86
     * @param \Illuminate\Database\Eloquent\Collection $results
87
     * @return array
88
     */
89 12
    protected function buildDictionaryForDeepRelationship(Collection $results): array
90
    {
91 12
        $dictionary = [];
92
93 12
        foreach ($results as $result) {
94 12
            $dictionary[$result->laravel_through_key][] = $result;
95
        }
96
97 12
        return $dictionary;
98
    }
99
}
100