SoftDeletes::getQualifiedDeletedByColumn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Database\Eloquent\Traits;
12
13
use Longman\Platfourm\Contracts\Auth\AuthUserService;
14
15
trait SoftDeletes
16
{
17
    /**
18
     * Indicates if the model is currently force deleting.
19
     *
20
     * @var bool
21
     */
22
    protected $forceDeleting = false;
23
24
    /**
25
     * Boot the soft deleting trait for a model.
26
     *
27
     * @return void
28
     */
29
    public static function bootSoftDeletes()
30
    {
31
        static::addGlobalScope(new SoftDeletingScope);
32
    }
33
34
    /**
35
     * Force a hard delete on a soft deleted model.
36
     *
37
     * @return bool|null
38
     */
39
    public function forceDelete()
40
    {
41
        $this->forceDeleting = true;
42
43
        $deleted = $this->delete();
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Longman\Platfourm\Databa...uent\Traits\SoftDeletes. Did you maybe mean bootSoftDeletes()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
45
        $this->forceDeleting = false;
46
47
        return $deleted;
48
    }
49
50
    /**
51
     * Perform the actual delete query on this model instance.
52
     *
53
     * @return mixed
54
     */
55
    protected function performDeleteOnModel()
56
    {
57
        if ($this->forceDeleting) {
58
            return $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey())->forceDelete();
0 ignored issues
show
Bug introduced by
It seems like newQueryWithoutScopes() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
59
        }
60
61
        return $this->runSoftDelete();
62
    }
63
64
    /**
65
     * Perform the actual delete query on this model instance.
66
     *
67
     * @return void
68
     */
69
    protected function runSoftDelete()
70
    {
71
        $query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());
0 ignored issues
show
Bug introduced by
It seems like newQueryWithoutScopes() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
73
        $this->{$this->getDeletedAtColumn()} = $time = $this->freshTimestamp();
0 ignored issues
show
Bug introduced by
It seems like freshTimestamp() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
75
        $update = [
76
            $this->getDeletedAtColumn() => $this->fromDateTime($time),
0 ignored issues
show
Bug introduced by
It seems like fromDateTime() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
77
        ];
78
79
        $userService = app()->make(AuthUserService::class);
80
        if ($userService->check()) {
81
            $this->{$this->getDeletedByColumn()} = $userService->user()->id;
82
            $update[$this->getDeletedByColumn()] = $userService->user()->id;
83
        }
84
85
        $query->update($update);
86
    }
87
88
    /**
89
     * Restore a soft-deleted model instance.
90
     *
91
     * @return bool|null
92
     */
93
    public function restore()
94
    {
95
        // If the restoring event does not return false, we will proceed with this
96
        // restore operation. Otherwise, we bail out so the developer will stop
97
        // the restore totally. We will clear the deleted timestamp and save.
98
        if ($this->fireModelEvent('restoring') === false) {
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
99
            return false;
100
        }
101
102
        $this->{$this->getDeletedAtColumn()} = null;
103
        $this->{$this->getDeletedByColumn()} = null;
104
105
        // Once we have saved the model, we will fire the "restored" event so this
106
        // developer will do anything they need to after a restore operation is
107
        // totally finished. Then we will return the result of the save call.
108
        $this->exists = true;
0 ignored issues
show
Bug introduced by
The property exists does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
109
110
        $result = $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
111
112
        $this->fireModelEvent('restored', false);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
113
114
        return $result;
115
    }
116
117
    /**
118
     * Determine if the model instance has been soft-deleted.
119
     *
120
     * @return bool
121
     */
122
    public function trashed()
123
    {
124
        return !is_null($this->{$this->getDeletedAtColumn()});
125
    }
126
127
    /**
128
     * Get a new query builder that includes soft deletes.
129
     *
130
     * @return \Illuminate\Database\Eloquent\Builder|static
131
     */
132
    public static function withTrashed()
133
    {
134
        return (new static)->newQueryWithoutScope(new SoftDeletingScope);
0 ignored issues
show
Bug introduced by
It seems like newQueryWithoutScope() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
135
    }
136
137
    /**
138
     * Get a new query builder that only includes soft deletes.
139
     *
140
     * @return \Illuminate\Database\Eloquent\Builder|static
141
     */
142
    public static function onlyTrashed()
143
    {
144
        $instance = new static;
145
146
        $column = $instance->getQualifiedDeletedAtColumn();
147
148
        return $instance->newQueryWithoutScope(new SoftDeletingScope)->whereNotNull($column);
0 ignored issues
show
Bug introduced by
It seems like newQueryWithoutScope() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
149
    }
150
151
    /**
152
     * Register a restoring model event with the dispatcher.
153
     *
154
     * @param  \Closure|string $callback
155
     * @return void
156
     */
157
    public static function restoring($callback)
158
    {
159
        static::registerModelEvent('restoring', $callback);
160
    }
161
162
    /**
163
     * Register a restored model event with the dispatcher.
164
     *
165
     * @param  \Closure|string $callback
166
     * @return void
167
     */
168
    public static function restored($callback)
169
    {
170
        static::registerModelEvent('restored', $callback);
171
    }
172
173
    /**
174
     * Get the name of the "deleted at" column.
175
     *
176
     * @return string
177
     */
178
    public function getDeletedAtColumn()
179
    {
180
        return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
181
    }
182
183
    /**
184
     * Get the name of the "deleted by" column.
185
     *
186
     * @return string
187
     */
188
    public function getDeletedByColumn()
189
    {
190
        return defined('static::DELETED_BY') ? static::DELETED_BY : 'deleted_by';
191
    }
192
193
    /**
194
     * Get the fully qualified "deleted at" column.
195
     *
196
     * @return string
197
     */
198
    public function getQualifiedDeletedAtColumn()
199
    {
200
        return $this->getTable() . '.' . $this->getDeletedAtColumn();
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
201
    }
202
203
    /**
204
     * Get the fully qualified "deleted by" column.
205
     *
206
     * @return string
207
     */
208
    public function getQualifiedDeletedByColumn()
209
    {
210
        return $this->getTable() . '.' . $this->getDeletedByColumn();
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
211
    }
212
213
}
214