Completed
Push — master ( 6b9dac...24d110 )
by Constantin
02:54
created

getLastPersistedEventSequenceAndIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Copyright (c) 2017 Constantin Galbenu <[email protected]>
4
 */
5
6
namespace Gica\Cqrs\Saga;
7
8
9
class SagaRepository
10
{
11
12
    /**
13
     * @var SagaPersistence
14
     */
15
    private $sagaPersistence;
16
17 5
    public function __construct(
18
        SagaPersistence $sagaPersistence
19
    )
20
    {
21 5
        $this->sagaPersistence = $sagaPersistence;
22 5
    }
23
24 3
    public function isEventAlreadyDispatched(string $sagaId, int $sequence, int $index): bool
25
    {
26 3
        $data = $this->sagaPersistence->loadData($sagaId);
27
28 3
        $lastSequence = $data ? $data['sequence'] : -10000;
29 3
        $lastIndex = $data ? $data['index'] : -1000;
30
31 3
        if ($sequence > $lastSequence || ($sequence == $lastSequence && $index > $lastIndex)) {
32 2
            return false;
33
        }
34
35 1
        return true;
36
    }
37
38 1
    public function persistLastProcessedEventBySaga(string $sagaId, int $sequence, int $index)
39
    {
40 1
        $this->sagaPersistence->saveData($sagaId, [
41 1
            'sequence' => $sequence,
42 1
            'index'    => $index,
43
        ]);
44 1
    }
45
46 1
    public function getLastPersistedEventSequenceAndIndex(string $sagaId)
47
    {
48 1
        $data = $this->sagaPersistence->loadData($sagaId);
49
50 1
        return $data ? [$data['sequence'], $data['index']] : [];
51
    }
52
}