Completed
Push — master ( 73f026...dc38fa )
by Sebastian
04:16
created

ValidationServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 3
c 3
b 1
f 1
lcom 0
cbo 3
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
B boot() 0 26 2
1
<?php
2
3
namespace App\Providers;
4
5
use App\Models\Enums\TagType;
6
use Illuminate\Support\ServiceProvider;
7
use Validator;
8
9
class ValidationServiceProvider extends ServiceProvider
10
{
11
    public function register()
12
    {
13
    }
14
15
    public function boot()
16
    {
17
        Validator::extend('tags_exist', function ($attribute, $tagNames, $parameters) {
18
            list($tagType) = $parameters;
19
            $tagType = new TagType($tagType);
20
21
            $exisitingTagNames = app(\App\Repositories\TagRepository::class)
22
                ->getAllWithType($tagType)
23
                ->lists('name')
24
                ->toArray();
25
26
            if (!\Spatie\values_in_array($tagNames, $exisitingTagNames)) {
27
                return false;
28
            }
29
30
            return true;
31
        });
32
33
        Validator::extend('enum', function ($attribute, $value, $parameters) {
34
35
            /** @var \App\Foundation\Models\Enums\Enum $class */
36
            $class = $parameters[0];
37
38
            return $class::isValid($value);
39
        });
40
    }
41
}
42