Completed
Push — master ( d37987...e17880 )
by Markus
20s queued 11s
created

EventServiceProvider::createEventDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 10
rs 10
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
     * @var \Jellyfish\Event\EventQueueNameGeneratorInterface|null
14
     */
15
    protected $eventQueueNameGenerator;
16
17
    /**
18
     * @var \Jellyfish\Event\EventMapperInterface
19
     */
20
    protected $eventMapper;
21
22
    /**
23
     * @var \Jellyfish\Event\EventQueueProducerInterface
24
     */
25
    protected $eventQueueProducer;
26
27
    /**
28
     * @var \Jellyfish\Event\EventQueueConsumerInterface
29
     */
30
    protected $eventQueueConsumer;
31
32
    /**
33
     * @var \Jellyfish\Event\EventQueueWorkerInterface
34
     */
35
    protected $eventQueueWorker;
36
37
    /**
38
     * @param \Pimple\Container $pimple
39
     *
40
     * @return void
41
     */
42
    public function register(Container $pimple): void
43
    {
44
        $this->registerEventFactory($pimple)
45
            ->registerEventDispatcher($pimple)
46
            ->registerCommands($pimple);
47
    }
48
49
    /**
50
     * @return \Jellyfish\Event\EventQueueNameGeneratorInterface
51
     */
52
    protected function createEventQueueNameGenerator(): EventQueueNameGeneratorInterface
53
    {
54
        if ($this->eventQueueNameGenerator === null) {
55
            $this->eventQueueNameGenerator = new EventQueueNameGenerator();
56
        }
57
58
        return $this->eventQueueNameGenerator;
59
    }
60
61
    /**
62
     * @param \Pimple\Container $container
63
     * @return \Jellyfish\Event\EventMapperInterface
64
     */
65
    protected function createEventMapper(Container $container): EventMapperInterface
66
    {
67
        if ($this->eventMapper === null) {
68
            $this->eventMapper = new EventMapper(
69
                $container->offsetGet('event_factory'),
70
                $container->offsetGet('message_factory'),
71
                $container->offsetGet('serializer')
72
            );
73
        }
74
75
        return $this->eventMapper;
76
    }
77
78
    /**
79
     * @param \Pimple\Container $container
80
     *
81
     * @return \Jellyfish\Event\EventQueueProducerInterface
82
     */
83
    protected function createEventQueueProducer(Container $container): EventQueueProducerInterface
84
    {
85
        if ($this->eventQueueProducer === null) {
86
            $this->eventQueueProducer = new EventQueueProducer(
87
                $this->createEventMapper($container),
88
                $this->createEventQueueNameGenerator(),
89
                $container->offsetGet('queue_client')
90
            );
91
        }
92
93
        return $this->eventQueueProducer;
94
    }
95
96
    /**
97
     * @param \Pimple\Container $container
98
     *
99
     * @return \Jellyfish\Event\EventQueueConsumerInterface
100
     */
101
    protected function createEventQueueConsumer(Container $container): EventQueueConsumerInterface
102
    {
103
        if ($this->eventQueueConsumer === null) {
104
            $this->eventQueueConsumer = new EventQueueConsumer(
105
                $container->offsetGet('process_factory'),
106
                $this->createEventMapper($container),
107
                $this->createEventQueueNameGenerator(),
108
                $container->offsetGet('queue_client'),
109
                $container->offsetGet('root_dir')
110
            );
111
        }
112
113
        return $this->eventQueueConsumer;
114
    }
115
116
    /**
117
     * @param \Pimple\Container $container
118
     *
119
     * @return \Jellyfish\Event\EventQueueWorkerInterface
120
     */
121
    protected function createEventQueueWorker(Container $container): EventQueueWorkerInterface
122
    {
123
        if ($this->eventQueueWorker === null) {
124
            $this->eventQueueWorker = new EventQueueWorker(
125
                $container->offsetGet('event_dispatcher')->getEventListenerProvider(),
126
                $this->createEventQueueConsumer($container)
127
            );
128
        }
129
130
        return $this->eventQueueWorker;
131
    }
132
133
    /**
134
     * @param \Pimple\Container $container
135
     *
136
     * @return \Jellyfish\Event\EventServiceProvider
137
     */
138
    protected function registerEventFactory(Container $container): EventServiceProvider
139
    {
140
        $container->offsetSet('event_factory', function () {
141
            return new EventFactory();
142
        });
143
144
        return $this;
145
    }
146
147
    /**
148
     * @param \Pimple\Container $container
149
     *
150
     * @return \Jellyfish\Event\EventServiceProvider
151
     */
152
    protected function registerEventDispatcher(Container $container): EventServiceProvider
153
    {
154
        $self = $this;
155
156
        $container->offsetSet('event_dispatcher', function (Container $container) use ($self) {
157
            return new EventDispatcher(
158
                new EventListenerProvider(),
159
                $self->createEventQueueProducer($container)
160
            );
161
        });
162
163
        return $this;
164
    }
165
166
    /**
167
     * @param \Pimple\Container $container
168
     *
169
     * @return \Jellyfish\Event\EventServiceProvider
170
     */
171
    protected function registerCommands(Container $container): EventServiceProvider
172
    {
173
        $self = $this;
174
175
        $container->extend('commands', function (array $commands, Container $container) use ($self) {
176
            $commands[] = new EventQueueConsumeCommand(
177
                $container->offsetGet('event_dispatcher')->getEventListenerProvider(),
178
                $self->createEventQueueConsumer($container),
179
                $container->offsetGet('lock_factory'),
180
                $container->offsetGet('logger')
181
            );
182
183
            $commands[] = new EventQueueWorkerStartCommand(
184
                $self->createEventQueueWorker($container)
185
            );
186
187
            return $commands;
188
        });
189
190
        return $this;
191
    }
192
}
193