LogRef::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Error;
6
7
use BEAR\AppMeta\AbstractAppMeta;
8
use BEAR\Sunday\Extension\Router\RouterMatch;
9
use Throwable;
10
11
use function file_put_contents;
12
use function get_class;
13
use function is_link;
14
use function is_writable;
15
use function sprintf;
16
use function symlink;
17
use function unlink;
18
19
final class LogRef
20
{
21
    /** @var string */
22
    private $ref;
23
24
    public function __construct(Throwable $e)
25
    {
26
        // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
27
        $this->ref = hash('crc32b', get_class($e) . $e->getMessage() . $e->getFile() . $e->getLine());
28
    }
29
30
    public function __toString(): string
31
    {
32
        return $this->ref;
33
    }
34
35
    public function log(Throwable $e, RouterMatch $request, AbstractAppMeta $appMeta): void
36
    {
37
        $logRefFile = sprintf('%s/logref.%s.log', $appMeta->logDir, $this->ref);
38
        $log = (string) new ExceptionAsString($e, $request);
39
        @file_put_contents($logRefFile, $log);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
40
        $linkFile = sprintf('%s/last.logref.log', $appMeta->logDir);
41
        is_link($linkFile) && is_writable($linkFile) && @unlink($linkFile);
42
        @symlink($logRefFile, $linkFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
43
    }
44
}
45