Completed
Push — master ( 0eca36...18d381 )
by Freek
01:35
created

EventProjectorServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 12.35 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 2 Features 1
Metric Value
wmc 6
c 5
b 2
f 1
lcom 1
cbo 5
dl 10
loc 81
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B boot() 10 24 4
B register() 0 24 1
B bindCommands() 0 28 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Illuminate\Support\Facades\Event;
6
use Illuminate\Support\ServiceProvider;
7
use Spatie\EventProjector\Console\Snapshots\CreateSnapshotCommand;
8
use Spatie\EventProjector\Console\ListProjectorsCommand;
9
use Spatie\EventProjector\Console\Make\MakeReactorCommand;
10
use Spatie\EventProjector\Console\ReplayEventsCommand;
11
use Spatie\EventProjector\Console\Make\MakeProjectorCommand;
12
use Spatie\EventProjector\Console\Make\MakeStorableEventCommand;
13
use Spatie\EventProjector\Console\Snapshots\CreateSnapshotsCommand;
14
use Spatie\EventProjector\Console\Snapshots\DeleteSnapshotsCommand;
15
use Spatie\EventProjector\Console\Snapshots\ListSnapshotsCommand;
16
use Spatie\EventProjector\Console\Snapshots\LoadSnapshotsCommand;
17
use Spatie\EventProjector\EventSerializers\EventSerializer;
18
19
class EventProjectorServiceProvider extends ServiceProvider
20
{
21
    public function boot()
22
    {
23
        if ($this->app->runningInConsole()) {
24
            $this->publishes([
25
                __DIR__.'/../config/event-projector.php' => config_path('event-projector.php'),
26
            ], 'config');
27
        }
28
29 View Code Duplication
        if (! class_exists('CreateStoredEventsTable')) {
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...
30
            $this->publishes([
31
                __DIR__.'/../stubs/create_stored_events_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_stored_events_table.php'),
32
            ], 'migrations');
33
        }
34
35 View Code Duplication
        if (! class_exists('CreateProjectorStatusesTable')) {
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...
36
            $this->publishes([
37
                __DIR__.'/../stubs/create_projector_statuses_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_projector_statuses_table.php'),
38
            ], 'migrations');
39
        }
40
41
        $this->app->bind(EventSerializer::class, config('event-projector.event_serializer'));
42
43
        $this->bindCommands();
44
    }
45
46
    public function register()
47
    {
48
        $this->mergeConfigFrom(__DIR__.'/../config/event-projector.php', 'event-projector');
49
50
        $this->app->singleton(EventProjectionist::class, function () {
51
            $config = config('event-projector');
52
53
            return new EventProjectionist($config);
54
        });
55
56
        $this->app->alias(EventProjectionist::class, 'event-projector');
57
58
        $this->app
59
            ->when(EventSubscriber::class)
60
            ->needs('$storedEventModelClass')
61
            ->give(config('event-projector.stored_event_model'));
62
63
        $this->app
64
            ->when(ReplayEventsCommand::class)
65
            ->needs('$storedEventModelClass')
66
            ->give(config('event-projector.stored_event_model'));
67
68
        Event::subscribe(EventSubscriber::class);
69
    }
70
71
    protected function bindCommands(): void
72
    {
73
        $this->app->bind('command.event-projector:list-projectors', ListProjectorsCommand::class);
74
        $this->app->bind('command.event-projector:replay-events', ReplayEventsCommand::class);
75
76
        $this->app->bind('command.event-projector:list-snapshots', ListSnapshotsCommand::class);
77
        $this->app->bind('command.event-projector:create-snapshots', CreateSnapshotsCommand::class);
78
        $this->app->bind('command.event-projector:load-snapshots', LoadSnapshotsCommand::class);
79
        $this->app->bind('command.event-projector:delete-snapshots', DeleteSnapshotsCommand::class);
80
81
        $this->app->bind('command.make:projector', MakeProjectorCommand::class);
82
        $this->app->bind('command.make:reactor', MakeReactorCommand::class);
83
        $this->app->bind('command.make:storable-event', MakeStorableEventCommand::class);
84
85
        $this->commands([
86
            'command.event-projector:list-projectors',
87
            'command.event-projector:replay-events',
88
89
            'command.event-projector:list-snapshots',
90
            'command.event-projector:create-snapshots',
91
            'command.event-projector:load-snapshots',
92
            'command.event-projector:delete-snapshots',
93
94
            'command.make:projector',
95
            'command.make:reactor',
96
            'command.make:storable-event',
97
        ]);
98
    }
99
}
100