Passed
Push — master ( 0ba402...934d75 )
by Bas
05:33
created

MorphTo   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 42
rs 10
c 2
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addConstraints() 0 4 2
A getResultsByType() 0 22 1
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Eloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Relations\MorphTo as IlluminateMorphTo;
6
7
class MorphTo extends IlluminateMorphTo
8
{
9
    /**
10
     * Set the base constraints on the relation query.
11
     *
12
     * @return void
13
     */
14
    public function addConstraints()
15
    {
16
        if (static::$constraints) {
17
            $this->query->where($this->ownerKey, '==', $this->child->{$this->foreignKey});
18
        }
19
    }
20
21
    /**
22
     * Get all of the relation results for a type.
23
     *
24
     * @param  string  $type
25
     * @return \Illuminate\Database\Eloquent\Collection
26
     */
27
    protected function getResultsByType($type)
28
    {
29
        $instance = $this->createModelByType($type);
30
31
        $ownerKey = $this->ownerKey ?? $instance->getKeyName();
32
33
        $query = $this->replayMacros($instance->newQuery())
34
            ->mergeConstraintsFrom($this->getQuery())
35
            ->with(array_merge(
36
                $this->getQuery()->getEagerLoads(),
37
                (array) ($this->morphableEagerLoads[get_class($instance)] ?? [])
38
            ))
39
            ->withCount(
40
                (array) ($this->morphableEagerLoadCounts[get_class($instance)] ?? [])
41
            );
42
43
        $whereIn = $this->whereInMethod($instance, $ownerKey);
44
45
        return $query->{$whereIn}(
46
            $ownerKey,
47
            $this->gatherKeysByType($type)
48
        )->get();
49
    }
50
}
51