Passed
Push — main ( 1bfc55...29d6ba )
by Fractal
02:09
created

ExceptionEventContextExtractor   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 25
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A extract() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2024 Mykhailo Shtanko [email protected]
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15
16
namespace FRZB\Component\MetricsPower\Logger\ContextExtractor;
17
18
use FRZB\Component\DependencyInjection\Attribute\AsService;
19
use FRZB\Component\DependencyInjection\Attribute\AsTagged;
20
use FRZB\Component\MetricsPower\Attribute\OptionsInterface;
21
use FRZB\Component\MetricsPower\Helper\ClassHelper;
22
use FRZB\Component\MetricsPower\Logger\Data\Context;
23
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
24
25
/**
26
 * @implements ContextExtractorInterface<ExceptionEvent, OptionsInterface>
27
 */
28
#[AsService, AsTagged(ContextExtractorInterface::class)]
29
class ExceptionEventContextExtractor implements ContextExtractorInterface
30
{
31
    private const MESSAGE = '[MetricsPower] [ERROR] [MESSAGE: Operation failed] [TARGET_CLASS: {target_class}] [EXCEPTION_CLASS: {exception_class}] [EXCEPTION_MESSAGE: {exception_message}]';
32
33
    public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context
34
    {
35
        $context = [
36
            'target_class' => ClassHelper::getShortName($target),
37
        ];
38
39
        if ($exception) {
40
            $context += [
41
                'exception_class' => ClassHelper::getShortName($target->getThrowable()),
42
                'exception_message' => $target->getThrowable()->getMessage(),
43
                'exception_trace' => $target->getThrowable()->getTrace(),
44
            ];
45
        }
46
47
        return new Context(self::MESSAGE, $context);
48
    }
49
50
    public static function getType(): string
51
    {
52
        return ExceptionEvent::class;
53
    }
54
}
55