HasOneThrough   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 45
ccs 14
cts 14
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addEagerConstraintsSimple() 0 5 1
A matchSimple() 0 20 4
1
<?php
2
3
namespace Volosyuk\SimpleEloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Relations\HasOneThrough as BaseHasOneThrough;
6
use Illuminate\Support\Collection;
7
use Volosyuk\SimpleEloquent\ModelAccessor;
8
9
class HasOneThrough extends BaseHasOneThrough
10
{
11
    use Relation, Pivot;
0 ignored issues
show
introduced by
The trait Volosyuk\SimpleEloquent\Relations\Pivot requires some properties which are not provided by Volosyuk\SimpleEloquent\Relations\HasOneThrough: $foreignPivotKey, $columns
Loading history...
12
13
    /**
14
     * Match the eagerly loaded results to their parents.
15
     *
16
     * @param  array   $models
17
     * @param  Collection  $results
18
     * @param  string  $relation
19
     * @return array
20
     */
21 1
    public function matchSimple(array &$models, Collection $results, $relation)
22
    {
23 1
        $dictionary = [];
24
25 1
        foreach ($results as $result) {
26 1
            $dictionary[ModelAccessor::get($result, 'laravel_through_key')][] = $result;
27
        }
28
29 1
        foreach ($models as &$model) {
30 1
            $keyName = $this->parent->getKeyName();
31
32 1
            if (isset($dictionary[$key = ModelAccessor::get($model, $keyName)])) {
33 1
                $value = $dictionary[$key];
34
35 1
                ModelAccessor::set($model, $relation, reset($value));
36
            }
37
        }
38 1
        unset($model);
39
40 1
        return $models;
41
    }
42
43
    /**
44
     * Set the constraints for an eager load of the relation.
45
     *
46
     * @param  array  $models
47
     * @return void
48
     */
49 1
    public function addEagerConstraintsSimple(array $models)
50
    {
51 1
        $table = $this->parent->getTable();
52
53 1
        $this->query->whereIn($table.'.'.$this->firstKey, $this->getKeys($models, $this->localKey));
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

53
        $this->query->/** @scrutinizer ignore-call */ 
54
                      whereIn($table.'.'.$this->firstKey, $this->getKeys($models, $this->localKey));
Loading history...
54 1
    }
55
}
56