• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fractalzombie / frzb-metrics-power / 8464240914

28 Mar 2024 08:06AM UTC coverage: 85.185% (-4.5%) from 89.706%
8464240914

push

github

fractalzombie
[MetricsPower] Added ContextExtractor for logger

79 of 98 new or added lines in 16 files covered. (80.61%)

1 existing line in 1 file now uncovered.

184 of 216 relevant lines covered (85.19%)

8.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.22
/Logger/ContextExtractor/DefaultContextExtractor.php
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 fractalzombie@gmail.com
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\Serializer\Encoder\JsonEncoder;
24
use Symfony\Component\Serializer\SerializerInterface;
25

26
/**
27
 * @implements ContextExtractorInterface<object, OptionsInterface>
28
 */
29
#[AsService, AsTagged(ContextExtractorInterface::class)]
30
class DefaultContextExtractor implements ContextExtractorInterface
31
{
32
    public const DEFAULT_TYPE = 'Default';
33
    private const MESSAGE_INFO = '[MetricsPower] [INFO] [MESSAGE: Operation succeed] [OPTIONS_CLASS: {options_class}] [TARGET_CLASS: {target_class}]';
34
    private const MESSAGE_ERROR = '[MetricsPower] [ERROR] [MESSAGE: Operation failed] [OPTIONS_CLASS: {options_class}] [MESSAGE_CLASS: {message_class}] [EXCEPTION_CLASS: {exception_class}] [EXCEPTION_MESSAGE: {exception_message}] [OPTIONS_VALUES: {option_values}]';
35

36
    public function __construct(
37
        private readonly SerializerInterface $serializer,
38
    ) {}
3✔
39

40
    public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context
41
    {
42
        $message = $exception ? self::MESSAGE_ERROR : self::MESSAGE_INFO;
3✔
43
        $context = ['target_class' => ClassHelper::getShortName($target)];
3✔
44

45
        if ($options?->isSerializable()) {
3✔
46
            $context += ['target_values' => $this->serializer->serialize($target, JsonEncoder::FORMAT)];
3✔
47
        }
48

49
        if ($options) {
3✔
50
            $context += [
3✔
51
                'options_class' => ClassHelper::getShortName($options),
3✔
52
                'option_values' => ClassHelper::getProperties($options),
3✔
53
            ];
3✔
54
        }
55

56
        if ($exception) {
3✔
NEW
57
            $context += [
×
NEW
58
                'exception_class' => ClassHelper::getShortName($exception),
×
NEW
59
                'exception_message' => $exception->getMessage(),
×
NEW
60
                'exception_trace' => $exception->getTrace(),
×
NEW
61
            ];
×
62
        }
63

64
        return new Context($message, $context);
3✔
65
    }
66

67
    public static function getType(): string
68
    {
69
        return self::DEFAULT_TYPE;
6✔
70
    }
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc