PhoneServiceProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 19
cts 21
cp 0.9048
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 159
    public function boot()
19
    {
20 159
        $this->registerValidator();
21
22 159
        $this->registerRule();
23 159
    }
24
25
    /**
26
     * Register the service provider.
27
     *
28
     * @return void
29
     */
30 159
    public function register()
31
    {
32
        $this->app->singleton('libphonenumber', function ($app) {
33
            return PhoneNumberUtil::getInstance();
34 159
        });
35
36 159
        $this->app->alias('libphonenumber', PhoneNumberUtil::class);
37 159
    }
38
39
    /**
40
     * Register the "phone" validator.
41
     */
42 159
    protected function registerValidator()
43
    {
44 159
        $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend';
45
46 159
        $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate');
47 159
    }
48
49
    /**
50
     * Register the "phone" rule macro.
51
     */
52 159
    protected function registerRule()
53
    {
54 159
        if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) {
55
            Rule::macro('phone', function () {
56
                return new Rules\Phone;
57 159
            });
58
        }
59 159
    }
60
61
    /**
62
     * Determine whether we can register a dependent validator.
63
     *
64
     * @return bool
65
     */
66 159
    public static function canUseDependentValidation()
67
    {
68 159
        $validator = new ReflectionClass('\Illuminate\Validation\Factory');
69
70 159
        return $validator->hasMethod('extendDependent');
71
    }
72
}
73