SentryPass   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 34
rs 10
c 1
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfiguration() 0 3 1
A configureHub() 0 13 1
A process() 0 12 1
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\DependencyInjection\Compiler;
17
18
use FRZB\Component\MetricsPower\DependencyInjection\Configuration;
19
use Sentry\ClientBuilder;
20
use Sentry\ClientInterface;
21
use Sentry\Options;
22
use Sentry\State\HubAdapter;
23
use Sentry\State\HubInterface;
24
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
25
use Symfony\Component\DependencyInjection\ContainerBuilder;
26
use Symfony\Component\DependencyInjection\Definition;
27
28
final class SentryPass implements CompilerPassInterface
29
{
30
    public function process(ContainerBuilder $container): void
31
    {
32
        $config = self::getConfiguration($container);
33
        $isPublic = (bool) ($config['test'] ?? null);
34
        $hubDefinition = self::configureHub($config);
35
36
        $container
37
            ->setDefinition(HubAdapter::class, $hubDefinition)
38
            ->setPublic($isPublic);
39
40
        $container
41
            ->setAlias(HubInterface::class, HubAdapter::class);
42
    }
43
44
    public static function configureHub(array $config): Definition
45
    {
46
        $isPublic = (bool) ($config['test'] ?? null);
47
        $clientOptions = (new Definition(Options::class, [$config]))->setPublic($isPublic);
48
        $clientBuilder = (new Definition(ClientBuilder::class, [$clientOptions]))->setPublic($isPublic);
49
        $client = (new Definition(ClientInterface::class))
50
            ->setPublic($isPublic)
51
            ->setFactory([$clientBuilder, 'getClient']);
52
53
        return (new Definition(HubInterface::class))
54
            ->setFactory([HubAdapter::class, 'getInstance'])
55
            ->addMethodCall('bindClient', [$client])
56
            ->setPublic($isPublic);
57
    }
58
59
    private static function getConfiguration(ContainerBuilder $container): array
60
    {
61
        return (array) $container->getParameter(Configuration::METRICS_POWER_CONFIG)[Configuration::SENTRY_CONFIG];
62
    }
63
}
64