Completed
Push — master ( 2cdfbe...c4b75e )
by Tobias
17:58
created

MessageSerializerDecorator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 64.29%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 104
ccs 18
cts 28
cp 0.6429
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A unwrapAndDeserialize() 0 4 1
A setHeader() 0 6 1
A getHeader() 0 8 2
B wrapAndSerialize() 0 22 4
1
<?php
2
3
namespace Happyr\Mq2phpBundle\Service;
4
5
use Happyr\Mq2phpBundle\Event\PrePublishMessage;
6
use SimpleBus\Serialization\Envelope\Serializer\MessageInEnvelopSerializer;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
9
/**
10
 * This service adds some extra headers on the message envelope.
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class MessageSerializerDecorator implements MessageInEnvelopSerializer, HeaderAwareInterface
15
{
16
    /**
17
     * @var MessageInEnvelopSerializer
18
     */
19
    private $serializer;
20
21
    /**
22
     * @var array
23
     */
24
    private $headers;
25
26
    /**
27
     * @var string
28
     */
29
    private $secretKey;
30
31
    /**
32
     * @var EventDispatcherInterface
33
     */
34
    private $eventDispatcher;
35
36
    /**
37
     * @param MessageInEnvelopSerializer $serializer
38
     * @param array                      $headers
39
     * @param string                     $secretKey
40
     * @param EventDispatcherInterface   $eventDispatcher
41
     */
42
    public function __construct(
43 1
        MessageInEnvelopSerializer $serializer,
44
        EventDispatcherInterface $eventDispatcher,
45
        array $headers = [],
46
        $secretKey = null
47
    ) {
48
        $this->serializer = $serializer;
49 1
        $this->eventDispatcher = $eventDispatcher;
50 1
        $this->headers = $headers;
51 1
        $this->secretKey = empty($secretKey) ? '' : $secretKey;
52 1
    }
53 1
54
    /**
55
     * Serialize a Message by wrapping it in an Envelope and serializing the envelope. This decoration will
56
     * take the SimpleBus envelope and add it in a json message.
57
     *
58
     * {@inheritdoc}
59
     */
60
    public function wrapAndSerialize($originalMessage)
61 1
    {
62
        $serializedMessage = $this->serializer->wrapAndSerialize($originalMessage);
63 1
64
        $message = [];
65 1
        foreach ($this->headers as $name => $value) {
66 1
            if (empty($value)) {
67 1
                continue;
68
            }
69
70
            $message['headers'][] = ['key' => $name, 'value' => $value];
71 1
        }
72 1
        $message['body'] = $serializedMessage;
73 1
74
        // Add a hash where the secret key is baked in.
75
        $message['headers'][] = ['key' => 'hash', 'value' => sha1($this->secretKey.$serializedMessage)];
76 1
77
        $event = new PrePublishMessage($message, is_object($originalMessage) ? get_class($originalMessage) : gettype($originalMessage));
78 1
        $this->eventDispatcher->dispatch(PrePublishMessage::NAME, $event);
79 1
80
        return json_encode($event->getMessage());
81 1
    }
82
83
    /**
84
     * Deserialize a Message that was wrapped in an Envelope.
85
     *
86
     * {@inheritdoc}
87
     */
88
    public function unwrapAndDeserialize($serializedEnvelope)
89
    {
90
        return $this->serializer->unwrapAndDeserialize($serializedEnvelope);
91
    }
92
93
    /**
94
     * @param string $name
95
     * @param mixed  $value
96
     *
97
     * @return $this
98
     */
99
    public function setHeader($name, $value)
100
    {
101
        $this->headers[$name] = $value;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param string $name
108
     */
109
    public function getHeader($name)
110
    {
111
        if (isset($this->headers[$name])) {
112
            return $this->headers[$name];
113
        }
114
115
        return;
116
    }
117
}
118