Passed
Pull Request — master (#19)
by
unknown
02:53
created

resolveIntegrationsFromUserConfig()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.728

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
nc 20
nop 0
dl 0
loc 57
ccs 14
cts 20
cp 0.7
crap 9.728
rs 8.1475
c 3
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Sentry;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Log\LoggerInterface;
9
use RuntimeException;
10
use Sentry\ClientBuilder;
11
use Sentry\Integration as SdkIntegration;
12
use Sentry\Integration\IntegrationInterface;
13
use Sentry\Options;
14
use Sentry\SentrySdk;
15
use Sentry\State\HubInterface;
16
use Sentry\Transport\TransportFactoryInterface;
17
use Yiisoft\Yii\Sentry\Http\YiiRequestFetcher;
18
use Yiisoft\Yii\Sentry\Integration\ExceptionContextIntegration;
19
use Yiisoft\Yii\Sentry\Integration\Integration;
20
21
use function is_string;
22
23
final class HubBootstrapper
24
{
25
    public const DEFAULT_INTEGRATIONS = [
26
        ExceptionContextIntegration::class
27
    ];
28
29 13
    public function __construct(
30
        private Options $options,
31
        private YiiSentryConfig $configuration,
32
        private TransportFactoryInterface $transportFactory,
33
        private LoggerInterface $logger,
34
        private HubInterface $hub,
35
        private ContainerInterface $container,
36
    ) {
37
    }
38
39 13
    public function bootstrap(): void
40
    {
41 13
        $this->options->setIntegrations(fn (array $integrations) => $this->prepareIntegrations($integrations));
42
43 13
        $clientBuilder = new ClientBuilder($this->options);
44
        $clientBuilder
45 13
            ->setTransportFactory($this->transportFactory)
46 13
            ->setLogger($this->logger);
47
48 13
        $client = $clientBuilder->getClient();
49
50 13
        $this->hub->bindClient($client);
51 13
        SentrySdk::setCurrentHub($this->hub);
52
    }
53
54
    /**
55
     * @param IntegrationInterface[] $integrations
56
     *
57
     * @return IntegrationInterface[]
58
     */
59 13
    public function prepareIntegrations(array $integrations): array
60
    {
61 13
        $userIntegrations = $this->resolveIntegrationsFromUserConfig();
62 13
        if (!$this->options->hasDefaultIntegrations()) {
63
            return array_merge($integrations, $userIntegrations);
64
        }
65
66 13
        $integrations = array_filter(
67
            $integrations,
68 13
            static function (SdkIntegration\IntegrationInterface $integration): bool {
69
                return !(
70 13
                    $integration instanceof SdkIntegration\ErrorListenerIntegration ||
71
                    $integration instanceof SdkIntegration\ExceptionListenerIntegration ||
72
                    $integration instanceof SdkIntegration\FatalErrorListenerIntegration ||
73
                    // We also remove the default request integration so it can be readded after with a Yii3
74
                    // specific request fetcher. This way we can resolve the request from Yii3 instead of
75
                    // constructing it from the global state.
76
                    $integration instanceof SdkIntegration\RequestIntegration
77
                );
78
            }
79
        );
80 13
        $integrations[] = new SdkIntegration\RequestIntegration(
81 13
            new YiiRequestFetcher($this->container)
82
        );
83
84 13
        return array_merge($integrations, $userIntegrations);
85
    }
86
87
    /**
88
     * Resolve the integrations from the user configuration with the container.
89
     *
90
     * @return SdkIntegration\IntegrationInterface[]
91
     */
92 13
    private function resolveIntegrationsFromUserConfig(): array
93
    {
94
        // Default Sentry SDK integrations
95 13
        $integrations = [
96 13
            new Integration(),
97
        ];
98
99 13
        $integrationsToResolve = $this->configuration->getIntegrations();
100
101 13
        $enableDefaultTracingIntegrations = array_key_exists('default_integrations', $this->configuration->getTracing())
102 11
            && (bool)$this->configuration->getTracing()['default_integrations'];
103
104
        if (
105 13
            $enableDefaultTracingIntegrations
106 13
            && $this->configuration->couldHavePerformanceTracingEnabled()
107
        ) {
108 1
            $integrationsToResolve = array_merge(
109
                $integrationsToResolve,
110
                self::DEFAULT_INTEGRATIONS
111
            );
112
        }
113
        /** @psalm-suppress MixedAssignment */
114 13
        foreach ($integrationsToResolve as $userIntegration) {
115
            if (
116
                $userIntegration instanceof
117
                SdkIntegration\IntegrationInterface
118
            ) {
119
                $integrations[] = $userIntegration;
120 1
            } elseif (is_string($userIntegration)) {
121
                /** @psalm-suppress MixedAssignment */
122 1
                $resolvedIntegration = $this->container->get($userIntegration);
123
124
                if (
125
                    !$resolvedIntegration instanceof
126
                        SdkIntegration\IntegrationInterface
127
                ) {
128
                    throw new RuntimeException(
129
                        sprintf(
130
                            'Sentry integration must be an instance of `%s` got `%s`.',
131
                            SdkIntegration\IntegrationInterface::class,
132
                            get_debug_type($resolvedIntegration)
133
                        )
134
                    );
135
                }
136
137 1
                $integrations[] = $resolvedIntegration;
138
            } else {
139
                throw new RuntimeException(
140
                    sprintf(
141
                        'Sentry integration must either be a valid container reference or an instance of `%s`.',
142
                        SdkIntegration\IntegrationInterface::class
143
                    )
144
                );
145
            }
146
        }
147
148 13
        return $integrations;
149
    }
150
}
151