Completed
Pull Request — master (#6)
by Daniel
02:39
created

EventServiceProvider::createEventDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Jellyfish\Event;
4
5
use Jellyfish\Event\Command\EventQueueConsumeCommand;
6
use Jellyfish\Event\Command\EventQueueWorkerStartCommand;
7
use Pimple\Container;
8
use Pimple\ServiceProviderInterface;
9
10
class EventServiceProvider implements ServiceProviderInterface
11
{
12
    /**
13
     * @param \Pimple\Container $pimple
14
     *
15
     * @return void
16
     */
17
    public function register(Container $pimple): void
18
    {
19
        $this->createEventFactory($pimple);
20
        $this->createEventQueueNameGenerator($pimple);
21
        $this->createEventMapper($pimple);
22
        $this->createEventQueueProducer($pimple);
23
        $this->createEventQueueConsumer($pimple);
24
        $this->createEventDispatcher($pimple);
25
        $this->createEventQueueWorker($pimple);
26
        $this->createCommands($pimple);
27
    }
28
29
    /**
30
     * @param \Pimple\Container $container
31
     *
32
     * @return \Pimple\ServiceProviderInterface
33
     */
34
    protected function createEventFactory(Container $container): ServiceProviderInterface
35
    {
36
        $container->offsetSet('event_factory', function () {
37
            return new EventFactory();
38
        });
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param \Pimple\Container $container
45
     *
46
     * @return \Pimple\ServiceProviderInterface
47
     */
48
    protected function createEventQueueNameGenerator(Container $container): ServiceProviderInterface
49
    {
50
        $container->offsetSet('event_queue_name_generator', function () {
51
            return new EventQueueNameGenerator();
52
        });
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param \Pimple\Container $container
59
     *
60
     * @return \Pimple\ServiceProviderInterface
61
     */
62
    protected function createEventMapper(Container $container): ServiceProviderInterface
63
    {
64
        $container->offsetSet('event_mapper', function (Container $container) {
65
            return new EventMapper(
66
                $container->offsetGet('event_factory'),
67
                $container->offsetGet('message_factory'),
68
                $container->offsetGet('serializer')
69
            );
70
        });
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param \Pimple\Container $container
77
     *
78
     * @return \Pimple\ServiceProviderInterface
79
     */
80
    protected function createEventQueueProducer(Container $container): ServiceProviderInterface
81
    {
82
        $container->offsetSet('event_queue_producer', function (Container $container) {
83
            return new EventQueueProducer(
84
                $container->offsetGet('event_mapper'),
85
                $container->offsetGet('event_queue_name_generator'),
86
                $container->offsetGet('queue_client')
87
            );
88
        });
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param \Pimple\Container $container
95
     *
96
     * @return \Pimple\ServiceProviderInterface
97
     */
98
    protected function createEventQueueConsumer(Container $container): ServiceProviderInterface
99
    {
100
        $container->offsetSet('event_queue_consumer', function (Container $container) {
101
            return new EventQueueConsumer(
102
                $container->offsetGet('process_factory'),
103
                $container->offsetGet('event_mapper'),
104
                $container->offsetGet('event_queue_name_generator'),
105
                $container->offsetGet('queue_client'),
106
                $container->offsetGet('root_dir')
107
            );
108
        });
109
110
        return $this;
111
    }
112
113
114
    /**
115
     * @param \Pimple\Container $container
116
     *
117
     * @return \Pimple\ServiceProviderInterface
118
     */
119
    protected function createEventDispatcher(Container $container): ServiceProviderInterface
120
    {
121
        $container->offsetSet('event_dispatcher', function (Container $container) {
122
            return new EventDispatcher(
123
                $container->offsetGet('event_queue_producer')
124
            );
125
        });
126
127
        return $this;
128
    }
129
130
    /**
131
     * @param \Pimple\Container $container
132
     *
133
     * @return \Pimple\ServiceProviderInterface
134
     */
135
    protected function createEventQueueWorker(Container $container): ServiceProviderInterface
136
    {
137
        $container->offsetSet('event_queue_worker', function (Container $container) {
138
            return new EventQueueWorker(
139
                $container->offsetGet('event_dispatcher'),
140
                $container->offsetGet('event_queue_consumer')
141
            );
142
        });
143
144
        return $this;
145
    }
146
147
    /**
148
     * @param \Pimple\Container $container
149
     *
150
     * @return \Pimple\ServiceProviderInterface
151
     */
152
    protected function createCommands(Container $container): ServiceProviderInterface
153
    {
154
        $container->extend('commands', function (array $commands, Container $container) {
155
            $commands[] = new EventQueueConsumeCommand(
156
                $container->offsetGet('event_dispatcher'),
157
                $container->offsetGet('event_queue_consumer')
158
            );
159
160
            $commands[] = new EventQueueWorkerStartCommand(
161
                $container->offsetGet('event_queue_worker')
162
            );
163
164
            return $commands;
165
        });
166
167
        return $this;
168
    }
169
}
170