LaravelValidateServiceProvider::boot()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 3
b 0
f 0
nc 12
nop 0
dl 0
loc 25
rs 9.4888
1
<?php
2
3
namespace Milwad\LaravelValidate;
4
5
use Illuminate\Support\Facades\File;
6
use Illuminate\Support\Facades\Validator;
7
use Illuminate\Support\ServiceProvider;
8
use Illuminate\Support\Str;
9
use Milwad\LaravelValidate\Utils\CountryLandlineCallback;
10
use Milwad\LaravelValidate\Utils\CountryPhoneCallback;
11
12
class LaravelValidateServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Register files.
16
     */
17
    public function register(): void
18
    {
19
        if ($this->app->runningInConsole()) {
20
            $this->publishLangFiles();
21
            $this->publishConfigFile();
22
        }
23
24
        $this->loadTranslationsFrom(__DIR__.'/lang', 'validation');
25
        $this->mergeConfigFrom(__DIR__.'/../config/laravel-validate.php', 'laravel-validate');
26
    }
27
28
    /**
29
     * Publish lang files.
30
     */
31
    protected function publishLangFiles(): void
32
    {
33
        $langs = File::directories(__DIR__.'/lang');
34
35
        foreach ($langs as $lang) {
36
            $lang = Str::after($lang, 'lang');
37
            $lang = Str::replace('\\', '', $lang);
38
            $lang = Str::replace('/', '', $lang);
39
40
            $this->publishes([
41
                __DIR__."/lang/$lang" => lang_path($lang),
42
            ], "validate-lang-$lang");
43
        }
44
    }
45
46
    /**
47
     * Publish config file.
48
     */
49
    protected function publishConfigFile(): void
50
    {
51
        $this->publishes([
52
            __DIR__.'/../config/laravel-validate.php' => config_path('laravel-validate.php'),
53
        ], 'laravel-validate-config');
54
    }
55
56
    /**
57
     * Boot applications.
58
     *
59
     * @throws \Throwable
60
     */
61
    public function boot(): void
62
    {
63
        $countries = config('laravel-validate.phone-country', []);
64
65
        foreach ($countries as $code => $country) {
66
            CountryPhoneCallback::addValidator($code, $country);
67
        }
68
69
        $landlineCountries = config('laravel-validate.landline-country', []);
70
71
        foreach ($landlineCountries as $code => $country) {
72
            CountryLandlineCallback::addValidator($code, $country);
73
        }
74
75
        // Register rules in container
76
        if (config('laravel-validate.using_container', false)) {
77
            $rules = File::files(__DIR__.'/Rules');
78
79
            foreach ($rules as $rule) {
80
                $className = 'Milwad\\LaravelValidate\\Rules\\'.$rule->getBasename('.php');
81
82
                Validator::extend(
83
                    $rule->getFilenameWithoutExtension(),
84
                    function ($attribute, $value, $parameters, $validator) use ($className) {
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

84
                    function ($attribute, $value, $parameters, /** @scrutinizer ignore-unused */ $validator) use ($className) {

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...
85
                        return (new $className($parameters))->passes($attribute, $value);
86
                    }
87
                );
88
            }
89
        }
90
    }
91
}
92