FilesystemDomainEventStream::peek()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Governor\Framework\EventStore\Filesystem;
10
11
use Governor\Framework\Domain\GenericDomainEventMessage;
12
use Governor\Framework\EventStore\EventStoreException;
13
use Governor\Framework\Domain\DomainEventStreamInterface;
14
use Governor\Framework\Serializer\SerializerInterface;
15
16
/**
17
 * Description of FilesystemDomainEventStream
18
 *
19
 * @author    "David Kalosi" <[email protected]>  
20
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> 
21
 */
22
class FilesystemDomainEventStream implements DomainEventStreamInterface
23
{
24
25
    protected $eventReader;
26
    protected $events;
27
    protected $serializer;
28
29 2 View Code Duplication
    public function __construct(\SplFileObject $file,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
        SerializerInterface $serializer)
31
    {
32 2
        $this->eventReader = new FilesystemEventMessageReader($file);
33 2
        $this->events = new \SplDoublyLinkedList();
34 2
        $this->serializer = $serializer;
35 2
        $this->doReadNext();
36 1
    }
37
38 2
    public function hasNext()
39
    {
40 2
        if ($this->events->isEmpty()) {
41 2
            $this->doReadNext();
42 2
        }
43
44 2
        return !$this->events->isEmpty();
45
    }
46
47 2
    public function next()
48
    {
49 2
        $nextMessage = $this->events->shift();
50
51 2
        if ($this->events->isEmpty()) {
52 2
            $this->doReadNext();
53 2
        }
54
55 2
        return $nextMessage;
56
    }
57
58
    public function peek()
59
    {
60
        return $this->events->top();
61
    }
62
63 3
    private function doReadNext()
64
    {
65
        try {
66 3
            if (null !== $event = $this->eventReader->readEventMessage()) {
67 2
                $payload = $this->serializer->deserialize($event->getPayload());
68 2
                $metadata = $this->serializer->deserialize($event->getMetaData());
69
70 2
                $message = new GenericDomainEventMessage($event->getAggregateIdentifier(),
71 2
                    $event->getScn(), $payload, $metadata,
72 2
                    $event->getEventIdentifier(), $event->getTimestamp());
73 2
                $this->events->push($message);
74 2
            }
75 3
        } catch (\Exception $ex) {
76 1
            throw new EventStoreException("An error occurred while reading the event stream",
77 1
            0, $ex);
78
        }
79 2
    }
80
81
}
82