Completed
Push — master ( c7117c...195c3c )
by Andrey
05:48
created

MorphTo   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
dl 0
loc 121
ccs 31
cts 35
cp 0.8857
rs 10
c 0
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSimpleResultsByType() 0 17 1
B matchSimpleToMorphParents() 0 13 5
A eagerLoadAndMatchSimple() 0 5 1
A buildDictionarySimple() 0 5 3
A getEagerSimple() 0 7 2
A addEagerConstraintsSimple() 0 3 1
A gatherSimpleKeysByType() 0 7 1
1
<?php
2
3
namespace Volosyuk\SimpleEloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Volosyuk\SimpleEloquent\Builder;
8
use Volosyuk\SimpleEloquent\ModelAccessor;
9
10
/**
11
 * Class MorphToWithSimple
12
 * @package Volosyuk\SimpleEloquent
13
 */
14
class MorphTo extends \Illuminate\Database\Eloquent\Relations\MorphTo
15
{
16
    use Relation;
17
18
    /**
19
     * Get the results of the relationship.
20
     *
21
     * Called via eager load method of Eloquent query builder.
22
     *
23
     * @return mixed
24
     */
25 1
    protected function getEagerSimple()
26
    {
27 1
        foreach (array_keys($this->dictionary) as $type) {
28 1
            $this->matchSimpleToMorphParents($type, $this->getSimpleResultsByType($type));
29
        }
30
31 1
        return $this->models;
32
    }
33
34
    /**
35
     * @param $models
36
     * @param $name
37
     * @return mixed
38
     */
39 1
    public function eagerLoadAndMatchSimple($models, $name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

39
    public function eagerLoadAndMatchSimple($models, /** @scrutinizer ignore-unused */ $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41 1
        $this->addEagerConstraintsSimple($models);
42
43 1
        return $this->getEagerSimple();
44
    }
45
46
    /**
47
     * Get all of the relation results for a type.
48
     *
49
     * @param  string  $type
50
     * @return Collection
51
     */
52 1
    protected function getSimpleResultsByType($type)
53
    {
54
        /**
55
         * @var Model $instance
56
         */
57 1
        $instance = $this->createModelByType($type);
58
59
        /**
60
         * @var Builder $query
61
         */
62 1
        $query = $this->replayMacros($instance->newQuery())
63 1
            ->mergeConstraintsFrom($this->getQuery())
64 1
            ->with($this->getQuery()->getEagerLoads());
65
66 1
        return $query->whereIn(
0 ignored issues
show
Bug introduced by
The method whereIn() does not exist on Volosyuk\SimpleEloquent\Builder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        return $query->/** @scrutinizer ignore-call */ whereIn(
Loading history...
67 1
            $instance->getTable().'.'.$instance->getKeyName(), $this->gatherKeysByType($type)
68 1
        )->getSimple();
69
    }
70
71
    /**
72
     * Match the results for a given type to their parents.
73
     *
74
     * @param  string  $type
75
     * @param  Collection  $results
76
     * @return void
77
     */
78 1
    protected function matchSimpleToMorphParents($type, Collection $results)
79
    {
80 1
        foreach ($results as $result) {
81 1
            foreach ($this->models as &$model) {
82
                if (
83 1
                    ModelAccessor::get($model, $this->morphType) == $type
84
                    &&
85 1
                    ModelAccessor::get($model, $this->foreignKey) == ModelAccessor::get($result, $this->parent->getKeyName())
86
                ) {
87 1
                    ModelAccessor::set($model, $this->relation, $result);
88
                }
89
            }
90 1
            unset($model);
91
        }
92 1
    }
93
94
95
    /**
96
     * Set the constraints for an eager load of the relation.
97
     *
98
     * @param  array  $models
99
     * @return void
100
     */
101 1
    public function addEagerConstraintsSimple(array $models)
102
    {
103 1
        $this->buildDictionarySimple($this->models = $models);
0 ignored issues
show
Documentation Bug introduced by
It seems like $models of type array is incompatible with the declared type Illuminate\Database\Eloquent\Collection of property $models.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
104 1
    }
105
106
107
    /**
108
     * Build a dictionary with the models.
109
     *
110
     * @param  array  $models
111
     * @return void
112
     */
113 1
    protected function buildDictionarySimple(array $models)
114
    {
115 1
        foreach ($models as $model) {
116 1
            if (ModelAccessor::get($model, $this->morphType)) {
117 1
                $this->dictionary[ModelAccessor::get($model, $this->morphType)][ModelAccessor::get($model, $this->foreignKey)][] = $model;
118
            }
119
        }
120 1
    }
121
122
    /**
123
     * Gather all of the foreign keys for a given type.
124
     *
125
     * @param  string  $type
126
     * @return array
127
     */
128
    protected function gatherSimpleKeysByType($type)
129
    {
130
        $foreign = $this->foreignKey;
131
132
        return collect($this->dictionary[$type])->map(function ($models) use ($foreign) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($this->di... })->values()->unique() returns the type Illuminate\Support\Collection which is incompatible with the documented return type array.
Loading history...
133
            return ModelAccessor::get(head($models), $foreign);
134
        })->values()->unique();
135
    }
136
}
137