Passed
Push — main ( e0948b...5456f8 )
by Fractal
02:50
created

DefaultContextExtractor::extract()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
nc 16
nop 3
dl 0
loc 25
rs 9.4888
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\Helper\ClassHelper;
21
use FRZB\Component\MetricsPower\Helper\MetricalHelper;
22
use FRZB\Component\MetricsPower\Logger\Data\LoggerContext;
23
use Symfony\Component\Serializer\Encoder\JsonEncoder;
24
use Symfony\Component\Serializer\SerializerInterface;
25
26
#[AsService, AsTagged(ContextExtractorInterface::class)]
27
final readonly class DefaultContextExtractor implements ContextExtractorInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 27 at column 6
Loading history...
28
{
29
    private const MESSAGE = '[MetricsPower] [INFO] [MESSAGE: Operation succeed] [TARGET_CLASS: {target_class}]';
30
31
    public function __construct(
32
        private SerializerInterface $serializer,
33
    ) {}
34
35
    public function extract(object $target): LoggerContext
36
    {
37
        $context = [
38
            'target_class' => ClassHelper::getShortName($target),
39
        ];
40
41
        if (($options = MetricalHelper::getFirstOptions($target)) && $options?->isSerializable()) {
42
            $context += [
43
                'target_values' => $this->serializer->serialize($target, JsonEncoder::FORMAT),
44
                'options_class' => ClassHelper::getShortName($options),
45
                'options_values' => ClassHelper::getProperties($target),
46
            ];
47
        }
48
49
        return new LoggerContext(self::MESSAGE, $context);
50
    }
51
52
    public static function getType(): string
53
    {
54
        return 'Default';
55
    }
56
}
57