Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

UniqueUrlSlugRule::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Urls\ValidationRules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Database\Eloquent\Model;
7
use Thinktomorrow\Chief\Urls\UrlRecord;
8
9
class UniqueUrlSlugRule implements Rule
10
{
11
    /** @var Model */
12
    private $ignoredModel;
13
14
    private $failedDetails = [];
15
16 73
    public function __construct(Model $ignoredModel = null)
17
    {
18 73
        $this->ignoredModel = $ignoredModel;
19 73
    }
20
21
    /**
22
     * Determine if the validation rule passes.
23
     *
24
     * @param  string  $attribute
25
     * @param  bool $slugs
26
     * @return bool
27
     */
28 66
    public function passes($attribute, $slugs)
29
    {
30 66
        foreach ($slugs as $locale => $slug) {
0 ignored issues
show
Bug introduced by
The expression $slugs of type boolean is not traversable.
Loading history...
31 66
            if (UrlRecord::existsIgnoringRedirects($slug, $locale, $this->ignoredModel)) {
32 3
                session()->flash('unique_url_slug_validation', [
33 3
                    'locale'       => $locale,
34 3
                    'slug'         => $slug,
35 3
                    'ignoredModel' => $this->ignoredModel,
36
                ]);
37
38 3
                $this->failedDetails['slug'] = $slug;
39 3
                $this->failedDetails['locale'] = $locale;
40
41 66
                return false;
42
            }
43
        }
44
45 66
        return true;
46
    }
47
48
    /**
49
     * Get the validation error message.
50
     *
51
     * @return string
52
     */
53 3
    public function message()
54
    {
55 3
        return 'De \''.$this->failedDetails['slug'].'\' link wordt in het '.$this->failedDetails['locale'].' al door een andere pagina gebruikt.';
56
    }
57
}
58