Passed
Push — 1.x ( b6a916...1fcbd7 )
by Milwad
03:01
created

LaravelValidateServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 10
Bugs 2 Features 4
Metric Value
eloc 28
c 10
b 2
f 4
dl 0
loc 74
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 11 2
A publishLangFiles() 0 6 2
A publishConfigFile() 0 5 1
A loadValidations() 0 4 2
1
<?php
2
3
namespace Milwad\LaravelValidate;
4
5
use Illuminate\Support\Facades\Validator;
6
use Illuminate\Support\ServiceProvider;
7
use Milwad\LaravelValidate\Rules\ValidBase64;
8
9
class LaravelValidateServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Languages names.
13
     *
14
     * @var array|string[]
15
     */
16
    protected array $langs = [
17
        'ar',
18
        'az',
19
        'en',
20
        'fa',
21
        'fr',
22
        'hi',
23
        'It',
24
        'ja',
25
        'ko',
26
        'ru',
27
        'zh_CN',
28
    ];
29
30
    /**
31
     * Register files.
32
     *
33
     * @return void
34
     */
35
    public function register()
36
    {
37
        if ($this->app->runningInConsole()) {
38
            $this->publishLangFiles();
39
            $this->publishConfigFile();
40
        }
41
42
        $this->loadValidations();
43
44
        $this->loadTranslationsFrom(__DIR__.'/../lang', 'validation');
45
        $this->mergeConfigFrom(__DIR__.'/../config/laravel-validate.php', 'laravel-validate');
46
    }
47
48
    /**
49
     * Publish lang files.
50
     *
51
     * @return void
52
     */
53
    private function publishLangFiles(): void
54
    {
55
        foreach ($this->langs as $lang) {
56
            $this->publishes([
57
                __DIR__ . "/Lang/$lang" => base_path("lang/$lang"),
58
            ], "validate-lang-$lang");
59
        }
60
    }
61
62
    /**
63
     * Publish config file.
64
     *
65
     * @return void
66
     */
67
    private function publishConfigFile()
68
    {
69
        $this->publishes([
70
            __DIR__ . '/../config/laravel-validate.php' => config_path('laravel-validate.php')
71
        ], 'laravel-validate-config');
72
    }
73
74
    /**
75
     * Load validation in container.
76
     *
77
     * @return void
78
     */
79
    private function loadValidations()
80
    {
81
        foreach (config('laravel-validate.rules', []) as $rule) {
82
            Validator::extend($rule['name'], function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $attribute 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

82
            Validator::extend($rule['name'], function (/** @scrutinizer ignore-unused */ $attribute, $value, $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

82
            Validator::extend($rule['name'], 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 $value 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

82
            Validator::extend($rule['name'], function ($attribute, /** @scrutinizer ignore-unused */ $value, $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 $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

82
            Validator::extend($rule['name'], 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...
83
84
            });
85
        }
86
    }
87
}
88