Completed
Push — master ( 835da2...f8eb08 )
by Krishnaprasad
05:09
created

ClamavValidatorServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
crap 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 bool
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->app['validator']
34 1
            ->resolver(function ($translator, $data, $rules, $messages) {
35 1
                return new ClamavValidator(
36 1
                    $translator,
37 1
                    $data,
38 1
                    $rules,
39
                    $messages
40 1
                );
41 1
            });
42
43 1
        $this->addNewRules();
44 1
    }
45
46
    /**
47
     * Get the list of new rules being added to the validator.
48
     *
49
     * @return array
50
     */
51 1
    public function getRules()
52
    {
53 1
        return $this->rules;
54
    }
55
56
57
    /**
58
     * Add new rules to the validator.
59
     */
60 1
    protected function addNewRules()
61
    {
62 1
        foreach ($this->getRules() as $rule) {
0 ignored issues
show
Bug introduced by
The expression $this->getRules() of type boolean is not traversable.
Loading history...
63 1
            $this->extendValidator($rule);
64 1
        }
65 1
    }
66
67
68
    /**
69
     * Extend the validator with new rules.
70
     *
71
     * @param  string $rule
72
     * @return void
73
     */
74 1
    protected function extendValidator($rule)
75
    {
76 1
        $method = studly_case($rule);
77 1
        $translation = $this->app['translator']->get('clamav-validator::validation');
78 1
        $this->app['validator']->extend($rule, ClamavValidator::class .'@validate' . $method,
79 1
            isset($translation[$rule]) ? $translation[$rule] : []);
80 1
    }
81
82
83
    /**
84
     * Register the service provider.
85
     *
86
     * @return void
87
     */
88
    public function register()
89
    {
90
    }
91
92
93
    /**
94
     * Get the services provided by the provider.
95
     *
96
     * @return array
97
     */
98 1
    public function provides()
99
    {
100 1
        return [];
101
    }
102
}
103