Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

UniqueSlug   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
eloc 23
dl 0
loc 69
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A slugResolver() 0 5 1
A make() 0 3 1
A get() 0 14 2
A __construct() 0 7 1
A isSlugUnique() 0 9 6
A sluggify() 0 3 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Concerns\Sluggable;
4
5
class UniqueSlug
6
{
7
    /** @var SluggableContract */
8
    private $model;
9
10
    /** @var array */
11
    private $blacklist;
12
13
    /** @var \Closure */
14
    private $slugResolver;
15
16
    public function __construct(SluggableContract $model, array $blacklist = [])
17
    {
18
        $this->model = $model;
19
        $this->blacklist = $blacklist;
20
21
        $this->slugResolver = function ($slug) {
22
            return str_slug($slug);
0 ignored issues
show
Deprecated Code introduced by
The function str_slug() has been deprecated: Str::slug() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

22
            return /** @scrutinizer ignore-deprecated */ str_slug($slug);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
23
        };
24
    }
25
26
    public static function make($model, array $blacklist = [])
27
    {
28
        return new static($model, $blacklist);
29
    }
30
31
    public function slugResolver(\Closure $resolver)
32
    {
33
        $this->slugResolver = $resolver;
34
35
        return $this;
36
    }
37
38
    public function get($title, SluggableContract $entity = null)
39
    {
40
        $slug = $originalslug = $this->sluggify($title);
41
        $i = 1;
42
43
        while (!$this->isSlugUnique($slug, $entity)) {
44
            $slug = $originalslug.'-'.$i;
45
            $i++;
46
        }
47
48
        // Add to blacklist
49
        $this->blacklist[] = $slug;
50
51
        return $slug;
52
    }
53
54
    private function sluggify($value)
55
    {
56
        return call_user_func($this->slugResolver, $value);
57
    }
58
59
    /**
60
     *
61
     * @param $slug
62
     * @param SluggableContract $entity
63
     * @return bool
64
     */
65
    private function isSlugUnique($slug, SluggableContract $entity = null)
66
    {
67
        $model = $this->model->findBySlug($slug);
68
69
        if (!in_array($slug, $this->blacklist) && (!$model || ($entity && $entity->id && $model->id == $entity->id))) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Thinktomorrow\Chief\Conc...gable\SluggableContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
70
            return true;
71
        }
72
73
        return false;
74
    }
75
}
76