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

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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\Messenger\Event\WorkerMessageFailedEvent;
24
use Symfony\Component\Serializer\Encoder\JsonEncoder;
25
use Symfony\Component\Serializer\SerializerInterface;
26
27
/**
28
 * @implements ContextExtractorInterface<WorkerMessageFailedEvent, OptionsInterface>
29
 */
30
#[AsService, AsTagged(ContextExtractorInterface::class)]
31
final class WorkerMessageFailedEventContextExtractor implements ContextExtractorInterface
32
{
33
    private const MESSAGE = '[MetricsPower] [ERROR] [MESSAGE: Handle failed] [TARGET_CLASS: {target_class}] [OPTIONS_CLASS: {options_class}] [MESSAGE_CLASS: {message_class}] [EXCEPTION_CLASS: {exception_class}] [EXCEPTION_MESSAGE: {exception_message}] [OPTIONS_VALUES: {option_values}]';
34
35
    public function __construct(
36
        private readonly SerializerInterface $serializer,
37
    ) {}
38
39
    public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context
40
    {
41
        $context = [
42
            'target_class' => ClassHelper::getShortName($target),
43
        ];
44
45
        if ($options?->isSerializable()) {
46
            $context += ['target_values' => $this->serializer->serialize($target->getEnvelope()->getMessage(), JsonEncoder::FORMAT)];
47
        }
48
49
        if ($options) {
50
            $context += [
51
                'options_class' => ClassHelper::getShortName($options),
52
                'option_values' => ClassHelper::getProperties($options),
53
            ];
54
        }
55
56
        if ($exception) {
57
            $context += [
58
                'exception_class' => ClassHelper::getShortName($exception),
59
                'exception_message' => $exception->getMessage(),
60
                'exception_trace' => $exception->getTrace(),
61
            ];
62
        }
63
64
        return new Context(self::MESSAGE, $context);
65
    }
66
67
    public static function getType(): string
68
    {
69
        return WorkerMessageFailedEvent::class;
70
    }
71
}
72