Completed
Push — master ( aaf480...024f5c )
by Sébastien
01:48
created

ServiceProvider::registerEventDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Sebdesign\SM;
4
5
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
6
use Sebdesign\SM\Callback\ContainerAwareCallback;
7
use Sebdesign\SM\Callback\ContainerAwareCallbackFactory;
8
use Sebdesign\SM\Commands\Debug;
9
use Sebdesign\SM\Event\Dispatcher;
10
use SM\Callback\CallbackFactoryInterface;
11
use SM\Callback\CascadeTransitionCallback;
12
use SM\Factory\Factory;
13
use SM\Factory\FactoryInterface;
14
use Symfony\Component\EventDispatcher\EventDispatcher;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17
class ServiceProvider extends BaseServiceProvider
18
{
19
    /**
20
     * Indicates if loading of the provider is deferred.
21
     *
22
     * @var bool
23
     */
24
    protected $defer = false;
25
26
    /**
27
     * Bootstrap the application services.
28
     */
29
    public function boot()
30
    {
31
        if ($this->app->runningInConsole()) {
32
            $this->publishes([
33
               __DIR__.'/../config/state-machine.php' => config_path('state-machine.php'),
34
           ], 'config');
35
        }
36
    }
37
38
    /**
39
     * Register the application services.
40
     */
41
    public function register()
42
    {
43
        $this->mergeConfigFrom(__DIR__.'/../config/state-machine.php', 'state-machine');
44
45
        $this->registerCallbackFactory();
46
        $this->registerEventDispatcher();
47
        $this->registerFactory();
48
        $this->registerCascadeTransitionCallback();
49
        $this->registerCommands();
50
    }
51
52
    protected function registerCallbackFactory()
53
    {
54
        $this->app->bind('sm.callback.factory', function () {
55
            return new ContainerAwareCallbackFactory(ContainerAwareCallback::class, $this->app);
56
        });
57
58
        $this->app->alias('sm.callback.factory', CallbackFactoryInterface::class);
59
    }
60
61
    protected function registerEventDispatcher()
62
    {
63
        $this->app->bind('sm.event.dispatcher', function () {
64
            return new Dispatcher($this->app['events']);
65
        });
66
67
        $this->app->alias('sm.event.dispatcher', EventDispatcherInterface::class);
68
    }
69
70
    protected function registerFactory()
71
    {
72
        $this->app->singleton('sm.factory', function () {
73
            return new Factory(
74
                $this->app['config']['state-machine'],
75
                $this->app->make('sm.event.dispatcher'),
76
                $this->app->make('sm.callback.factory')
77
            );
78
        });
79
80
        $this->app->alias('sm.factory', FactoryInterface::class);
81
    }
82
83
    protected function registerCascadeTransitionCallback()
84
    {
85
        $this->app->bind(CascadeTransitionCallback::class, function () {
86
            return new CascadeTransitionCallback($this->app->make('sm.factory'));
87
        });
88
    }
89
90
    protected function registerCommands()
91
    {
92
        $this->app->bind(Debug::class, function () {
93
            return new Debug($this->app['config']['state-machine']);
94
        });
95
96
        $this->commands([
97
            Debug::class,
98
        ]);
99
    }
100
101
    /**
102
     * Get the services provided by the provider.
103
     *
104
     * @return array
105
     */
106
    public function provides()
107
    {
108
        return [
109
            'sm.callback.factory',
110
            'sm.event.dispatcher',
111
            'sm.factory',
112
            Debug::class,
113
        ];
114
    }
115
}
116