Passed
Pull Request — master (#6)
by Daniel
04:58
created

EventServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 51
dl 0
loc 157
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createEventDispatcher() 0 9 1
A createEventFactory() 0 7 1
A createEventMapper() 0 11 1
A createEventQueueNameGenerator() 0 7 1
A createEventQueueConsumer() 0 12 1
A register() 0 10 1
A createEventQueueProducer() 0 11 1
A createEventQueueWorker() 0 10 1
A createCommands() 0 16 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
            );
107
        });
108
109
        return $this;
110
    }
111
112
113
    /**
114
     * @param \Pimple\Container $container
115
     *
116
     * @return \Pimple\ServiceProviderInterface
117
     */
118
    protected function createEventDispatcher(Container $container): ServiceProviderInterface
119
    {
120
        $container->offsetSet('event_dispatcher', function (Container $container) {
121
            return new EventDispatcher(
122
                $container->offsetGet('event_queue_producer')
123
            );
124
        });
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param \Pimple\Container $container
131
     *
132
     * @return \Pimple\ServiceProviderInterface
133
     */
134
    protected function createEventQueueWorker(Container $container): ServiceProviderInterface
135
    {
136
        $container->offsetSet('event_queue_worker', function (Container $container) {
137
            return new EventQueueWorker(
138
                $container->offsetGet('event_dispatcher'),
139
                $container->offsetGet('event_queue_consumer')
140
            );
141
        });
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param \Pimple\Container $container
148
     *
149
     * @return \Pimple\ServiceProviderInterface
150
     */
151
    protected function createCommands(Container $container): ServiceProviderInterface
152
    {
153
        $container->extend('commands', function (array $commands, Container $container) {
154
            $commands[] = new EventQueueConsumeCommand(
155
                $container->offsetGet('event_dispatcher'),
156
                $container->offsetGet('event_queue_consumer')
157
            );
158
159
            $commands[] = new EventQueueWorkerStartCommand(
160
                $container->offsetGet('event_queue_worker')
161
            );
162
163
            return $commands;
164
        });
165
166
        return $this;
167
    }
168
}
169