GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

BaseModelEventsTrait::usingSoftDeletes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Milkmeowo\Framework\Base\Traits;
4
5
use Milkmeowo\Framework\Base\Models\Observers\BaseModelObserver;
6
use Milkmeowo\Framework\Base\Repositories\Interfaces\BaseRepositoryEventsInterface;
7
8
/**
9
 * Whenever a new model is saved for the first time,
10
 * the creating and created events will fire.
11
 * If a model already existed in the database and the save method is called,
12
 * the updating / updated events will fire.
13
 * However, in both cases, the saving / saved events will fire.
14
 *
15
 * @CREATE: saving > creating > created > saved
16
 * @UPDATE: saving > updating > updated > saved
17
 * @DELETE: deleting > deleted
18
 * @RESTORE: restoring > restored
19
 */
20
trait BaseModelEventsTrait
21
{
22
    /**
23
     * @var BaseRepositoryEventsInterface
24
     */
25
    public static $repository;
26
27
    /**
28
     * booted. observe the base model event with priority 99.
29
     *
30
     * @return mixed
31
     */
32
    public static function bootBaseModelEventsTrait()
33
    {
34
        // Setup event bindings
35
        $priority = 99;
36
37
        static::observe(new BaseModelObserver(), $priority);
38
    }
39
40
    /**
41
     * set the related RepositoryEventsInterface.
42
     *
43
     * @param BaseRepositoryEventsInterface $repository
44
     */
45
    public function setRepository(BaseRepositoryEventsInterface $repository)
46
    {
47
        self::$repository = $repository;
48
    }
49
50
    /**
51
     * get the related RepositoryEventsInterface.
52
     *
53
     * @return BaseRepositoryEventsInterface
54
     */
55
    public function getRepository()
56
    {
57
        return self::$repository;
58
    }
59
60
    /**
61
     * Listen a creating model event.
62
     *
63
     * @return mixed the function result
64
     */
65
    public function onCreating()
66
    {
67
68
        // auto set related user id
69
        if ($this->autoRelatedUserId && empty($this->{static::RELATED_USER_ID}) && $this->hasTableColumn(static::RELATED_USER_ID)) {
0 ignored issues
show
Bug introduced by
The property autoRelatedUserId 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...
Bug introduced by
It seems like hasTableColumn() 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...
70
            $user_id = $this->getAuthUserId();
0 ignored issues
show
Bug introduced by
It seems like getAuthUserId() 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...
71
            if ($user_id > 0) {
72
                $this->{static::RELATED_USER_ID} = $user_id;
73
            }
74
        }
75
76
        $repository = $this->getRepository();
77
        if ($repository instanceof BaseRepositoryEventsInterface) {
78
            $repository->onCreating();
79
        }
80
    }
81
82
    /**
83
     * Listen a created model event.
84
     *
85
     * @return mixed the function result
86
     */
87
    public function onCreated()
88
    {
89
        $repository = $this->getRepository();
90
        if ($repository instanceof BaseRepositoryEventsInterface) {
91
            $repository->onCreated();
92
        }
93
    }
94
95
    /**
96
     * Listen a updating model event.
97
     *
98
     * @return mixed the function result
99
     */
100
    public function onUpdating()
101
    {
102
        $repository = $this->getRepository();
103
        if ($repository instanceof BaseRepositoryEventsInterface) {
104
            $repository->onUpdating();
105
        }
106
    }
107
108
    /**
109
     * Listen a updated model event.
110
     *
111
     * @return mixed the function result
112
     */
113
    public function onUpdated()
114
    {
115
        $repository = $this->getRepository();
116
        if ($repository instanceof BaseRepositoryEventsInterface) {
117
            $repository->onUpdated();
118
        }
119
    }
120
121
    /**
122
     * Listen a saving model event.
123
     *
124
     * @return mixed the function result
125
     */
126
    public function onSaving()
127
    {
128
129
        // update ipstamps if true
130
        if ($this->ipstamps) {
0 ignored issues
show
Bug introduced by
The property ipstamps 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...
131
            $this->updateIps();
0 ignored issues
show
Bug introduced by
It seems like updateIps() 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...
132
        }
133
134
        // update userstamps if true
135
        if ($this->userstamps) {
0 ignored issues
show
Bug introduced by
The property userstamps 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...
136
            $this->updateUsers();
0 ignored issues
show
Bug introduced by
It seems like updateUsers() 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...
137
        }
138
139
        $repository = $this->getRepository();
140
        if ($repository instanceof BaseRepositoryEventsInterface) {
141
            $repository->onSaving();
142
        }
143
    }
144
145
    /**
146
     * Listen a saved model event.
147
     *
148
     * @return mixed the function result
149
     */
150
    public function onSaved()
151
    {
152
        $repository = $this->getRepository();
153
        if ($repository instanceof BaseRepositoryEventsInterface) {
154
            $repository->onSaved();
155
        }
156
    }
157
158
    /**
159
     * Listen a deleting model event.
160
     *
161
     * @return mixed the function result
162
     */
163
    public function onDeleting()
164
    {
165
        if (static::usingSoftDeletes()) {
166
            if ($this->hasTableColumn(static::DELETED_BY)) {
0 ignored issues
show
Bug introduced by
It seems like hasTableColumn() 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...
167
                $this->{static::DELETED_BY} = $this->getAuthUserId();
0 ignored issues
show
Bug introduced by
It seems like getAuthUserId() 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...
168
            }
169
170
            if ($this->hasTableColumn(static::DELETED_IP)) {
0 ignored issues
show
Bug introduced by
It seems like hasTableColumn() 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...
171
                $this->{static::DELETED_IP} = smart_get_client_ip();
172
            }
173
174
            $this->flushEventListeners();
0 ignored issues
show
Bug introduced by
It seems like flushEventListeners() 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...
175
            $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...
176
        }
177
178
        $repository = $this->getRepository();
179
        if ($repository instanceof BaseRepositoryEventsInterface) {
180
            $repository->onDeleting();
181
        }
182
    }
183
184
    /**
185
     * Listen a deleted model event.
186
     *
187
     * @return mixed the function result
188
     */
189
    public function onDeleted()
190
    {
191
        $repository = $this->getRepository();
192
        if ($repository instanceof BaseRepositoryEventsInterface) {
193
            $repository->onDeleted();
194
        }
195
    }
196
197
    /**
198
     * Listen a restoring model event.
199
     *
200
     * @return mixed the function result
201
     */
202
    public function onRestoring()
203
    {
204
        if ($this->hasTableColumn(static::DELETED_BY)) {
0 ignored issues
show
Bug introduced by
It seems like hasTableColumn() 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...
205
            $this->{static::DELETED_BY} = null;
206
        }
207
        if ($this->hasTableColumn(static::DELETED_IP)) {
0 ignored issues
show
Bug introduced by
It seems like hasTableColumn() 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...
208
            $this->{static::DELETED_IP} = null;
209
        }
210
211
        $repository = $this->getRepository();
212
        if ($repository instanceof BaseRepositoryEventsInterface) {
213
            $repository->onRestoring();
214
        }
215
    }
216
217
    /**
218
     * Listen a restored model event.
219
     *
220
     * @return mixed the function result
221
     */
222
    public function onRestored()
223
    {
224
        $repository = $this->getRepository();
225
        if ($repository instanceof BaseRepositoryEventsInterface) {
226
            $repository->onRestored();
227
        }
228
    }
229
230
    /**
231
     * Has the model loaded the SoftDeletes trait.
232
     *
233
     * @return bool
234
     */
235
    public static function usingSoftDeletes()
236
    {
237
        static $usingSoftDeletes;
238
239
        if (is_null($usingSoftDeletes)) {
240
            return $usingSoftDeletes = in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses(get_called_class()));
241
        }
242
243
        return $usingSoftDeletes;
244
    }
245
}
246