Completed
Push — master ( 18d381...23e5bd )
by Freek
01:28
created

Snapshot::createForProjector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Spatie\EventProjector\Snapshots;
4
5
use Illuminate\Contracts\Filesystem\Filesystem;
6
use Spatie\EventProjector\EventProjectionist;
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 static function createForProjector(Projector $projector, string $name = ''): self
21
    {
22
        $lastEventId = $projector->getLastProcessedEventId();
23
24
        $name = "{$projector->getName()}---{$lastEventId}---{$name}.txt";
25
26
        return app(static::class)->setFileName($name);
27
    }
28
29
    public static function createForFile(Filesystem $disk, string $fileName): Snapshot
30
    {
31
        return (new static($disk,$fileName));
0 ignored issues
show
Bug introduced by
The call to Snapshot::__construct() misses a required argument $fileName.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$disk is of type object<Illuminate\Contra...\Filesystem\Filesystem>, but the function expects a object<Spatie\EventProjector\EventProjectionist>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$fileName is of type string, but the function expects a object<Illuminate\Contra...\Filesystem\Filesystem>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
    }
33
34
    public function __construct(EventProjectionist $eventProjectionist, Filesystem $disk, string $fileName)
35
    {
36
        $this->eventProjectionist = $eventProjectionist;
37
38
        $this->disk = $disk;
39
40
        $this->fileName = $fileName;
41
    }
42
43
    public function getLastProcessedEventId(): int
44
    {
45
        return $this->getNameParts()['lastProcessedEventId'];
46
    }
47
48
    public function getProjectorName(): string
49
    {
50
        return $this->getNameParts()['projectorName'];
51
    }
52
53
    public function getProjector(): Projector
54
    {
55
        $projectorName = $this->getProjectorName();
56
57
        return $this->eventProjectionist->getProjector($projectorName);
58
    }
59
60
    public function getName(): string
61
    {
62
        return $this->getNameParts()['name'];
63
    }
64
65
    /**
66
     * @param string|resource $contents
67
     */
68
    public function write($contents)
69
    {
70
        $this->disk->put($this->fileName, $contents);
71
    }
72
73
    public function read(): ?string
74
    {
75
        return $this->disk->get($this->fileName);
76
    }
77
78
    public function readStream()
79
    {
80
        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...
81
    }
82
83
    protected function getNameParts(): array
84
    {
85
        $baseName = pathinfo($this->fileName, PATHINFO_FILENAME);
86
87
        $nameParts = explode('---', $baseName);
88
89
        $projectorName =  $nameParts[0];
90
91
        $lastProcessedEventId = (int) $nameParts[1];
92
93
        $name =  $nameParts[2] ?? '';
94
95
        return compact('projectorName', 'lastProcessedEventId', 'name');
96
    }
97
}