Completed
Pull Request — master (#260)
by Colin
06:42
created

SluggableObserver::fireSluggingEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php namespace Cviebrock\EloquentSluggable;
2
3
use Cviebrock\EloquentSluggable\Services\SlugService;
4
use Illuminate\Contracts\Events\Dispatcher;
5
use Illuminate\Database\Eloquent\Model;
6
7
8
class SluggableObserver
9
{
10
11
    /**
12
     * @var \Cviebrock\EloquentSluggable\Services\SlugService
13
     */
14
    private $slugService;
15
16
    /**
17
     * @var \Illuminate\Contracts\Events\Dispatcher
18
     */
19
    private $events;
20
21
    /**
22
     * SluggableObserver constructor.
23
     *
24
     * @param \Cviebrock\EloquentSluggable\Services\SlugService $slugService
25
     * @param \Illuminate\Contracts\Events\Dispatcher $events
26
     */
27
    public function __construct(SlugService $slugService, Dispatcher $events)
28
    {
29
        $this->slugService = $slugService;
30
        $this->events = $events;
31
    }
32
33
    /**
34
     * @param \Illuminate\Database\Eloquent\Model $model
35
     * @return boolean|null
36
     */
37
    public function saving(Model $model)
38
    {
39
        return $this->generateSlug($model, 'saving');
40
    }
41
42
    /**
43
     * @param \Illuminate\Database\Eloquent\Model $model
44
     * @param string $event
45
     * @return boolean|null
46
     */
47
    protected function generateSlug(Model $model, $event)
48
    {
49
        // If the "slugging" event returns a value, abort
50
        if ($this->fireSluggingEvent($model, $event) !== null) {
51
            return;
52
        }
53
        $wasSlugged = $this->slugService->slug($model);
54
55
        $this->fireSluggedEvent($model, $wasSlugged);
0 ignored issues
show
Documentation introduced by
$wasSlugged is of type boolean, but the function expects a string.

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...
56
    }
57
58
    /**
59
     * Fire the namespaced validating event.
60
     *
61
     * @param  \Illuminate\Database\Eloquent\Model $model
62
     * @param  string $event
63
     * @return mixed
64
     */
65
    protected function fireSluggingEvent(Model $model, $event)
66
    {
67
        return $this->events->until('eloquent.slugging: ' . get_class($model), [$model, $event]);
68
    }
69
70
    /**
71
     * Fire the namespaced post-validation event.
72
     *
73
     * @param  \Illuminate\Database\Eloquent\Model $model
74
     * @param  string $status
75
     * @return void
76
     */
77
    protected function fireSluggedEvent(Model $model, $status)
78
    {
79
        $this->events->fire('eloquent.slugged: ' . get_class($model), [$model, $status]);
80
    }
81
}
82