Completed
Push — master ( 789991...7fba3b )
by Freek
01:46
created

EventProjectorServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 115
Duplicated Lines 26.09 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 8
Bugs 2 Features 1
Metric Value
wmc 6
lcom 1
cbo 9
dl 30
loc 115
rs 10
c 8
b 2
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
B bindCommands() 0 32 1
B boot() 10 22 4
A register() 20 56 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\Facades\Storage;
7
use Illuminate\Support\ServiceProvider;
8
use Spatie\EventProjector\Console\RebuildProjectorCommand;
9
use Spatie\EventProjector\Snapshots\SnapshotFactory;
10
use Spatie\EventProjector\Console\ReplayEventsCommand;
11
use Spatie\EventProjector\Snapshots\SnapshotRepository;
12
use Spatie\EventProjector\Console\ListProjectorsCommand;
13
use Spatie\EventProjector\Console\ResetProjectorCommand;
14
use Spatie\EventProjector\Console\Make\MakeReactorCommand;
15
use Spatie\EventProjector\EventSerializers\EventSerializer;
16
use Spatie\EventProjector\Console\Make\MakeProjectorCommand;
17
use Spatie\EventProjector\Console\Make\MakeStorableEventCommand;
18
use Spatie\EventProjector\Console\Snapshots\ListSnapshotsCommand;
19
use Spatie\EventProjector\Console\Snapshots\CreateSnapshotCommand;
20
use Spatie\EventProjector\Console\Snapshots\DeleteSnapshotCommand;
21
use Spatie\EventProjector\Console\Snapshots\RestoreSnapshotCommand;
22
23
class EventProjectorServiceProvider extends ServiceProvider
24
{
25
    public function boot()
26
    {
27
        if ($this->app->runningInConsole()) {
28
            $this->publishes([
29
                __DIR__.'/../config/event-projector.php' => config_path('event-projector.php'),
30
            ], 'config');
31
        }
32
33 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...
34
            $this->publishes([
35
                __DIR__.'/../stubs/create_stored_events_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_stored_events_table.php'),
36
            ], 'migrations');
37
        }
38
39 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...
40
            $this->publishes([
41
                __DIR__.'/../stubs/create_projector_statuses_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_projector_statuses_table.php'),
42
            ], 'migrations');
43
        }
44
45
        Event::subscribe(EventSubscriber::class);
46
    }
47
48
    public function register()
49
    {
50
        $this->mergeConfigFrom(__DIR__.'/../config/event-projector.php', 'event-projector');
51
52
        $this->app->singleton(EventProjectionist::class, function () {
53
            $config = config('event-projector');
54
55
            $projectionist = new EventProjectionist($config);
56
57
            $projectionist
58
                ->addProjectors($config['projectors'] ?? [])
59
                ->addReactors($config['reactors'] ?? []);
60
61
            return $projectionist;
62
        });
63
64
        $this->app->alias(EventProjectionist::class, 'event-projector');
65
66 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...
67
            $eventProjectionist = app(EventProjectionist::class);
68
69
            $config = config('event-projector');
70
71
            $diskName = $config['snapshots_disk'];
72
            $disk = Storage::disk($diskName);
73
74
            return new SnapshotFactory($eventProjectionist, $disk, $config);
75
        });
76
77 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...
78
            $eventProjectionist = app(EventProjectionist::class);
79
80
            $config = config('event-projector');
81
82
            $diskName = $config['snapshots_disk'];
83
            $disk = Storage::disk($diskName);
84
85
            return new SnapshotRepository($eventProjectionist, $disk, $config);
86
        });
87
88
        $this->app->singleton(EventSubscriber::class, function () {
89
            $eventProjectionist = app(EventProjectionist::class);
90
            $config = config('event-projector');
91
92
            return new EventSubscriber($eventProjectionist, $config);
93
        });
94
95
        $this->app
96
            ->when(ReplayEventsCommand::class)
97
            ->needs('$storedEventModelClass')
98
            ->give(config('event-projector.stored_event_model'));
99
100
        $this->app->bind(EventSerializer::class, config('event-projector.event_serializer'));
101
102
        $this->bindCommands();
103
    }
104
105
    protected function bindCommands()
106
    {
107
        $this->app->bind('command.event-projector:list-projectors', ListProjectorsCommand::class);
108
        $this->app->bind('command.event-projector:reset-projector', ResetProjectorCommand::class);
109
        $this->app->bind('command.event-projector:rebuild-projector', RebuildProjectorCommand::class);
110
        $this->app->bind('command.event-projector:replay-events', ReplayEventsCommand::class);
111
112
        $this->app->bind('command.event-projector:list-snapshots', ListSnapshotsCommand::class);
113
        $this->app->bind('command.event-projector:create-snapshot', CreateSnapshotCommand::class);
114
        $this->app->bind('command.event-projector:restore-snapshot', RestoreSnapshotCommand::class);
115
        $this->app->bind('command.event-projector:delete-snapshot', DeleteSnapshotCommand::class);
116
117
        $this->app->bind('command.make:projector', MakeProjectorCommand::class);
118
        $this->app->bind('command.make:reactor', MakeReactorCommand::class);
119
        $this->app->bind('command.make:storable-event', MakeStorableEventCommand::class);
120
121
        $this->commands([
122
            'command.event-projector:list-projectors',
123
            'command.event-projector:reset-projector',
124
            'command.event-projector:rebuild-projector',
125
            'command.event-projector:replay-events',
126
127
            'command.event-projector:list-snapshots',
128
            'command.event-projector:create-snapshot',
129
            'command.event-projector:restore-snapshot',
130
            'command.event-projector:delete-snapshot',
131
132
            'command.make:projector',
133
            'command.make:reactor',
134
            'command.make:storable-event',
135
        ]);
136
    }
137
}
138