Completed
Push — master ( c9545d...9b8f2e )
by Freek
01:31
created

Snapshot::getProjector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 lastProcessedEventId(): int
31
    {
32
        return $this->getNameParts()['lastProcessedEventId'];
33
    }
34
35
    public function projectorName(): string
36
    {
37
        return $this->getNameParts()['projectorName'];
38
    }
39
40
    public function getProjector(): Projector
41
    {
42
        $projectorName = $this->projectorName();
43
44
        return $this->eventProjectionist->getProjector($projectorName);
45
    }
46
47
    public function name(): string
48
    {
49
        return $this->getNameParts()['name'];
50
    }
51
52
    /**
53
     * @param string|resource $contents
54
     */
55
    public function write($contents)
56
    {
57
        $this->disk->put($this->fileName, $contents);
58
    }
59
60
    public function read(): ?string
61
    {
62
        return $this->disk->get($this->fileName);
63
    }
64
65
    public function readStream()
66
    {
67
        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...
68
    }
69
70
    public function delete()
71
    {
72
        $this->disk->delete($this->fileName);
73
    }
74
75
    public function createdAt(): Carbon
76
    {
77
        $timestamp = $this->disk->lastModified($this->fileName);
78
79
        return Carbon::createFromTimestamp($timestamp);
80
    }
81
82
    protected function getNameParts(): array
83
    {
84
        $baseName = pathinfo($this->fileName, PATHINFO_FILENAME);
85
86
        $nameParts = explode('---', $baseName);
87
88
        $projectorName =  str_replace('+', '\\', $nameParts[1]);
89
90
        $lastProcessedEventId = (int) $nameParts[2];
91
92
        $name =  $nameParts[3] ?? '';
93
94
        return compact('projectorName', 'lastProcessedEventId', 'name');
95
    }
96
}