Completed
Push — master ( 3cd148...6fd5a8 )
by Freek
01:45 queued 14s
created

SnapshotFactory::createForFile()   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 2
1
<?php
2
3
namespace Spatie\EventProjector\Snapshots;
4
5
use Exception;
6
use Illuminate\Contracts\Filesystem\Filesystem;
7
use Spatie\EventProjector\EventProjectionist;
8
use Spatie\EventProjector\Exceptions\CouldNotCreateSnapshot;
9
10
class SnapshotFactory
11
{
12
    /** @var \Spatie\EventProjector\EventProjectionist */
13
    protected $eventProjectionist;
14
15
    /** @var \Spatie\EventProjector\Snapshots\Filesystem */
16
    protected $disk;
17
18
    public function __construct(EventProjectionist $eventProjectionist, Filesystem $disk)
19
    {
20
        $this->eventProjectionist = $eventProjectionist;
21
22
        $this->disk = $disk;
0 ignored issues
show
Documentation Bug introduced by
It seems like $disk of type object<Illuminate\Contra...\Filesystem\Filesystem> is incompatible with the declared type object<Spatie\EventProje...r\Snapshots\Filesystem> of property $disk.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
    }
24
25
    public function createForProjector(Snapshottable $projector, string $name = ''): Snapshot
26
    {
27
        $lastEventId = $projector->getLastProcessedEventId();
28
29
        $fileName = "Snapshot---{$projector->getName()}---{$lastEventId}---{$name}.txt";
30
31
        $snapshot = new Snapshot(
32
            $this->eventProjectionist,
33
            $this->disk,
34
            $fileName
35
        );
36
37
        try {
38
            $projector->writeToSnapshot($snapshot);
39
        } catch (Exception $exception) {
40
            try {
41
                $snapshot->delete();
42
            } catch (Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
43
            }
44
45
            throw CouldNotCreateSnapshot::projectorThrewExceptionDuringWritingToSnapshot($projector, $exception);
46
        }
47
48
        if (!$this->disk->exists($fileName)) {
49
            throw CouldNotCreateSnapshot::projectorDidNotWriteAnythingToSnapshot($projector);
50
        }
51
52
        if (!static::snapshotIsValid($lastEventId)) {
53
            $snapshot->delete();
54
        }
55
56
        return $snapshot;
57
    }
58
59
    public function createForFile(Filesystem $disk, string $fileName): Snapshot
60
    {
61
        return new Snapshot($this->eventProjectionist, $disk, $fileName);
62
    }
63
64
    protected static function snapshotIsValid(int $lastEventId): bool
0 ignored issues
show
Unused Code introduced by
The parameter $lastEventId 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...
65
    {
66
        return true;
67
    }
68
}