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

ToJsonFilePersistence   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 41
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1
ccs 14
cts 14
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A loadData() 0 10 3
A saveData() 0 4 1
A getFilePath() 0 4 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
}