Completed
Pull Request — master (#10)
by ARCANEDEV
06:55
created

EventServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php namespace Arcanedev\LaravelAuth\Providers;
2
3
use Arcanedev\Support\Providers\EventServiceProvider as ServiceProvider;
4
5
/**
6
 * Class     EventServiceProvider
7
 *
8
 * @package  Arcanedev\LaravelAuth\Providers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class EventServiceProvider extends ServiceProvider
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Main Functions
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function register()
21
    {
22
        parent::register();
23 528
24
        $this->app->booted(function () {
25 528
            $this->registerObservers();
26
        });
27
    }
28 528
29 396
    /**
30 396
     * Register the application's event listeners.
31 396
     */
32 396
    protected function registerObservers()
33
    {
34 528
        $observers = [
35 528
            'users'              => \Arcanesoft\Contracts\Auth\Models\User::class,
36 396
            'roles'              => \Arcanesoft\Contracts\Auth\Models\Role::class,
37 528
            'permissions-groups' => \Arcanesoft\Contracts\Auth\Models\PermissionsGroup::class,
38
            'permissions'        => \Arcanesoft\Contracts\Auth\Models\Permission::class,
39
        ];
40
41
        foreach ($observers as $name => $abstract) {
42
            $this->observe($name, $abstract);
43
        }
44
    }
45
46
    /* ------------------------------------------------------------------------------------------------
47
     |  Other Functions
48
     | ------------------------------------------------------------------------------------------------
49 528
     */
50
    /**
51 528
     * Observe the model.
52 528
     *
53 396
     * @param  string  $name
54 528
     * @param  string  $abstract
55
     */
56
    private function observe($name, $abstract)
57
    {
58
        $this->getModel($abstract)->observe(
59
            $this->getObserver($name)
60
        );
61
    }
62
63 528
    /**
64
     * Get the concrete model.
65 528
     *
66
     * @param  string  $abstract
67
     *
68
     * @return \Illuminate\Database\Eloquent\Model
69
     */
70
    private function getModel($abstract)
71
    {
72
        return $this->app->make($abstract);
73
    }
74
75 528
    /**
76
     * Get the observer class.
77
     *
78 528
     * @param  string  $key
79
     *
80 528
     * @return string
81
     */
82
    private function getObserver($key)
83
    {
84
        /** @var  \Illuminate\Contracts\Config\Repository  $config */
85
        $config = $this->app['config'];
86
87
        return $config->get("laravel-auth.{$key}.observer");
88
    }
89
}
90