Passed
Push — master ( 4d1569...691091 )
by Stephen
02:49 queued 10s
created

ModelEvents::wasTrackingEventFired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace Sfneal\CrudModelActions\Utils;
5
6
7
use Illuminate\Database\Eloquent\Model;
8
use Sfneal\Events\AbstractEvent;
9
use Sfneal\Models\AbstractModel;
10
11
trait ModelEvents
12
{
13
    /**
14
     * @var AbstractEvent Model Tracking Event to fire
15
     */
16
    protected $trackingEvent;
17
18
    /**
19
     * @var AbstractModel|Model
20
     */
21
    private $trackingEventModel;
22
23
    /**
24
     * @var bool Determine if the TrackingEvent was fired
25
     */
26
    private $trackingEventWasFired;
27
28
    /**
29
     * Fire the trackingEvent
30
     *
31
     * @return void
32
     */
33
    protected function fireEvent()
34
    {
35
        if (isset($this->trackingEvent)) {
36
            event(new $this->trackingEvent($this->trackingEventModel()));
0 ignored issues
show
Unused Code introduced by
The call to Sfneal\Events\AbstractEvent::__construct() has too many arguments starting with $this->trackingEventModel(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
            event(/** @scrutinizer ignore-call */ new $this->trackingEvent($this->trackingEventModel()));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
37
            $this->trackingEventWasFired = true;
38
        }
39
    }
40
41
    /**
42
     * Retrieve the Model to be used in the fireEvent() method
43
     *
44
     *  - optionally set a the $trackingEventModel property
45
     *  - useful for using a different model in the trackingEvent than the resolved/saved model
46
     *
47
     * @param Model|null $model
48
     * @return AbstractModel|Model
49
     */
50
    protected function trackingEventModel(Model $model = null): Model
51
    {
52
        if (isset($model)) {
53
            $this->trackingEventModel = $model;
54
        }
55
56
        return $this->trackingEventModel ?? $this->model;
57
    }
58
59
    /**
60
     * Determine if the TrackingEvent was fired
61
     *
62
     * @return bool
63
     */
64
    protected function wasTrackingEventFired(): bool
65
    {
66
        return $this->trackingEventWasFired;
67
    }
68
}
69