Completed
Pull Request — master (#90)
by Arnaud
02:11
created

ApplicationConfigurationStorage::__construct()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace LAG\AdminBundle\Application\Configuration;
4
5
use Exception;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
/**
9
 * Store the application configuration object to be used in other parts of the application.
10
 */
11
class ApplicationConfigurationStorage
12
{
13
    /**
14
     * @var ApplicationConfiguration
15
     */
16
    private $configuration;
17
    
18
    /**
19
     * @var bool
20
     */
21
    private $frozen = false;
22
    
23
    /**
24
     * ApplicationConfigurationStorage constructor.
25
     *
26
     * Define the application configuration. Once the configuration is defined, it can not be defined anymore. An
27
     * exception will be thrown if the set method is called a second time.
28
     *
29
     * @param array $configuration
30
     */
31
    public function __construct(array $configuration = [])
32
    {
33
        // resolve the application configuration array in to a configuration object
34
        $resolver = new OptionsResolver();
35
        $applicationConfiguration = new ApplicationConfiguration();
36
        $applicationConfiguration->configureOptions($resolver);
37
        $applicationConfiguration->setParameters($resolver->resolve($configuration));
38
    
39
        $this->configuration = $applicationConfiguration;
40
        $this->frozen = true;
41
    }
42
    
43
    /**
44
     * Return the application configuration. If the none is defined yet, an exception will be thrown.
45
     *
46
     * @return ApplicationConfiguration
47
     *
48
     * @throws Exception
49
     */
50
    public function getApplicationConfiguration()
51
    {
52
        if (null === $this->configuration) {
53
            throw new Exception('The application configuration has not been set');
54
        }
55
        
56
        return $this->configuration;
57
    }
58
    
59
    /**
60
     * Return true is the configuration is defined and the storage frozen.
61
     *
62
     * @return bool
63
     */
64
    public function isFrozen()
65
    {
66
        return $this->frozen;
67
    }
68
}
69