Completed
Push — master ( 9b8f2e...06d1b0 )
by Freek
01:40
created

Snapshot::isValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\EventProjector\Snapshots;
4
5
use Carbon\Carbon;
6
use Spatie\EventProjector\EventProjectionist;
7
use Illuminate\Contracts\Filesystem\Filesystem;
8
use Spatie\EventProjector\Projectors\Projector;
9
10
class Snapshot
11
{
12
    /** @var \Spatie\EventProjector\EventProjectionist */
13
    protected $eventProjectionist;
14
15
    /** @var \Illuminate\Contracts\Filesystem\Filesystem */
16
    protected $disk;
17
18
    /** @var string */
19
    protected $fileName;
20
21
    public function __construct(EventProjectionist $eventProjectionist, Filesystem $disk, string $fileName)
22
    {
23
        $this->eventProjectionist = $eventProjectionist;
24
25
        $this->disk = $disk;
26
27
        $this->fileName = $fileName;
28
    }
29
30
    public function isValid(): bool
31
    {
32
        return $this->projectorName() !== '';
33
    }
34
35
    public function lastProcessedEventId(): int
36
    {
37
        return $this->getNameParts()['lastProcessedEventId'];
38
    }
39
40
    public function projectorName(): string
41
    {
42
        return $this->getNameParts()['projectorName'];
43
    }
44
45
    public function getProjector(): Projector
46
    {
47
        $projectorName = $this->projectorName();
48
49
        return $this->eventProjectionist->getProjector($projectorName);
50
    }
51
52
    public function name(): string
53
    {
54
        return $this->getNameParts()['name'];
55
    }
56
57
    /**
58
     * @param string|resource $contents
59
     */
60
    public function write($contents)
61
    {
62
        $this->disk->put($this->fileName, $contents);
63
    }
64
65
    public function read(): ?string
66
    {
67
        return $this->disk->get($this->fileName);
68
    }
69
70
    public function readStream()
71
    {
72
        return $this->disk->readStream($this->fileName);
0 ignored issues
show
Bug introduced by
The method readStream() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
    }
74
75
    public function delete()
76
    {
77
        $this->disk->delete($this->fileName);
78
    }
79
80
    public function createdAt(): Carbon
81
    {
82
        $timestamp = $this->disk->lastModified($this->fileName);
83
84
        return Carbon::createFromTimestamp($timestamp);
85
    }
86
87
    protected function getNameParts(): array
88
    {
89
        $baseName = pathinfo($this->fileName, PATHINFO_FILENAME);
90
91
        $nameParts = explode('---', $baseName);
92
93
        $projectorName =  str_replace('+', '\\', $nameParts[1] ?? '');
94
95
        $lastProcessedEventId = $nameParts[2] ?? 0;
96
97
        $name =  $nameParts[3] ?? '';
98
99
        return compact('projectorName', 'lastProcessedEventId', 'name');
100
    }
101
}