PhoneServiceProvider::canUseDependentValidation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
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 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