FilesystemSnapshotEventWriter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 84.62%

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 6
c 3
b 2
f 1
lcom 1
cbo 4
dl 0
loc 85
ccs 22
cts 26
cp 0.8462
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A writeSnapshotEvent() 0 15 2
A calculateOffset() 0 17 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\EventStore\Filesystem;
26
27
use Governor\Framework\Serializer\SerializerInterface;
28
use Governor\Framework\Domain\DomainEventMessageInterface;
29
use Governor\Framework\EventStore\EventStoreException;
30
31
/**
32
 * Description of FilesystemSnapshotEventWriter
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 FilesystemSnapshotEventWriter
38
{
39
40
    /**
41
     * @var \SplFileObject
42
     */
43
    private $eventFile;
44
45
    /**
46
     * @var \SplFileObject
47
     */
48
    private $snapshotEventFile;
49
50
    /**
51
     * @var SerializerInterface
52
     */
53
    private $eventSerializer;
54
55
    /**
56
     * Creates a snapshot event writer that writes any given <code>snapshotEvent</code> to the given
57
     * <code>snapshotEventFile</code>.
58
     *
59
     * @param \SplFileObject $eventFile         used to skip the number of bytes specified by the latest snapshot
60
     * @param \SplFileObject $snapshotEventFile the file to read snapshots from
61
     * @param SerializerInterface $eventSerializer   the serializer that is used to deserialize events in snapshot file
62
     */
63 1
    public function __construct(\SplFileObject $eventFile,
64
            \SplFileObject $snapshotEventFile,
65
            SerializerInterface $eventSerializer)
66
    {
67 1
        $this->eventFile = $eventFile;
68 1
        $this->snapshotEventFile = $snapshotEventFile;
69 1
        $this->eventSerializer = $eventSerializer;
70 1
    }
71
72
    /**
73
     * Writes the given snapshotEvent to the {@link #snapshotEventFile}.
74
     * Prepends a long value to the event in the file indicating the bytes to skip when reading the {@link #eventFile}.
75
     *
76
     * @param DomainEventMessageInterface $snapshotEvent The snapshot to write to the {@link #snapshotEventFile}
77
     * @throws EventStoreException
78
     */
79 1
    public function writeSnapshotEvent(DomainEventMessageInterface $snapshotEvent)
80
    {
81
        try {
82 1
            $offset = $this->calculateOffset($snapshotEvent);
83 1
            $this->snapshotEventFile->fwrite(pack("N", $offset));
84
85 1
            $eventMessageWriter = new FilesystemEventMessageWriter($this->snapshotEventFile,
86 1
                    $this->eventSerializer);
87
88 1
            $eventMessageWriter->writeEventMessage($snapshotEvent);
89 1
        } catch (\Exception $ex) {
90
            throw new EventStoreException("Error writing a snapshot event", 0,
91
            $ex);
92
        }
93 1
    }
94
95
    /**
96
     * Calculate the bytes to skip when reading the event file.
97
     *
98
     * @param DomainEventMessageInterface $snapshotEvent the snapshot event
99
     * @return integer the bytes to skip when reading the event file
100
     *
101
     * @throws \Exception
102
     */
103 1
    private function calculateOffset(DomainEventMessageInterface $snapshotEvent)
104
    {
105
        try {
106 1
            $eventMessageReader = new FilesystemEventMessageReader($this->eventFile,
107 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...
108
109 1
            $lastReadSequenceNumber = -1;
110 1
            while ($lastReadSequenceNumber < $snapshotEvent->getScn()) {
111 1
                $entry = $eventMessageReader->readEventMessage();
112 1
                $lastReadSequenceNumber = $entry->getScn();
113 1
            }
114
115 1
            return $this->eventFile->ftell();
116
        } catch (\Exception $ex) {
117
            throw $ex;
118
        }
119
    }
120
121
}
122