Completed
Push — master ( 49c7d6...332f63 )
by Krishnaprasad
04:55
created

ClamavValidatorServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.5%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 109
ccs 37
cts 40
cp 0.925
rs 10
c 0
b 0
f 0

6 Methods

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