Completed
Push — master ( bc82fc...a71d24 )
by Petre
04:23
created

SMAirbrakeExtension::loadServiceDefinitions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SM\AirbrakeBundle\DependencyInjection;
5
6
use SM\AirbrakeBundle\Enum\AirbrakeDefaultEnum;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10
use Symfony\Component\DependencyInjection\Loader;
11
12
/**
13
 * Defines the service container configuration for the bundle.
14
 *
15
 * @package SM\AirbrakeBundle\DependencyInjection
16
 * @author  Petre Pătrașc <[email protected]>
17
 */
18
class SMAirbrakeExtension extends Extension
19
{
20
    /**
21
     * Loads a specific configuration.
22
     *
23
     * @param array            $configs   An array of configuration values
24
     * @param ContainerBuilder $container A ContainerBuilder instance
25
     *
26
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
27
     */
28
    public function load(array $configs, ContainerBuilder $container)
29
    {
30
        $configuration = new Configuration();
31
        $config        = $this->processConfiguration($configuration, $configs);
32
33
        $this->loadServiceDefinitions($container);
34
35
        foreach ($config as $configKey => $configValue) {
36
            $container->setParameter("sm_airbrake.{$configKey}", $configValue);
37
        }
38
39
        $this->configureRootDirectory($container, $config);
40
        $this->configureEnvironment($container, $config);
41
        $this->configureAppVersion($container, $config);
42
    }
43
44
    /**
45
     * Get the application version from the VERSION file in the root directory.
46
     *
47
     * @param ContainerBuilder $container
48
     *
49
     * @return string
50
     */
51
    private function getAppVersion(ContainerBuilder $container): string
52
    {
53
        $kernelRootDir = dirname($container->getParameter('kernel.root_dir'));
54
55
        if (false === file_exists("{$kernelRootDir}/VERSION")) {
56
            return AirbrakeDefaultEnum::APP_VERSION;
57
        }
58
59
        return file_get_contents("$kernelRootDir/VERSION");
60
    }
61
62
    /**
63
     * Configure the root directory parameter.
64
     *
65
     * @param ContainerBuilder $container
66
     * @param array            $config
67
     */
68
    protected function configureRootDirectory(ContainerBuilder $container, array $config)
69
    {
70
        if (AirbrakeDefaultEnum::ROOT_DIRECTORY === $config['root_directory']) {
71
            $container->setParameter(
72
                'sm_airbrake.root_directory',
73
                dirname($container->getParameter('kernel.root_dir'))
74
            );
75
        }
76
    }
77
78
    /**
79
     * Configure the environment parameter.
80
     *
81
     * @param ContainerBuilder $container
82
     * @param array            $config
83
     */
84
    protected function configureEnvironment(ContainerBuilder $container, array $config)
85
    {
86
        if (AirbrakeDefaultEnum::ENVIRONMENT === $config['environment']
87
            && $container->hasParameter('app.environment')
88
        ) {
89
            $container->setParameter(
90
                'sm_airbrake.environment',
91
                $container->getParameter('app.environment')
92
            );
93
        }
94
    }
95
96
    /**
97
     * Configure the application version parameter.
98
     *
99
     * @param ContainerBuilder $container
100
     * @param array            $config
101
     */
102
    protected function configureAppVersion(ContainerBuilder $container, array $config)
103
    {
104
        if (AirbrakeDefaultEnum::APP_VERSION === $config['app_version']) {
105
            $container->setParameter(
106
                'sm_airbrake.app_version',
107
                $this->getAppVersion($container)
108
            );
109
        }
110
    }
111
112
    /**
113
     * Load the service definitions.
114
     *
115
     * @param ContainerBuilder $container
116
     */
117
    protected function loadServiceDefinitions(ContainerBuilder $container)
118
    {
119
        $loader          = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
120
        $definitionFiles = [
121
            'services.yml',
122
            'listeners.yml',
123
        ];
124
        foreach ($definitionFiles as $file) {
125
            $loader->load($file);
126
        }
127
    }
128
}
129