Completed
Push — master ( d88eff...7995d8 )
by Petre
02:16
created

SMAirbrakeExtension::configureEnvironment()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 2
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
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
34
        $loader->load('services.yml');
35
36
        foreach ($config as $configKey => $configValue) {
37
            $container->setParameter("sm_airbrake.{$configKey}", $configValue);
38
        }
39
40
        $this->configureRootDirectory($container, $config);
41
        $this->configureEnvironment($container, $config);
42
        $this->configureAppVersion($container, $config);
43
    }
44
45
    /**
46
     * Get the application version from the VERSION file in the root directory.
47
     *
48
     * @param ContainerBuilder $container
49
     *
50
     * @return string
51
     */
52
    private function getAppVersion(ContainerBuilder $container): string
53
    {
54
        $kernelRootDir = dirname($container->getParameter('kernel.root_dir'));
55
56
        if (false === file_exists("{$kernelRootDir}/VERSION")) {
57
            return AirbrakeDefaultEnum::APP_VERSION;
58
        }
59
60
        return file_get_contents("$kernelRootDir/VERSION");
61
    }
62
63
    /**
64
     * Configure the root directory parameter.
65
     *
66
     * @param ContainerBuilder $container
67
     * @param array            $config
68
     */
69
    protected function configureRootDirectory(ContainerBuilder $container, array $config)
70
    {
71
        if (AirbrakeDefaultEnum::ROOT_DIRECTORY === $config['root_directory']) {
72
            $container->setParameter(
73
                'sm_airbrake.root_directory',
74
                dirname($container->getParameter('kernel.root_dir'))
75
            );
76
        }
77
    }
78
79
    /**
80
     * Configure the environment parameter.
81
     *
82
     * @param ContainerBuilder $container
83
     * @param array            $config
84
     */
85
    protected function configureEnvironment(ContainerBuilder $container, array $config)
86
    {
87
        if (AirbrakeDefaultEnum::ENVIRONMENT === $config['environment']
88
            && $container->hasParameter('app.environment')
89
        ) {
90
            $container->setParameter(
91
                'sm_airbrake.environment',
92
                $container->getParameter('app.environment')
93
            );
94
        }
95
    }
96
97
    /**
98
     * Configure the application version parameter.
99
     *
100
     * @param ContainerBuilder $container
101
     * @param array $config
102
     */
103
    protected function configureAppVersion(ContainerBuilder $container, array $config)
104
    {
105
        if (AirbrakeDefaultEnum::APP_VERSION === $config['app_version']) {
106
            $container->setParameter(
107
                'sm_airbrake.app_version',
108
                $this->getAppVersion($container)
109
            );
110
        }
111
    }
112
}
113