Completed
Push — master ( 21155b...20a786 )
by Freek
03:37
created

EventProjectorServiceProvider::boot()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 0
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Illuminate\Support\Facades\Event;
6
use Illuminate\Support\ServiceProvider;
7
use Spatie\EventProjector\Console\MakeProjectorCommand;
8
use Spatie\EventProjector\Console\MakeReactorCommand;
9
use Spatie\EventProjector\Console\ReplayEventsCommand;
10
11
class EventProjectorServiceProvider extends ServiceProvider
12
{
13
    public function boot()
14
    {
15
        if ($this->app->runningInConsole()) {
16
            $this->publishes([
17
                __DIR__.'/../config/event-projector.php' => config_path('event-projector.php'),
18
            ], 'config');
19
        }
20
21
        if (! class_exists('CreateStoredEventsTable')) {
22
            $this->publishes([
23
                __DIR__ . '/../stubs/create_stored_events_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_stored_events_table.php'),
24
            ], 'migrations');
25
        }
26
27
        $this->commands([
28
            ReplayEventsCommand::class,
29
            MakeProjectorCommand::class,
30
            MakeReactorCommand::class,
31
        ]);
32
    }
33
34
    public function register()
35
    {
36
        $this->mergeConfigFrom(__DIR__.'/../config/event-projector.php', 'event-projector');
37
38
        $this->app
39
            ->when(EventSubscriber::class)
40
            ->needs('$storedEventModelClass')
41
            ->give(config('event-projector.stored_event_model'));
42
43
        $this->app
44
            ->when(ReplayEventsCommand::class)
45
            ->needs('$storedEventModelClass')
46
            ->give(config('event-projector.stored_event_model'));
47
48
        Event::subscribe(EventSubscriber::class);
49
50
        $this->app->singleton(EventProjectionist::class, function () {
51
            return new EventProjectionist();
52
        });
53
54
        $this->app->alias(EventProjectionist::class, 'event-projector');
55
    }
56
}
57