ClamavValidatorServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 19
cts 19
cp 1
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Sunspikes\ClamavValidator;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Support\Str;
7
8
class ClamavValidatorServiceProvider extends ServiceProvider
9
{
10
11
    /**
12
     * Indicates if loading of the provider is deferred.
13
     *
14
     * @var bool
15
     */
16
    protected $defer = false;
17
18
    /**
19
     * The list of validator rules.
20
     *
21
     * @var array
22
     */
23
    protected $rules = [
24
        'clamav',
25
    ];
26
27
    /**
28
     * Bootstrap the application events.
29
     *
30
     * @return void
31
     */
32 1
    public function boot()
33
    {
34 1
        $this->loadTranslationsFrom(__DIR__ . '/../lang', 'clamav-validator');
35
36 1
        $this->publishes([
37 1
            __DIR__ . '/../../config/clamav.php' => $this->app->configPath('clamav.php'),
38 1
        ], 'config');
39 1
        $this->publishes([
40 1
        __DIR__.'/../lang' => $this->app->resourcePath('lang/vendor/clamav-validator'),
41 1
        ], 'lang');
42 1
        $this->app['validator']
43 1
            ->resolver(function ($translator, $data, $rules, $messages, $customAttributes = []) {
44 1
                return new ClamavValidator(
45 1
                    $translator,
46 1
                    $data,
47 1
                    $rules,
48 1
                    $messages,
49
                    $customAttributes
50 1
                );
51 1
            });
52
53 1
        $this->addNewRules();
54 1
    }
55
56
    /**
57
     * Get the list of new rules being added to the validator.
58
     *
59
     * @return array
60
     */
61 1
    public function getRules()
62
    {
63 1
        return $this->rules;
64
    }
65
66
67
    /**
68
     * Add new rules to the validator.
69
     */
70 1
    protected function addNewRules()
71
    {
72 1
        foreach ($this->getRules() as $rule) {
73 1
            $this->extendValidator($rule);
74 1
        }
75 1
    }
76
77
78
    /**
79
     * Extend the validator with new rules.
80
     *
81
     * @param  string $rule
82
     * @return void
83
     */
84 1
    protected function extendValidator($rule)
85
    {
86 1
        $method = Str::studly($rule);
87 1
        $translation = $this->app['translator']->get('clamav-validator::validation');
88 1
        $this->app['validator']->extend(
89 1
            $rule,
90 1
            ClamavValidator::class .'@validate' . $method,
91 1
            isset($translation[$rule]) ? $translation[$rule] : []
92 1
        );
93 1
    }
94
95
96
    /**
97
     * Register the service provider.
98
     *
99
     * @return void
100
     */
101
    public function register()
102
    {
103
        $this->mergeConfigFrom(__DIR__ . '/../../config/clamav.php', 'clamav');
104
    }
105
106
107
    /**
108
     * Get the services provided by the provider.
109
     *
110
     * @return array
111
     */
112
    public function provides()
113
    {
114
        return [];
115
    }
116
}
117