Passed
Push — master ( 2ee547...bfef22 )
by Aleksei
13:34
created

FileSnapshot::rotateSnapshots()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

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