EventQueueProducer::enqueue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Event;
6
7
use Jellyfish\Queue\DestinationFactoryInterface;
8
use Jellyfish\Queue\DestinationInterface;
9
use Jellyfish\Queue\QueueClientInterface;
10
11
class EventQueueProducer implements EventQueueProducerInterface
12
{
13
    /**
14
     * @var \Jellyfish\Event\EventMapperInterface
15
     */
16
    protected $eventMapper;
17
18
    /**
19
     * @var \Jellyfish\Queue\QueueClientInterface
20
     */
21
    protected $queueClient;
22
23
    /**
24
     * @var \Jellyfish\Queue\DestinationFactoryInterface
25
     */
26
    protected $destinationFactory;
27
28
    /**
29
     * @param \Jellyfish\Event\EventMapperInterface $eventMapper
30
     * @param \Jellyfish\Queue\QueueClientInterface $queueClient
31
     * @param \Jellyfish\Queue\DestinationFactoryInterface $destinationFactory
32
     */
33
    public function __construct(
34
        EventMapperInterface $eventMapper,
35
        QueueClientInterface $queueClient,
36
        DestinationFactoryInterface $destinationFactory
37
    ) {
38
        $this->eventMapper = $eventMapper;
39
        $this->queueClient = $queueClient;
40
        $this->destinationFactory = $destinationFactory;
41
    }
42
43
    /**
44
     * @param \Jellyfish\Event\EventInterface $event
45
     *
46
     * @return \Jellyfish\Event\EventQueueProducerInterface
47
     */
48
    public function enqueue(
49
        EventInterface $event
50
    ): EventQueueProducerInterface {
51
        $destination = $this->destinationFactory->create()
52
            ->setName($event->getName())
53
            ->setType(DestinationInterface::TYPE_FANOUT);
54
55
        $message = $this->eventMapper->toMessage($event);
56
57
        $this->queueClient->sendMessage($destination, $message);
58
59
        return $this;
60
    }
61
}
62