MessageSerializerDecorator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85.19%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 104
ccs 23
cts 27
cp 0.8519
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A unwrapAndDeserialize() 0 4 1
A __construct() 0 11 2
B wrapAndSerialize() 0 22 4
A setHeader() 0 6 1
A getHeader() 0 8 2
1
<?php
2
3
namespace Happyr\Mq2phpBundle\Service;
4
5
use Happyr\Mq2phpBundle\Event\PrePublishMessage;
6
use SimpleBus\Serialization\Envelope\Serializer\MessageInEnvelopeSerializer;
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 MessageInEnvelopeSerializer, HeaderAwareInterface
15
{
16
    /**
17
     * @var MessageInEnvelopeSerializer
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 MessageInEnvelopeSerializer $serializer
38
     * @param array                      $headers
39
     * @param string                     $secretKey
40
     * @param EventDispatcherInterface   $eventDispatcher
41
     */
42 2
    public function __construct(
43
        MessageInEnvelopeSerializer $serializer,
44
        EventDispatcherInterface $eventDispatcher,
45
        array $headers = [],
46
        $secretKey = null
47
    ) {
48 2
        $this->serializer = $serializer;
49 2
        $this->eventDispatcher = $eventDispatcher;
50 2
        $this->headers = $headers;
51 2
        $this->secretKey = empty($secretKey) ? '' : $secretKey;
52 2
    }
53
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 1
    public function wrapAndSerialize($originalMessage)
61
    {
62 1
        $serializedMessage = $this->serializer->wrapAndSerialize($originalMessage);
63
64 1
        $message = [];
65 1
        foreach ($this->headers as $name => $value) {
66 1
            if (empty($value)) {
67
                continue;
68
            }
69
70 1
            $message['headers'][] = ['key' => $name, 'value' => $value];
71
        }
72 1
        $message['body'] = $serializedMessage;
73
74
        // Add a hash where the secret key is baked in.
75 1
        $message['headers'][] = ['key' => 'hash', 'value' => sha1($this->secretKey.$serializedMessage)];
76
77 1
        $event = new PrePublishMessage($message, is_object($originalMessage) ? get_class($originalMessage) : gettype($originalMessage));
78 1
        $this->eventDispatcher->dispatch(PrePublishMessage::NAME, $event);
79
80 1
        return json_encode($event->getMessage());
81
    }
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 1
    public function setHeader($name, $value)
100
    {
101 1
        $this->headers[$name] = $value;
102
103 1
        return $this;
104
    }
105
106
    /**
107
     * @param string $name
108
     */
109 1
    public function getHeader($name)
110
    {
111 1
        if (isset($this->headers[$name])) {
112
            return $this->headers[$name];
113
        }
114
115 1
        return;
116
    }
117
}
118