Passed
Push — master ( 1e2780...ad2957 )
by Jonas
11:35
created

IsRecursiveRelation::delete()   A

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 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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 219
    public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
29
    {
30 219
        $this->andSelf = $andSelf;
31
32 219
        parent::__construct($query, $parent, $foreignKey, $localKey);
33 219
    }
34
35
    /**
36
     * Build model dictionary.
37
     *
38
     * @param \Illuminate\Database\Eloquent\Collection $results
39
     * @return array
40
     */
41 50
    protected function buildDictionary(Collection $results)
42
    {
43 50
        return $results->mapToDictionary(function (Model $result) {
44 50
            return [$result->getFirstPathSegment() => $result];
45 50
        })->all();
46
    }
47
48
    /**
49
     * Get the fully qualified local key name.
50
     *
51
     * @return string
52
     */
53 149
    public function getQualifiedLocalKeyName()
54
    {
55 149
        return $this->qualifyColumn($this->localKey);
0 ignored issues
show
Bug introduced by
It seems like qualifyColumn() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( 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
     * Update records in the database.
60
     *
61
     * @param array $values
62
     * @return int
63
     */
64 8
    public function update(array $values)
65
    {
66 8
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__, func_get_args());
67
    }
68
69
    /**
70
     * Decrement a column's value by a given amount.
71
     *
72
     * @param string $column
73
     * @param float|int $amount
74
     * @param array $extra
75
     * @return int
76
     */
77 4
    public function increment($column, $amount = 1, array $extra = [])
78
    {
79 4
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__, func_get_args());
80
    }
81
82
    /**
83
     * Decrement a column's value by a given amount.
84
     *
85
     * @param string|\Illuminate\Database\Query\Expression $column
86
     * @param float|int $amount
87
     * @param array $extra
88
     * @return int
89
     */
90 4
    public function decrement($column, $amount = 1, array $extra = [])
91
    {
92 4
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__, func_get_args());
93
    }
94
95
    /**
96
     * Delete records from the database.
97
     *
98
     * @return mixed
99
     */
100 4
    public function delete()
101
    {
102 4
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__);
103
    }
104
105
    /**
106
     * Force a hard delete on a soft deleted model.
107
     *
108
     * @return bool|null
109
     */
110 4
    public function forceDelete()
111
    {
112 4
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__);
113
    }
114
115
    /**
116
     * Execute an update or delete query after adding the necessary "where in" constraint.
117
     *
118
     * @param string $method
119
     * @param array $parameters
120
     * @return mixed
121
     */
122 24
    public function executeUpdateOrDeleteQuery($method, array $parameters = [])
123
    {
124 24
        $expression = $this->query->getQuery()->from;
125
126 24
        $table = $this->parent->getTable();
127
128 24
        $this->query->getQuery()->from = $table;
129
130 24
        $this->query->getModel()->setTable($table);
131
132 24
        $keys = $this->query->getQuery()->newQuery()->from($expression)->select($this->localKey);
133
134 24
        return $this->query->whereIn($this->getQualifiedLocalKeyName(), $keys)->$method(...$parameters);
135
    }
136
}
137