Completed
Push — master ( 0084dc...ca6961 )
by Freek
01:49
created

EventProjectorServiceProvider::boot()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 20
rs 9.2
cc 4
eloc 13
nc 8
nop 0
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Illuminate\Support\Facades\Event;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\ServiceProvider;
8
use Spatie\EventProjector\Snapshots\SnapshotFactory;
9
use Spatie\EventProjector\Console\ReplayEventsCommand;
10
use Spatie\EventProjector\Snapshots\SnapshotRepository;
11
use Spatie\EventProjector\Console\ListProjectorsCommand;
12
use Spatie\EventProjector\Console\ResetProjectorCommand;
13
use Spatie\EventProjector\Console\Make\MakeReactorCommand;
14
use Spatie\EventProjector\EventSerializers\EventSerializer;
15
use Spatie\EventProjector\Console\Make\MakeProjectorCommand;
16
use Spatie\EventProjector\Console\Make\MakeStorableEventCommand;
17
use Spatie\EventProjector\Console\Snapshots\ListSnapshotsCommand;
18
use Spatie\EventProjector\Console\Snapshots\CreateSnapshotCommand;
19
use Spatie\EventProjector\Console\Snapshots\DeleteSnapshotCommand;
20
use Spatie\EventProjector\Console\Snapshots\RestoreSnapshotCommand;
21
22
class EventProjectorServiceProvider extends ServiceProvider
23
{
24
    public function boot()
25
    {
26
        if ($this->app->runningInConsole()) {
27
            $this->publishes([
28
                __DIR__.'/../config/event-projector.php' => config_path('event-projector.php'),
29
            ], 'config');
30
        }
31
32
        if (! class_exists('CreateStoredEventsTable')) {
33
            $this->publishes([
34
                __DIR__.'/../stubs/create_stored_events_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_stored_events_table.php'),
35
            ], 'migrations');
36
        }
37
38
        if (! class_exists('CreateProjectorStatusesTable')) {
39
            $this->publishes([
40
                __DIR__.'/../stubs/create_projector_statuses_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_projector_statuses_table.php'),
41
            ], 'migrations');
42
        }
43
    }
44
45
    public function register()
46
    {
47
        $this->mergeConfigFrom(__DIR__.'/../config/event-projector.php', 'event-projector');
48
49
        $this->app->singleton(EventProjectionist::class, function () {
50
            $config = config('event-projector');
51
52
            $projectionist = new EventProjectionist($config);
53
54
            $projectionist
55
                ->addProjectors($config['projectors'] ?? [])
56
                ->addReactors($config['reactors'] ?? []);
57
58
            return $projectionist;
59
        });
60
61
        $this->app->alias(EventProjectionist::class, 'event-projector');
62
63 View Code Duplication
        $this->app->bind(SnapshotFactory::class, function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
            $eventProjectionist = app(EventProjectionist::class);
65
66
            $config = config('event-projector');
67
68
            $diskName = $config['snapshots_disk'];
69
            $disk = Storage::disk($diskName);
70
71
            return new SnapshotFactory($eventProjectionist, $disk, $config);
72
        });
73
74 View Code Duplication
        $this->app->bind(SnapshotRepository::class, function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
            $eventProjectionist = app(EventProjectionist::class);
76
77
            $config = config('event-projector');
78
79
            $diskName = $config['snapshots_disk'];
80
            $disk = Storage::disk($diskName);
81
82
            return new SnapshotRepository($eventProjectionist, $disk, $config);
83
        });
84
85
        $this->app->bind(EventSubscriber::class, function () {
86
            $eventProjectionist = app(EventProjectionist::class);
87
            $config = config('event-projector');
88
89
            return new EventSubscriber($eventProjectionist, $config);
90
        });
91
92
        $this->app
93
            ->when(ReplayEventsCommand::class)
94
            ->needs('$storedEventModelClass')
95
            ->give(config('event-projector.stored_event_model'));
96
97
        Event::subscribe(EventSubscriber::class);
98
99
        $this->app->bind(EventSerializer::class, config('event-projector.event_serializer'));
100
101
        $this->bindCommands();
102
    }
103
104
    protected function bindCommands()
105
    {
106
        $this->app->bind('command.event-projector:list-projectors', ListProjectorsCommand::class);
107
        $this->app->bind('command.event-projector:reset-projector', ResetProjectorCommand::class);
108
        $this->app->bind('command.event-projector:replay-events', ReplayEventsCommand::class);
109
110
        $this->app->bind('command.event-projector:list-snapshots', ListSnapshotsCommand::class);
111
        $this->app->bind('command.event-projector:create-snapshot', CreateSnapshotCommand::class);
112
        $this->app->bind('command.event-projector:restore-snapshot', RestoreSnapshotCommand::class);
113
        $this->app->bind('command.event-projector:delete-snapshot', DeleteSnapshotCommand::class);
114
115
        $this->app->bind('command.make:projector', MakeProjectorCommand::class);
116
        $this->app->bind('command.make:reactor', MakeReactorCommand::class);
117
        $this->app->bind('command.make:storable-event', MakeStorableEventCommand::class);
118
119
        $this->commands([
120
            'command.event-projector:list-projectors',
121
            'command.event-projector:reset-projector',
122
            'command.event-projector:replay-events',
123
124
            'command.event-projector:list-snapshots',
125
            'command.event-projector:create-snapshot',
126
            'command.event-projector:restore-snapshot',
127
            'command.event-projector:delete-snapshot',
128
129
            'command.make:projector',
130
            'command.make:reactor',
131
            'command.make:storable-event',
132
        ]);
133
    }
134
}
135