EventServiceProvider::createEventMapper()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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