Completed
Push — master ( c5ec2d...1e9b29 )
by Till
03:26
created

registerCommandEventsMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace TillKruss\LaravelTactician;
4
5
use League\Tactician\CommandBus;
6
use Illuminate\Support\ServiceProvider;
7
use TillKruss\LaravelTactician\Executer;
8
use League\Tactician\Handler\Locator\HandlerLocator;
9
use League\Tactician\Handler\CommandHandlerMiddleware;
10
use TillKruss\LaravelTactician\Middleware\EventMiddleware;
11
use TillKruss\LaravelTactician\Middleware\LoggerMiddleware;
12
use League\Tactician\Handler\MethodNameInflector\MethodNameInflector;
13
use League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor;
14
15
class TacticianServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Bootstrap the application events.
19
     */
20
    public function boot()
21
    {
22
        $config = __DIR__.'/config/tactician.php';
23
24
        $this->publishes([$config => config_path('tactician.php')]);
25
        $this->mergeConfigFrom($config, 'tactician');
26
    }
27
28
    /**
29
     * Register the tactician services.
30
     */
31
    public function register()
32
    {
33
        $this->registerCommandBus();
34
        $this->registerCommandExecuter();
35
        $this->registerLoggerMiddleware();
36
        $this->registerCommandEventsMiddleware();
37
        $this->registerCommandHandlerMiddleware();
38
        $this->bindTacticianInterfaces();
39
    }
40
41
    /**
42
     * Register the command bus instance.
43
     */
44
    protected function registerCommandBus()
45
    {
46
        $this->app->singleton('League\Tactician\CommandBus', function ($app) {
47
            return $app->make('tactician.commandbus');
48
        });
49
50
        $this->app->singleton('tactician.commandbus', function ($app) {
51
            $middleware = array_map(function ($name) use ($app) {
52
                return is_string($name) ? $app->make($name) : $name;
53
            }, $app->config->get('tactician.middleware'));
54
55
            return new CommandBus($middleware);
56
        });
57
    }
58
59
    /**
60
     * Register the command bus executer instance.
61
     */
62
    protected function registerCommandExecuter()
63
    {
64
        $this->app->singleton('TillKruss\LaravelTactician\Contracts\Executer', function ($app) {
65
            return $app->make(Executer::class);
66
        });
67
    }
68
69
    /**
70
     * Register the command handler middleware instance.
71
     */
72
    protected function registerCommandHandlerMiddleware()
73
    {
74
        $this->app->bind('League\Tactician\Handler\CommandHandlerMiddleware', function ($app) {
75
            return $app->make('tactician.middleware.commandhandler');
76
        });
77
78
        $this->app->bind('tactician.middleware.commandhandler', function ($app) {
79
            return new CommandHandlerMiddleware(
80
                $app->make(CommandNameExtractor::class),
81
                $app->make(HandlerLocator::class),
82
                $app->make(MethodNameInflector::class)
83
            );
84
        });
85
    }
86
87
    protected function registerLoggerMiddleware()
88
    {
89
        $this->app->bind('League\Tactician\Logger\LoggerMiddleware', function ($app) {
90
            return $app->make(LoggerMiddleware::class);
91
        });
92
    }
93
94
    protected function registerCommandEventsMiddleware()
95
    {
96
        $this->app->bind('League\Tactician\CommandEvents\EventMiddleware', function ($app) {
97
            return $app->make(EventMiddleware::class);
98
        });
99
    }
100
101
    /**
102
     * Register the tactician interface instances.
103
     */
104
    protected function bindTacticianInterfaces()
105
    {
106
        $this->app->bind('League\Tactician\Handler\Locator\HandlerLocator', function ($app) {
107
            return $app->make($app->config->get('tactician.locator'));
108
        });
109
110
        $this->app->bind('League\Tactician\Handler\MethodNameInflector\MethodNameInflector', function ($app) {
111
            return $app->make($app->config->get('tactician.inflector'));
112
        });
113
114
        $this->app->bind('League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor', function ($app) {
115
            return $app->make($app->config->get('tactician.extractor'));
116
        });
117
    }
118
}
119