Manifest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\Tasks\Backup;
4
5
use Countable;
6
use SplFileObject;
7
8
class Manifest implements Countable
9
{
10
    /** @var string */
11
    protected $manifestPath;
12
13
    public static function create(string $manifestPath): self
14
    {
15
        return new static($manifestPath);
16
    }
17
18
    public function __construct(string $manifestPath)
19
    {
20
        $this->manifestPath = $manifestPath;
21
22
        touch($manifestPath);
23
    }
24
25
    public function path(): string
26
    {
27
        return $this->manifestPath;
28
    }
29
30
    /**
31
     * @param array|string $filePaths
32
     *
33
     * @return $this
34
     */
35
    public function addFiles($filePaths): self
36
    {
37
        if (is_string($filePaths)) {
38
            $filePaths = [$filePaths];
39
        }
40
41
        foreach ($filePaths as $filePath) {
42
            if (! empty($filePath)) {
43
                file_put_contents($this->manifestPath, $filePath.PHP_EOL, FILE_APPEND);
44
            }
45
        }
46
47
        return $this;
48
    }
49
50
    /**
51
     * @return \Generator|string[]
52
     */
53
    public function files()
54
    {
55
        $file = new SplFileObject($this->path());
56
57
        while (! $file->eof()) {
58
            $filePath = $file->fgets();
59
60
            if (! empty($filePath)) {
61
                yield trim($filePath);
62
            }
63
        }
64
    }
65
66
    public function count(): int
67
    {
68
        $file = new SplFileObject($this->manifestPath, 'r');
69
70
        $file->seek(PHP_INT_MAX);
71
72
        return $file->key();
73
    }
74
}
75