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

UniqueSlug::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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