Completed
Push — master ( 3056cf...7fcb56 )
by Krishnaprasad
04:26
created

ClamavValidatorServiceProvider::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

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