Passed
Push — laravel-7-support ( 46b12f...75175c )
by Quentin
17:51 queued 12:03
created

ValidationServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 46.15%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 60
ccs 12
cts 26
cp 0.4615
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 2 1
B boot() 0 45 8
1
<?php
2
3
namespace A17\Twill;
4
5
use A17\Twill\Repositories\BlockRepository;
6
use Illuminate\Support\Facades\Validator;
7
use Illuminate\Support\ServiceProvider;
8
use Illuminate\Support\Str;
9
10
class ValidationServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Registers the package additional validation rules.
14
     *
15
     * @return void
16
     */
17 59
    public function boot()
18
    {
19
        Validator::extend('absolute_or_relative_url', function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

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

19
        Validator::extend('absolute_or_relative_url', function ($attribute, $value, $parameters, /** @scrutinizer ignore-unused */ $validator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parameters is not used and could be removed. ( Ignorable by Annotation )

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

19
        Validator::extend('absolute_or_relative_url', function ($attribute, $value, /** @scrutinizer ignore-unused */ $parameters, $validator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20 1
            return Str::startsWith($value, '/') || Validator::make([$attribute => $value], [$attribute => 'url'])->passes();
21 59
        }, 'The :attribute should be a valid url (absolute or relative)');
22
23
        Validator::extend('relative_or_secure_url', function ($attribute, $value, $parameters) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed. ( Ignorable by Annotation )

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

23
        Validator::extend('relative_or_secure_url', function ($attribute, $value, /** @scrutinizer ignore-unused */ $parameters) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24 1
            return Str::startsWith($value, '/') || filter_var($value, FILTER_VALIDATE_URL) !== false && Str::startsWith($value, 'https');
25 59
        }, 'The :attribute should be a valid url (relative or https)');
26
27
        Validator::extend('web_color', function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed. ( Ignorable by Annotation )

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

27
        Validator::extend('web_color', function ($attribute, $value, /** @scrutinizer ignore-unused */ $parameters, $validator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

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

27
        Validator::extend('web_color', function ($attribute, $value, $parameters, /** @scrutinizer ignore-unused */ $validator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28 1
            return preg_match('/^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i', $value);
29 59
        });
30
31
        Validator::extend('phone_number', function ($attribute, $value, $parameters) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed. ( Ignorable by Annotation )

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

31
        Validator::extend('phone_number', function ($attribute, $value, /** @scrutinizer ignore-unused */ $parameters) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32 1
            return preg_match("/^[+]?[0-9\-\ ]*$/", $value);
33 59
        });
34
35
        Validator::extend('validBlocks', function ($attribute, $value, $parameters, $validator) {
36
            $blockMessages = [];
37
38
            foreach ($value as $block) {
39
                $cmsBlock = $this->app->make(BlockRepository::class)->buildFromCmsArray($block, false);
40
41
                $rules = config('twill.block_editor.blocks.' . $cmsBlock['type'] . '.rules') ?? [];
42
43
                unset($cmsBlock['content']);
44
45
                $blockValidator = Validator::make(array_merge($block['content'], $cmsBlock), $rules);
46
47
                if (!$blockValidator->passes()) {
48
                    foreach ($blockValidator->errors()->all() as $error) {
49
                        $blockMessages[] = $error;
50
                    }
51
                }
52
53
                if (!empty($blockMessages ?? [])) {
54
                    array_unshift($blockMessages, 'This block has validation issues:');
55
                    $validator->errors()->add('block.' . $block['id'], join('<br>', $blockMessages));
56
                }
57
58
                $blockMessages = [];
59
            }
60
61
            return true;
62 59
        });
63 59
    }
64
65
    /**
66
     * @return void
67
     */
68 59
    public function register()
69
    {
70
71 59
    }
72
}
73