Completed
Push — master ( d81690...79b790 )
by Propa
02:58
created

PhoneServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 19
cts 23
cp 0.8261
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A registerValidator() 0 6 2
A canUseDependentValidation() 0 6 1
A register() 0 8 1
A registerRule() 0 8 3
1
<?php namespace Propaganistas\LaravelPhone;
2
3
use Illuminate\Support\ServiceProvider;
4
use Illuminate\Support\Traits\Macroable;
5
use Illuminate\Validation\Rule;
6
use libphonenumber\PhoneNumberUtil;
7
use Propaganistas\LaravelPhone\Rules;
8
use Propaganistas\LaravelPhone\Validation;
9
use ReflectionClass;
10
11
class PhoneServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * Bootstrap the application events.
15
     *
16
     * @return void
17
     */
18 135
    public function boot()
19
    {
20 135
        $this->registerValidator();
21
22 135
        $this->registerRule();
23 135
    }
24
25
    /**
26
     * Register the service provider.
27
     *
28
     * @return void
29
     */
30 45
    public function register()
31
    {
32 90
        $this->app->singleton('libphonenumber', function ($app) {
33
            return PhoneNumberUtil::getInstance();
34 135
        });
35
36 135
        $this->app->alias('libphonenumber', PhoneNumberUtil::class);
37 135
    }
38
39
    /**
40
     * Register the "phone" validator.
41
     */
42 135
    protected function registerValidator()
43
    {
44 135
        $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend';
45
46 135
        $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate');
47 135
    }
48
49
    /**
50
     * Register the "phone" rule macro.
51
     */
52 135
    protected function registerRule()
53
    {
54 135
        if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) {
55
            Rule::macro('phone', function () {
56
                return new Rules\Phone;
57
            });
58
        }
59 135
    }
60
61
    /**
62
     * Determine whether we can register a dependent validator.
63
     *
64
     * @return bool
65
     */
66 135
    public static function canUseDependentValidation()
67
    {
68 135
        $validator = new ReflectionClass('\Illuminate\Validation\Factory');
69
70 135
        return $validator->hasMethod('extendDependent');
71
    }
72
}
73