Completed
Push — master ( 135018...0f0a30 )
by Andrey
04:47
created

Relation   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 23
cts 28
cp 0.8214
rs 10
c 0
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A buildMorphMapFromModels() 0 9 3
A addEagerConstraintsSimple() 0 3 1
A morphMap() 0 10 4
A getKeys() 0 5 2
A eagerLoadAndMatchSimple() 0 5 1
A initSimpleRelation() 0 8 2
A getEagerSimple() 0 3 1
1
<?php
2
3
namespace Volosyuk\SimpleEloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
use stdClass;
9
use Volosyuk\SimpleEloquent\Builder;
10
use Volosyuk\SimpleEloquent\ModelAccessor;
11
12
/**
13
 * Trait SimpleRelation
14
 * @package Volosyuk\SimpleEloquent
15
 *
16
 * @property Builder $query
17
 * @property Model $parent
18
 */
19
trait Relation
20
{
21
    /**
22
     * An array to map class names to their morph names in database.
23
     *
24
     * @var array
25
     */
26
    public static $morphMap = [];
27
28
    /**
29
     * @param $models
30
     * @param $name
31
     * @return array
32
     */
33 9
    public function eagerLoadAndMatchSimple($models, $name)
34
    {
35 9
        $results = $this->getEagerSimple();
36
37 9
        return $this->matchSimple($models, $results, $name);
0 ignored issues
show
Bug introduced by
It seems like matchSimple() 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

37
        return $this->/** @scrutinizer ignore-call */ matchSimple($models, $results, $name);
Loading history...
38
    }
39
40
    /**
41
     * @param array $models
42
     * @param $relation
43
     * @return array|stdClass[]
44
     */
45 10
    public function initSimpleRelation(array &$models, $relation)
46
    {
47 10
        foreach ($models as &$model) {
48 10
            ModelAccessor::set($model, $relation, null);
49
        }
50 10
        unset($model);
51
52 10
        return $models;
53
    }
54
55
    /**
56
     * Get the relationship for eager loading.
57
     *
58
     * @return Collection
59
     */
60 9
    protected function getEagerSimple()
61
    {
62 9
        return $this->getSimple();
0 ignored issues
show
Bug introduced by
It seems like getSimple() 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

62
        return $this->/** @scrutinizer ignore-call */ getSimple();
Loading history...
63
    }
64
65
    /**
66
     * Get all of the primary keys for an array of models.
67
     *
68
     * @param  array   $models
69
     * @param  string  $key
70
     * @return array
71
     */
72
    protected function getKeys(array $models, $key = null)
73
    {
74 6
        return array_unique(array_values(array_map(function ($value) use ($key) {
75 6
            return $key ? ModelAccessor::get($value, $key) : ModelAccessor::get($value, $this->parent->getKeyName());
76 6
        }, $models)));
77
    }
78
79
    /**
80
     * Set the constraints for an eager load of the relation.
81
     *
82
     * @param  array  $models
83
     * @return void
84
     */
85 4
    public function addEagerConstraintsSimple(array $models)
86
    {
87 4
        $this->query->whereIn($this->getQualifiedForeignKeyName(), $this->getKeys($models));
0 ignored issues
show
Bug introduced by
It seems like getQualifiedForeignKeyName() 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

87
        $this->query->whereIn($this->/** @scrutinizer ignore-call */ getQualifiedForeignKeyName(), $this->getKeys($models));
Loading history...
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

87
        $this->query->/** @scrutinizer ignore-call */ 
88
                      whereIn($this->getQualifiedForeignKeyName(), $this->getKeys($models));
Loading history...
88 4
    }
89
90
91
    /**
92
     * Set or get the morph map for polymorphic relations.
93
     *
94
     * @param  array|null  $map
95
     * @param  bool  $merge
96
     * @return array
97
     */
98 1
    public static function morphMap(array $map = null, $merge = true)
99
    {
100 1
        $map = static::buildMorphMapFromModels($map);
101
102 1
        if (is_array($map)) {
103
            static::$morphMap = $merge && static::$morphMap
0 ignored issues
show
Bug Best Practice introduced by
The expression static::morphMap of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
                ? $map + static::$morphMap : $map;
105
        }
106
107 1
        return static::$morphMap;
108
    }
109
110
111
    /**
112
     * Builds a table-keyed array from model class names.
113
     *
114
     * @param  string[]|null  $models
115
     * @return array|null
116
     */
117 1
    protected static function buildMorphMapFromModels(array $models = null)
118
    {
119 1
        if (is_null($models) || Arr::isAssoc($models)) {
120 1
            return $models;
121
        }
122
123
        return array_combine(array_map(function ($model) {
124
            return (new $model)->getTable();
125
        }, $models), $models);
126
    }
127
}
128