Completed
Pull Request — master (#6)
by Richan
14:34
created

CacheableObserver::fireEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Suitmedia\Cacheable;
4
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Illuminate\Database\Eloquent\Model;
7
use Suitmedia\Cacheable\Contracts\CacheableModel;
8
use Suitmedia\Cacheable\Events\CacheableEvent;
9
use Suitmedia\Cacheable\Events\CacheableInvalidated;
10
use Suitmedia\Cacheable\Events\CacheableInvalidating;
11
12
class CacheableObserver
13
{
14
    /**
15
     * Event dispatcher object
16
     *
17
     * @var \Illuminate\Contracts\Events\Dispatcher
18
     */
19
    protected $events;
20
21
    /**
22
     * Cacheable observer constructor
23
     *
24
     * @param \Illuminate\Contracts\Events\Dispatcher $events
25
     */
26
    public function __construct(Dispatcher $events)
27
    {
28
        $this->events = $events;
29
    }
30
31
    /**
32
     * Fire cacheable events
33
     *
34
     * @param  \Cacheable\Events\CacheableEvent $event
0 ignored issues
show
Documentation introduced by
Should the type for parameter $event not be CacheableEvent?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
35
     * @return mixed
36
     */
37
    public function fireEvent(CacheableEvent $event)
38
    {
39
        if (method_exists($this->events, 'fire')) {
40
            return $this->events->fire($event);
41
        }
42
43
        return $this->events->dispatch($event);
44
    }
45
46
    /**
47
     * Tell the cacheable service to flush all cache
48
     * that related to the given model.
49
     *
50
     * @param \Illuminate\Database\Eloquent\Model $model
0 ignored issues
show
Documentation introduced by
Should the type for parameter $model not be CacheableModel?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
51
     *
52
     * @return void
53
     */
54
    protected function flushCache(CacheableModel $model)
55
    {
56
        $tags = $model->cacheTags();
57
58
        $this->fireEvent(new CacheableInvalidating($model, $tags));
59
60
        \Cacheable::flush($tags);
61
62
        $this->fireEvent(new CacheableInvalidated($model, $tags));
63
    }
64
65
    /**
66
     * Saved event handler.
67
     *
68
     * @param \Illuminate\Database\Eloquent\Model $model
69
     *
70
     * @return void
71
     */
72
    public function saved(Model $model)
73
    {
74
        $this->flushCache($model);
0 ignored issues
show
Documentation introduced by
$model is of type object<Illuminate\Database\Eloquent\Model>, but the function expects a object<Suitmedia\Cacheab...ntracts\CacheableModel>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
    }
76
77
    /**
78
     * Deleted event handler.
79
     *
80
     * @param \Illuminate\Database\Eloquent\Model $model
81
     *
82
     * @return void
83
     */
84
    public function deleted(Model $model)
85
    {
86
        $this->flushCache($model);
0 ignored issues
show
Documentation introduced by
$model is of type object<Illuminate\Database\Eloquent\Model>, but the function expects a object<Suitmedia\Cacheab...ntracts\CacheableModel>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
    }
88
89
    /**
90
     * Restored event handler.
91
     *
92
     * @param \Illuminate\Database\Eloquent\Model $model
93
     *
94
     * @return void
95
     */
96
    public function restored(Model $model)
97
    {
98
        $this->flushCache($model);
0 ignored issues
show
Documentation introduced by
$model is of type object<Illuminate\Database\Eloquent\Model>, but the function expects a object<Suitmedia\Cacheab...ntracts\CacheableModel>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
    }
100
}
101