Completed
Push — master ( 60f133...b1ddad )
by Colin
08:08 queued 05:16
created

SluggableObserver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 6
c 4
b 1
f 0
lcom 1
cbo 2
dl 0
loc 74
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A saving() 0 4 1
A generateSlug() 0 10 2
A fireSluggingEvent() 0 4 1
A fireSluggedEvent() 0 4 1
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
class SluggableObserver
8
{
9
10
    /**
11
     * @var \Cviebrock\EloquentSluggable\Services\SlugService
12
     */
13
    private $slugService;
14
15
    /**
16
     * @var \Illuminate\Contracts\Events\Dispatcher
17
     */
18
    private $events;
19
20
    /**
21
     * SluggableObserver constructor.
22
     *
23
     * @param \Cviebrock\EloquentSluggable\Services\SlugService $slugService
24
     * @param \Illuminate\Contracts\Events\Dispatcher $events
25
     */
26
    public function __construct(SlugService $slugService, Dispatcher $events)
27
    {
28
        $this->slugService = $slugService;
29
        $this->events = $events;
30
    }
31
32
    /**
33
     * @param \Illuminate\Database\Eloquent\Model $model
34
     * @return boolean|null
35
     */
36
    public function saving(Model $model)
37
    {
38
        return $this->generateSlug($model, 'saving');
39
    }
40
41
    /**
42
     * @param \Illuminate\Database\Eloquent\Model $model
43
     * @param string $event
44
     * @return boolean|null
45
     */
46
    protected function generateSlug(Model $model, $event)
47
    {
48
        // If the "slugging" event returns a value, abort
49
        if ($this->fireSluggingEvent($model, $event) !== null) {
50
            return;
51
        }
52
        $wasSlugged = $this->slugService->slug($model);
53
54
        $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...
55
    }
56
57
    /**
58
     * Fire the namespaced validating event.
59
     *
60
     * @param  \Illuminate\Database\Eloquent\Model $model
61
     * @param  string $event
62
     * @return mixed
63
     */
64
    protected function fireSluggingEvent(Model $model, $event)
65
    {
66
        return $this->events->until('eloquent.slugging: ' . get_class($model), [$model, $event]);
67
    }
68
69
    /**
70
     * Fire the namespaced post-validation event.
71
     *
72
     * @param  \Illuminate\Database\Eloquent\Model $model
73
     * @param  string $status
74
     * @return void
75
     */
76
    protected function fireSluggedEvent(Model $model, $status)
77
    {
78
        $this->events->fire('eloquent.slugged: ' . get_class($model), [$model, $status]);
79
    }
80
}
81