Passed
Push — master ( a6853f...a735f8 )
by Aleksei
31:53 queued 20:52
created

FileSnapshot::saveSnapshot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Snapshots;
6
7
use Spiral\Exceptions\ExceptionRendererInterface;
8
use Spiral\Exceptions\Verbosity;
9
use Spiral\Files\Exception\FilesException;
10
use Spiral\Files\FilesInterface;
11
use Symfony\Component\Finder\Finder;
12
use Symfony\Component\Finder\SplFileInfo;
13
14
class FileSnapshot
15
{
16 6
    public function __construct(
17
        private readonly string $directory,
18
        private readonly int $maxFiles,
19
        private readonly Verbosity $verbosity,
20
        private readonly ExceptionRendererInterface $renderer,
21
        private readonly FilesInterface $files,
22 6
    ) {}
23
24 3
    public function create(\Throwable $e): SnapshotInterface
25
    {
26 3
        $snapshot = new Snapshot($this->getID($e), $e);
27
28 3
        $this->saveSnapshot($snapshot);
29 3
        $this->rotateSnapshots();
30
31 3
        return $snapshot;
32
    }
33
34 3
    protected function saveSnapshot(SnapshotInterface $snapshot): void
35
    {
36 3
        $filename = $this->getFilename($snapshot, new \DateTime());
37
38 3
        $this->files->write(
39 3
            $filename,
40 3
            $this->renderer->render($snapshot->getException(), $this->verbosity),
41 3
            FilesInterface::RUNTIME,
42 3
            true,
43 3
        );
44
    }
45
46
    /**
47
     * Remove older snapshots.
48
     */
49 3
    protected function rotateSnapshots(): void
50
    {
51 3
        $finder = new Finder();
52 3
        $finder->in($this->directory)->sort(
53 3
            static fn(SplFileInfo $a, SplFileInfo $b): int|float => $b->getMTime() - $a->getMTime(),
54 3
        );
55
56 3
        $count = 0;
57 3
        foreach ($finder as $file) {
58 3
            $count++;
59 3
            if ($count > $this->maxFiles) {
60
                try {
61 1
                    $this->files->delete($file->getRealPath());
62
                } catch (FilesException) {
63
                    // ignore
64
                }
65
            }
66
        }
67
    }
68
69
    /**
70
     * @throws \Exception
71
     */
72 3
    protected function getFilename(SnapshotInterface $snapshot, \DateTimeInterface $time): string
73
    {
74 3
        return \sprintf(
75 3
            '%s/%s-%s.txt',
76 3
            $this->directory,
77 3
            $time->format('d.m.Y-Hi.s'),
78 3
            (new \ReflectionClass($snapshot->getException()))->getShortName(),
79 3
        );
80
    }
81
82 3
    protected function getID(\Throwable $e): string
83
    {
84 3
        return \md5(\implode('|', [$e->getMessage(), $e->getFile(), $e->getLine()]));
85
    }
86
}
87