Passed
Pull Request — master (#813)
by Alexander
06:06
created

FileSnapshot   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 27
c 1
b 0
f 0
dl 0
loc 72
ccs 26
cts 27
cp 0.963
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A saveSnapshot() 0 9 1
A getID() 0 3 1
A create() 0 8 1
A getFilename() 0 7 1
A __construct() 0 7 1
A rotateSnapshots() 0 14 4
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Finder\SplFileInfo;
13
14
class FileSnapshot
15
{
16 6
    public function __construct(
17
        private string $directory,
18
        private int $maxFiles,
19
        private Verbosity $verbosity,
20
        private ExceptionRendererInterface $renderer,
21
        private FilesInterface $files
22
    ) {
23
    }
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
            $filename,
41 3
            $this->renderer->render($snapshot->getException(), $this->verbosity),
42
            FilesInterface::RUNTIME,
43
            true
44
        );
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) => $b->getMTime() - $a->getMTime()
55
        );
56
57 3
        $count = 0;
58 3
        foreach ($finder as $file) {
59 3
            $count++;
60 3
            if ($count > $this->maxFiles) {
61
                try {
62 1
                    $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
            '%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
        );
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