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

ToJsonFilePersistence::saveData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Copyright (c) 2017 Constantin Galbenu <[email protected]>
4
 */
5
6
namespace Gica\Cqrs\Saga\SagaPersistence;
7
8
9
use Gica\Cqrs\Saga\SagaPersistence;
10
use Gica\FileSystem\FileSystemInterface;
11
12
class ToJsonFilePersistence implements SagaPersistence
13
{
14
    /**
15
     * @var FileSystemInterface
16
     */
17
    private $fileSystem;
18
    /**
19
     * @var string
20
     */
21
    private $directory;
22
23 1
    public function __construct(
24
        FileSystemInterface $fileSystem,
25
        string $directory
26
    )
27
    {
28 1
        $this->fileSystem = $fileSystem;
29 1
        $this->directory = $directory;
30 1
    }
31
32 1
    public function loadData(string $sagaId)
33
    {
34
        try {
35 1
            $fileContents = $this->fileSystem->fileGetContents($this->getFilePath($sagaId));
36 1
            return null !== $fileContents ? json_decode($fileContents, true) : null;
37
38 1
        } catch (\Throwable $exception) {
39 1
            return null;
40
        }
41
    }
42
43 1
    public function saveData(string $sagaId, $data)
44
    {
45 1
        $this->fileSystem->filePutContents($this->getFilePath($sagaId), json_encode($data));
46 1
    }
47
48 1
    private function getFilePath(string $sagaId): string
49
    {
50 1
        return $this->directory . DIRECTORY_SEPARATOR . $sagaId;
51
    }
52
}