QueueInteropCommandQueue   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 12
c 3
b 0
f 0
dl 0
loc 63
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A send() 0 7 2
A getMessageProducer() 0 3 1
A getMessage() 0 3 1
1
<?php
2
3
/*
4
 * cqrs-async-queue-interop (https://github.com/phpgears/cqrs-async-queue-interop).
5
 * Queue-interop async decorator for CQRS command bus.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/cqrs-async-queue-interop
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\CQRS\Async\QueueInterop;
15
16
use Gears\CQRS\Async\AbstractCommandQueue;
17
use Gears\CQRS\Async\Exception\CommandQueueException;
18
use Gears\CQRS\Async\Serializer\CommandSerializer;
19
use Gears\CQRS\Command;
20
use Interop\Queue\Context;
21
use Interop\Queue\Destination;
22
use Interop\Queue\Message;
23
use Interop\Queue\Producer;
24
25
class QueueInteropCommandQueue extends AbstractCommandQueue
26
{
27
    /**
28
     * Queue context.
29
     *
30
     * @var Context
31
     */
32
    protected $context;
33
34
    /**
35
     * @var Destination
36
     */
37
    protected $destination;
38
39
    /**
40
     * EnqueueCommandBus constructor.
41
     *
42
     * @param CommandSerializer $serializer
43
     * @param Context           $context
44
     * @param Destination       $destination
45
     */
46
    public function __construct(CommandSerializer $serializer, Context $context, Destination $destination)
47
    {
48
        parent::__construct($serializer);
49
50
        $this->context = $context;
51
        $this->destination = $destination;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    final public function send(Command $command): void
58
    {
59
        // @codeCoverageIgnoreStart
60
        try {
61
            $this->getMessageProducer()->send($this->destination, $this->getMessage($command));
62
        } catch (\Exception $exception) {
63
            throw new CommandQueueException('Failure enqueueing command.', 0, $exception);
64
        }
65
        // @codeCoverageIgnoreEnd
66
    }
67
68
    /**
69
     * Get message from command.
70
     *
71
     * @param Command $command
72
     *
73
     * @return Message
74
     */
75
    protected function getMessage(Command $command): Message
76
    {
77
        return $this->context->createMessage($this->getSerializedCommand($command));
78
    }
79
80
    /**
81
     * Get message producer.
82
     *
83
     * @return Producer
84
     */
85
    protected function getMessageProducer(): Producer
86
    {
87
        return $this->context->createProducer();
88
    }
89
}
90