Completed
Push — master ( 2ef464...a5d107 )
by Daniel
18s queued 11s
created

QueueClient::setProducer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\QueueRabbitMq;
6
7
use Jellyfish\Queue\ConsumerInterface;
8
use Jellyfish\Queue\DestinationInterface;
9
use Jellyfish\Queue\Exception\ConsumerNotFoundException;
10
use Jellyfish\Queue\Exception\ProducerNotFoundException;
11
use Jellyfish\Queue\MessageInterface;
12
use Jellyfish\Queue\ProducerInterface;
13
use Jellyfish\Queue\QueueClientInterface;
14
15
use function sprintf;
16
17
class QueueClient implements QueueClientInterface
18
{
19
    /**
20
     * @var \Jellyfish\Queue\ConsumerInterface[]
21
     */
22
    protected $consumers;
23
    
24
    /**
25
     * @var \Jellyfish\Queue\ProducerInterface[]
26
     */
27
    protected $producers;
28
29
    /**
30
     * @param \Jellyfish\Queue\ConsumerInterface[] $consumers
31
     * @param \Jellyfish\Queue\ProducerInterface[] $producers
32
     */
33
    public function __construct(
34
        array $consumers = [],
35
        array $producers = []
36
    ) {
37
        $this->consumers = $consumers;
38
        $this->producers = $producers;
39
    }
40
41
    /**
42
     * @param string $type
43
     * @param \Jellyfish\Queue\ConsumerInterface $consumer
44
     *
45
     * @return \Jellyfish\Queue\QueueClientInterface
46
     */
47
    public function setConsumer(string $type, ConsumerInterface $consumer): QueueClientInterface
48
    {
49
        $this->consumers[$type] = $consumer;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param string $type
56
     * @param \Jellyfish\Queue\ProducerInterface $producer
57
     *
58
     * @return \Jellyfish\Queue\QueueClientInterface
59
     */
60
    public function setProducer(string $type, ProducerInterface $producer): QueueClientInterface
61
    {
62
        $this->producers[$type] = $producer;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param \Jellyfish\Queue\DestinationInterface $destination
69
     *
70
     * @return \Jellyfish\Queue\MessageInterface|null
71
     */
72
    public function receiveMessage(DestinationInterface $destination): ?MessageInterface
73
    {
74
        if (!isset($this->consumers[$destination->getType()])) {
75
            throw new ConsumerNotFoundException(sprintf(
76
                'There is no consumer for type "%s".',
77
                $destination->getType()
78
            ));
79
        }
80
81
        return $this->consumers[$destination->getType()]->receiveMessage($destination);
82
    }
83
84
85
    /**
86
     * @param \Jellyfish\Queue\DestinationInterface $destination
87
     * @param int $limit
88
     *
89
     * @return \Jellyfish\Queue\MessageInterface[]
90
     */
91
    public function receiveMessages(DestinationInterface $destination, int $limit): array
92
    {
93
        if (!isset($this->consumers[$destination->getType()])) {
94
            throw new ConsumerNotFoundException(sprintf(
95
                'There is no consumer for type "%s".',
96
                $destination->getType()
97
            ));
98
        }
99
100
        return $this->consumers[$destination->getType()]->receiveMessages($destination, $limit);
101
    }
102
103
    /**
104
     * @param \Jellyfish\Queue\DestinationInterface $destination
105
     * @param \Jellyfish\Queue\MessageInterface $message
106
     *
107
     * @return \Jellyfish\Queue\QueueClientInterface
108
     */
109
    public function sendMessage(DestinationInterface $destination, MessageInterface $message): QueueClientInterface
110
    {
111
        if (!isset($this->producers[$destination->getType()])) {
112
            throw new ProducerNotFoundException(sprintf(
113
                'There is no producer for type "%s".',
114
                $destination->getType()
115
            ));
116
        }
117
118
        $this->producers[$destination->getType()]->sendMessage($destination, $message);
119
120
        return $this;
121
    }
122
}
123