Passed
Push — master ( 4e5334...4eb7e1 )
by Jonas
04:13
created

IsRecursiveRelation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
c 2
b 0
f 0
dl 0
loc 75
ccs 20
cts 20
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getQualifiedLocalKeyName() 0 3 1
A buildDictionary() 0 5 1
A __call() 0 19 2
A __construct() 0 5 1
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
9
trait IsRecursiveRelation
10
{
11
    /**
12
     * Whether to include the parent model.
13
     *
14
     * @var bool
15
     */
16
    protected $andSelf;
17
18
    /**
19
     * Create a new recursive relationship instance.
20
     *
21
     * @param \Illuminate\Database\Eloquent\Builder $query
22
     * @param \Illuminate\Database\Eloquent\Model $parent
23
     * @param string $foreignKey
24
     * @param string $localKey
25
     * @param bool $andSelf
26
     * @return void
27
     */
28 140
    public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
29
    {
30 140
        $this->andSelf = $andSelf;
31
32 140
        parent::__construct($query, $parent, $foreignKey, $localKey);
33 140
    }
34
35
    /**
36
     * Build model dictionary.
37
     *
38
     * @param \Illuminate\Database\Eloquent\Collection $results
39
     * @return array
40
     */
41 32
    protected function buildDictionary(Collection $results)
42
    {
43 32
        return $results->mapToDictionary(function (Model $result) {
44 32
            return [$result->getFirstPathSegment() => $result];
45 32
        })->all();
46
    }
47
48
    /**
49
     * Get the fully qualified local key name.
50
     *
51
     * @return string
52
     */
53 88
    public function getQualifiedLocalKeyName()
54
    {
55 88
        return $this->qualifyColumn($this->localKey);
0 ignored issues
show
Bug introduced by
The method qualifyColumn() does not exist on Staudenmeir\LaravelAdjac...ons\IsRecursiveRelation. 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

55
        return $this->/** @scrutinizer ignore-call */ qualifyColumn($this->localKey);
Loading history...
56
    }
57
58
    /**
59
     * Handle dynamic method calls to the relationship.
60
     *
61
     * @param string $method
62
     * @param array $parameters
63
     * @return mixed
64
     */
65 117
    public function __call($method, $parameters)
66
    {
67 117
        $methods = ['update', 'increment', 'decrement', 'delete', 'forceDelete'];
68
69 117
        if (in_array($method, $methods)) {
70 12
            $expression = $this->query->getQuery()->from;
71
72 12
            $table = $this->parent->getTable();
73
74 12
            $this->query->getQuery()->from = $table;
75
76 12
            $this->query->getModel()->setTable($table);
77
78 12
            $keys = $this->query->getQuery()->newQuery()->from($expression)->select($this->localKey);
79
80 12
            return $this->query->whereIn($this->getQualifiedLocalKeyName(), $keys)->$method(...$parameters);
81
        }
82
83 117
        return parent::__call($method, $parameters);
84
    }
85
}
86