MorphMany::matchSimple()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Volosyuk\SimpleEloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Relations\MorphMany as BaseMorphMany;
6
use Illuminate\Support\Collection;
7
use Volosyuk\SimpleEloquent\ModelAccessor;
8
9
/**
10
 * Class MorphManyWIthSimple
11
 * @package Volosyuk\SimpleEloquent
12
 */
13
class MorphMany extends BaseMorphMany
14
{
15
    use Relation;
16
17
    /**
18
     * Match the eagerly loaded results to their parents.
19
     *
20
     * @param  array   $models
21
     * @param  Collection  $results
22
     * @param  string  $relation
23
     * @return array
24
     */
25 1
    protected function matchSimple(array $models, Collection $results, $relation)
26
    {
27 1
        return $this->matchOneOrManySimple($models, $results, $relation, 'many');
28
    }
29
30
    /**
31
     * Match the eagerly loaded results to their many parents.
32
     *
33
     * @param  array   $models
34
     * @param  Collection  $results
35
     * @param  string  $relation
36
     * @param  string  $type
37
     * @return array
38
     */
39 1
    protected function matchOneOrManySimple(array &$models, Collection $results, $relation, $type)
40
    {
41 1
        $dictionary = [];
42
43 1
        $foreign = $this->getForeignKeyName();
44
45 1
        foreach ($results as $result) {
46 1
            $dictionary[ModelAccessor::get($result, $foreign)][] = $result;
47
        }
48
49 1
        foreach ($models as &$model) {
50 1
            $value = Collection::make();
51
52 1
            if (isset($dictionary[$key = ModelAccessor::get($model, $this->localKey)])) {
53 1
                $value = $this->getRelationValue($dictionary, $key, $type);
54
            }
55
56 1
            ModelAccessor::set($model, $relation, $value);
57
        }
58
59 1
        return $models;
60
    }
61
}
62