Completed
Push — master ( 6fd5a8...870531 )
by Freek
01:41
created

Snapshot::createForFile()   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 2
1
<?php
2
3
namespace Spatie\EventProjector\Snapshots;
4
5
use Spatie\EventProjector\EventProjectionist;
6
use Illuminate\Contracts\Filesystem\Filesystem;
7
use Spatie\EventProjector\Projectors\Projector;
8
9
class Snapshot
10
{
11
    /** @var \Spatie\EventProjector\EventProjectionist */
12
    protected $eventProjectionist;
13
14
    /** @var \Illuminate\Contracts\Filesystem\Filesystem */
15
    protected $disk;
16
17
    /** @var string */
18
    protected $fileName;
19
20
    public function __construct(EventProjectionist $eventProjectionist, Filesystem $disk, string $fileName)
21
    {
22
        $this->eventProjectionist = $eventProjectionist;
23
24
        $this->disk = $disk;
25
26
        $this->fileName = $fileName;
27
    }
28
29
    public function getLastProcessedEventId(): int
30
    {
31
        return $this->getNameParts()['lastProcessedEventId'];
32
    }
33
34
    public function getProjectorName(): string
35
    {
36
        return $this->getNameParts()['projectorName'];
37
    }
38
39
    public function getProjector(): Projector
40
    {
41
        $projectorName = $this->getProjectorName();
42
43
        return $this->eventProjectionist->getProjector($projectorName);
44
    }
45
46
    public function getName(): string
47
    {
48
        return $this->getNameParts()['name'];
49
    }
50
51
    /**
52
     * @param string|resource $contents
53
     */
54
    public function write($contents)
55
    {
56
        $this->disk->put($this->fileName, $contents);
57
    }
58
59
    public function read(): ?string
60
    {
61
        return $this->disk->get($this->fileName);
62
    }
63
64
    public function readStream()
65
    {
66
        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...
67
    }
68
69
    protected function getNameParts(): array
70
    {
71
        $baseName = pathinfo($this->fileName, PATHINFO_FILENAME);
72
73
        $nameParts = explode('---', $baseName);
74
75
        $projectorName =  $nameParts[1];
76
77
        $lastProcessedEventId = (int) $nameParts[2];
78
79
        $name =  $nameParts[3] ?? '';
80
81
        return compact('projectorName', 'lastProcessedEventId', 'name');
82
    }
83
84
    public function delete()
85
    {
86
        $this->disk->delete($this->fileName);
87
    }
88
}