Completed
Push — master ( 530541...84f331 )
by Freek
09:07 queued 06:30
created

Snapshot::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
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\Exceptions\InvalidSnapshot;
9
use Spatie\EventProjector\Models\StoredEvent;
10
11
class Snapshot
12
{
13
    /** @var \Spatie\EventProjector\EventProjectionist */
14
    protected $eventProjectionist;
15
16
    /** @var \Illuminate\Contracts\Filesystem\Filesystem */
17
    protected $disk;
18
19
    /** @var string */
20
    protected $fileName;
21
22
    public function __construct(
23
        EventProjectionist $eventProjectionist,
24
        array $config,
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
        Filesystem $disk,
26
        string $fileName)
27
    {
28
        $this->eventProjectionist = $eventProjectionist;
29
30
        $this->disk = $disk;
31
32
        $this->fileName = $fileName;
33
    }
34
35
    public function isValid(): bool
36
    {
37
        return $this->projectorName() !== '';
38
    }
39
40
    public function fileName(): string
41
    {
42
        return $this->fileName;
43
    }
44
45
    public function lastProcessedEventId(): int
46
    {
47
        return $this->fileNameParts()['lastProcessedEventId'];
48
    }
49
50
    public function lastProcessedEvent(): StoredEvent
51
    {
52
        $storedEventModelClass = $this->config['stored_event_model'];
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
54
        $storedEvent = $storedEventModelClass::query()
55
            ->where('last_processed_event_id', $this->lastProcessedEventId())
56
            ->first();
57
58
        if (! $storedEvent) {
59
            throw InvalidSnapshot::lastProcessedEventDoesNotExist($this);
60
        }
61
62
        return $storedEvent;
63
    }
64
65
    public function projectorName(): string
66
    {
67
        return $this->fileNameParts()['projectorName'];
68
    }
69
70
    public function projector(): Snapshottable
71
    {
72
        $projectorName = $this->projectorName();
73
74
        if (! $projector = $this->eventProjectionist->getProjector($projectorName)) {
75
            throw InvalidSnapshot::projectorDoesNotExist($this);
76
        }
77
78
        return $projector;
79
    }
80
81
    public function name(): string
82
    {
83
        return $this->fileNameParts()['name'];
84
    }
85
86
    /**
87
     * @param string|resource $contents
88
     */
89
    public function write($contents)
90
    {
91
        $this->disk->put($this->fileName, $contents);
92
    }
93
94
    public function read(): ?string
95
    {
96
        return $this->disk->get($this->fileName);
97
    }
98
99
    public function readStream()
100
    {
101
        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...
102
    }
103
104
    public function restore(): self
105
    {
106
        $storedEvent = $this->lastProcessedEvent();
107
108
        $this->projector()->restoreSnapshot($this);
109
110
        $this->config['projector_status_model']::rememberLastProcessedEvent($storedEvent);
111
    }
112
113
    public function delete()
114
    {
115
        $this->disk->delete($this->fileName);
116
    }
117
118
    public function createdAt(): Carbon
119
    {
120
        $timestamp = $this->disk->lastModified($this->fileName);
121
122
        return Carbon::createFromTimestamp($timestamp);
123
    }
124
125
    protected function fileNameParts(): array
126
    {
127
        $baseName = pathinfo($this->fileName, PATHINFO_FILENAME);
128
129
        $nameParts = explode('---', $baseName);
130
131
        $projectorName = str_replace('+', '\\', $nameParts[1] ?? '');
132
133
        $lastProcessedEventId = $nameParts[2] ?? 0;
134
135
        $name = $nameParts[3] ?? '';
136
137
        return compact('projectorName', 'lastProcessedEventId', 'name');
138
    }
139
}
140