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

PhoneServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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