EventMessageWriter::writeEventMessage()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 35
ccs 25
cts 25
cp 1
rs 8.8571
cc 3
eloc 24
nc 4
nop 1
crap 3
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * The software is based on the Axon Framework project which is
17
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
18
 * see <http://www.axonframework.org/>.
19
 * 
20
 * This software consists of voluntary contributions made by many individuals
21
 * and is licensed under the MIT license. For more information, see
22
 * <http://www.governor-framework.org/>.
23
 */
24
25
namespace Governor\Framework\EventHandling\Io;
26
27
use Governor\Framework\Serializer\SerializerInterface;
28
use Governor\Framework\Serializer\MessageSerializer;
29
use Governor\Framework\Domain\EventMessageInterface;
30
use Governor\Framework\Domain\DomainEventMessageInterface;
31
32
/**
33
 * EventMessageWriter is responsible to convert an {@link EventMessageInterface} to a binary stream.
34
 *
35
 * @author    "David Kalosi" <[email protected]>  
36
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> 
37
 */
38
class EventMessageWriter
39
{
40
41
    /**
42
     * @var MessageSerializer 
43
     */
44
    private $serializer;
45
46
    /**     
47
     * @param SerializerInterface $serializer Serializer.
48
     */
49 8
    public function __construct(SerializerInterface $serializer)
50
    {
51 8
        $this->serializer = new MessageSerializer($serializer);
52 8
    }
53
54
    /**
55
     * @param EventMessageInterface $event
56
     * @return mixed
57
     */
58 8
    public function writeEventMessage(EventMessageInterface $event)
59
    {
60 8
        if ($event instanceof DomainEventMessageInterface) {
61 1
            $type = 3;
62 1
        } else {
63 7
            $type = 1;
64
        }
65
66 8
        $serializedPayload = $this->serializer->serializePayload($event);
67 8
        $serializedMetaData = $this->serializer->serializeMetaData($event);
68
                
69 8
        $data = pack("na36N", $type, $event->getIdentifier(),
70 8
                $event->getTimestamp()->format('U'));
71
72 8
        if ($event instanceof DomainEventMessageInterface) {
73 1
            $data .= pack("a36N", $event->getAggregateIdentifier(),
74 1
                    $event->getScn());
75 1
        }
76
77
        // TODO payload revision
78 8
        $packFormat = sprintf("Na%sNa%sNa%s",
79 8
                strlen($serializedPayload->getType()->getName()),
80 8
                strlen($serializedPayload->getData()),
81 8
                strlen($serializedMetaData->getData()));
82
83 8
        $data .= pack($packFormat,
84 8
                strlen($serializedPayload->getType()->getName()),
85 8
                $serializedPayload->getType()->getName(),
86 8
                strlen($serializedPayload->getData()),
87 8
                $serializedPayload->getData(),
88 8
                strlen($serializedMetaData->getData()),
89 8
                $serializedMetaData->getData());
90
                
91 8
        return $data;
92
    }
93
94
}