Completed
Push — master ( 184287...689cb9 )
by Andrey
02:44
created

Relation::buildMorphMapFromModels()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 12
rs 9.6666
c 0
b 0
f 0
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
     * @param $models
23
     * @param $name
24
     * @return array
25
     */
26 9
    public function eagerLoadAndMatchSimple($models, $name)
27
    {
28 9
        $results = $this->getEagerSimple();
29
30 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

30
        return $this->/** @scrutinizer ignore-call */ matchSimple($models, $results, $name);
Loading history...
31
    }
32
33
    /**
34
     * @param array $models
35
     * @param $relation
36
     * @return array|stdClass[]
37
     */
38 10
    public function initSimpleRelation(array &$models, $relation)
39
    {
40 10
        foreach ($models as &$model) {
41 10
            ModelAccessor::set($model, $relation, null);
42
        }
43 10
        unset($model);
44
45 10
        return $models;
46
    }
47
48
    /**
49
     * Get the relationship for eager loading.
50
     *
51
     * @return Collection
52
     */
53 9
    protected function getEagerSimple()
54
    {
55 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

55
        return $this->/** @scrutinizer ignore-call */ getSimple();
Loading history...
56
    }
57
58
    /**
59
     * Get all of the primary keys for an array of models.
60
     *
61
     * @param  array   $models
62
     * @param  string  $key
63
     * @return array
64
     */
65
    protected function getKeys(array $models, $key = null)
66
    {
67 6
        return array_unique(array_values(array_map(function ($value) use ($key) {
68 6
            return $key ? ModelAccessor::get($value, $key) : ModelAccessor::get($value, $this->parent->getKeyName());
69 6
        }, $models)));
70
    }
71
72
    /**
73
     * Set the constraints for an eager load of the relation.
74
     *
75
     * @param  array  $models
76
     * @return void
77
     */
78 4
    public function addEagerConstraintsSimple(array $models)
79
    {
80 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

80
        $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

80
        $this->query->/** @scrutinizer ignore-call */ 
81
                      whereIn($this->getQualifiedForeignKeyName(), $this->getKeys($models));
Loading history...
81 4
    }
82
83
    /**
84
     * Builds a table-keyed array from model class names.
85
     *
86
     * @param  string[]|null  $models
87
     * @return array|null
88
     */
89
    protected static function buildMorphMapFromModels(array $models = null)
90
    {
91
        if (is_null($models) || Arr::isAssoc($models)) {
92
            return $models;
93
        }
94
95
        return array_combine(array_map(function ($model) {
96
            return (new $model)->getTable();
97
        }, $models), $models);
98
    }
99
}
100