FilesystemSnapshotEventReader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 5
nc 1
nop 3
crap 1
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\EventStore\Filesystem;
26
27
use Governor\Framework\EventStore\EventStoreException;
28
use Governor\Framework\Serializer\SerializerInterface;
29
use Governor\Framework\Serializer\SerializedDomainEventDataInterface;
30
31
/**
32
 * Description of FileSystemSnapshotEventReader
33
 * 
34
 * @author    "David Kalosi" <[email protected]>  
35
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> 
36
 */
37
class FilesystemSnapshotEventReader
38
{
39
40
     /**
41
     * @var \SplFileObject
42
     */
43
    private $eventFile;
44
     /**
45
     * @var \SplFileObject
46
     */
47
    private $snapshotEventFile;
48
    
49
    /**     
50
     * @var SerializerInterface
51
     */
52
    private $eventSerializer;
53
54
    /**
55
     * Creates a snapshot event reader that reads the latest snapshot from the <code>snapshotEventFile</code>.
56
     *
57
     * @param \SplFileObject $eventFile         used to skip the number of bytes specified by the latest snapshot
58
     * @param \SplFileObject $snapshotEventFile the file to read snapshots from
59
     * @param SerializerInterface $eventSerializer   the serializer that is used to deserialize events in snapshot file
60
     */
61 1
    public function __construct(\SplFileObject $eventFile, \SplFileObject $snapshotEventFile,
62
            SerializerInterface $eventSerializer)
63
    {
64 1
        $this->eventFile = $eventFile;
65 1
        $this->snapshotEventFile = $snapshotEventFile;
66 1
        $this->eventSerializer = $eventSerializer;
67 1
    }
68
69
    /**
70
     * Reads the latest snapshot of the given aggregate identifier.
71
     *
72
     * @param string $type       the aggregate's type
73
     * @param string $identifier the aggregate's identifier
74
     * @return SerializedDomainEventDataInterface The latest snapshot of the given aggregate identifier
75
     *
76
     * @throws EventStoreException when reading the <code>snapshotEventFile</code> or reading the <code>eventFile</code> failed
77
     */
78 1
    public function readSnapshotEvent($type, $identifier)
79
    {
80 1
        $snapshotEvent = null;
81 1
        $fileSystemSnapshotEvent = $this->readLastSnapshotEntry();
82
83 1
        if (null !== $fileSystemSnapshotEvent) {
84 1
            $this->eventFile->fseek($fileSystemSnapshotEvent['bytesToSkip']);
85 1
            $actuallySkipped = $this->eventFile->ftell();
86
            
87 1
            if ($actuallySkipped !== $fileSystemSnapshotEvent['bytesToSkip']) {
88
                throw new EventStoreException(sprintf(
89
                        "The skip operation did not actually skip the expected amount of bytes. " .
90
                        "The event log of aggregate of type %s and identifier %s might be corrupt.",
91
                        $type, $identifier));
92
            }
93
94 1
            $snapshotEvent = $fileSystemSnapshotEvent['snapshotEvent'];
95 1
        }
96
97 1
        return $snapshotEvent;
98
    }
99
100 1
    private function readLastSnapshotEntry()
101
    {
102 1
        $lastSnapshotEvent = null;
103
104
        do {
105 1
            $snapshotEvent = $this->readSnapshotEventEntry();
106
107 1
            if (!empty($snapshotEvent)) {
108 1
                $lastSnapshotEvent = $snapshotEvent;
109 1
            }
110 1
        } while (!empty($snapshotEvent));
111
112 1
        return $lastSnapshotEvent;
113
    }
114
115 1
    private function readSnapshotEventEntry()
116
    {
117 1
        $snapshotEventReader = new FilesystemEventMessageReader($this->snapshotEventFile,
118 1
                $this->eventSerializer);
0 ignored issues
show
Unused Code introduced by
The call to FilesystemEventMessageReader::__construct() has too many arguments starting with $this->eventSerializer.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
119
120 1
        $bytesToSkip = $this->readLong($this->snapshotEventFile);
121 1
        $snapshotEvent = $snapshotEventReader->readEventMessage();
122
123 1
        if (null === $bytesToSkip && null === $snapshotEvent) {
124 1
            return array();
125
        }
126
127 1
        return array('snapshotEvent' => $snapshotEvent, 'bytesToSkip' => $bytesToSkip);
128
    }
129
130
    /**
131
     * @param \SplFileObject $file
132
     * @return int
133
     */
134 1
    private function readLong($file)
135
    {
136 1
        $stream = null;
137 1
        for ($cc = 0; $cc < 4; $cc++) {
138 1
            if ($file->eof()) {
139 1
                return null;
140
            }
141
142 1
            $stream .= $file->fgetc();
143 1
        }
144
145 1
        $data = unpack("Nskip", $stream);
146 1
        return $data['skip'];
147
    }
148
149
}
150