MorphMany::matchOneOrManySimple()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 6
nop 4
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
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