Passed
Push — main ( d2dcdf...52d5ed )
by Fractal
02:24
created

SentryOptionsResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onWorkerMessageFailedEvent() 0 7 3
A getType() 0 3 1
A __invoke() 0 6 1
A onWorkerMessageHandledEvent() 0 3 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\OptionsResolver\Resolver;
17
18
use FRZB\Component\DependencyInjection\Attribute\AsService;
19
use FRZB\Component\DependencyInjection\Attribute\AsTagged;
20
use FRZB\Component\MetricsPower\Attribute\SentryOptions;
21
use Sentry\State\HubInterface;
22
use Symfony\Component\Messenger\Event\AbstractWorkerMessageEvent;
23
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent;
24
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
25
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
26
27
#[AsService, AsTagged(OptionsResolverInterface::class)]
28
class SentryOptionsResolver implements OptionsResolverInterface
29
{
30
    public function __construct(
31
        private readonly HubInterface $hub
32
    ) {}
33
34
    public function __invoke(AbstractWorkerMessageEvent|SendMessageToTransportsEvent $event, SentryOptions $options): void
35
    {
36
        match ($event::class) {
37
            WorkerMessageFailedEvent::class => $this->onWorkerMessageFailedEvent($event, $options),
0 ignored issues
show
Bug introduced by
It seems like $event can also be of type Symfony\Component\Messen...essageToTransportsEvent; however, parameter $event of FRZB\Component\MetricsPo...kerMessageFailedEvent() does only seem to accept Symfony\Component\Messen...orkerMessageFailedEvent, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
            WorkerMessageFailedEvent::class => $this->onWorkerMessageFailedEvent(/** @scrutinizer ignore-type */ $event, $options),
Loading history...
Bug introduced by
Are you sure the usage of $this->onWorkerMessageFa...Event($event, $options) targeting FRZB\Component\MetricsPo...kerMessageFailedEvent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38
            WorkerMessageHandledEvent::class => $this->onWorkerMessageHandledEvent($event, $options),
0 ignored issues
show
Bug introduced by
It seems like $event can also be of type Symfony\Component\Messen...essageToTransportsEvent; however, parameter $event of FRZB\Component\MetricsPo...erMessageHandledEvent() does only seem to accept Symfony\Component\Messen...rkerMessageHandledEvent, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            WorkerMessageHandledEvent::class => $this->onWorkerMessageHandledEvent(/** @scrutinizer ignore-type */ $event, $options),
Loading history...
Bug introduced by
Are you sure the usage of $this->onWorkerMessageHa...Event($event, $options) targeting FRZB\Component\MetricsPo...erMessageHandledEvent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
39
            default => null,
40
        };
41
    }
42
43
    public static function getType(): string
44
    {
45
        return SentryOptions::class;
46
    }
47
48
    private function onWorkerMessageFailedEvent(WorkerMessageFailedEvent $event, SentryOptions $options): void
49
    {
50
        if ($event->willRetry() && $options->waitRetry) {
51
            return;
52
        }
53
54
        $this->hub->captureException($event->getThrowable());
55
    }
56
57
    private function onWorkerMessageHandledEvent(WorkerMessageHandledEvent $event, SentryOptions $options): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

57
    private function onWorkerMessageHandledEvent(/** @scrutinizer ignore-unused */ WorkerMessageHandledEvent $event, SentryOptions $options): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        $options->onHandleFlush && $this->hub->getClient()?->flush();
60
    }
61
}
62