AbstractKernel::setResolvedOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Axstrad Library
4
 *
5
 * Copyright (c) 2015 Dan Kempster
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * Feel free to edit as you please, and have fun.
11
 *
12
 * @author Dan Kempster <[email protected]>
13
 */
14
15
namespace Axstrad\Bundle\UseCaseTestBundle\Kernel;
16
17
use Axstrad\Bundle\UseCaseTestBundle\Test\MultiUseCaseTest;
18
use Symfony\Component\Config\Loader\LoaderInterface;
19
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
22
23
/**
24
 * Axstrad\Bundle\UseCaseTestBundle\Kernel\AbstractKernel
25
 */
26
abstract class AbstractKernel extends BaseKernel
27
{
28
    /**
29
     * @var null|string The path to the test's temp dir. A unique dir to this
30
     *      use case.
31
     */
32
    private $testTempDir = null;
33
34
    /**
35
     * The class options passed during class construction.
36
     *
37
     * @var array
38
     */
39
    protected $options = array();
40
41 1
    public function getUseCase()
42
    {
43 1
        return $this->options['use_case'];
44
    }
45
46
47
    /**
48
     * Class constructor
49
     *
50
     * @param string $environment The app's run environment
51
     * @param boolean $debug Should the app run in debug mode
52
     * @param array $options Options for the Kernel. Allowed options are:
53
     *     - use_case : The current use case
54
     *     - root_dir : The app's root directory
55
     *
56
     * @uses configureOptions To configure an {@link
57
     *       http://symfony.com/doc/current/components/options_resolver.html
58
     *       OptionsResolver} to resolve $options
59
     * @uses setResolvedOptions To set the options after they've been resolved.
60
     */
61 9
    public function __construct($environment, $debug, array $options)
62
    {
63 9
        $resolver = new OptionsResolver;
64 9
        static::configureOptions($resolver);
65 9
        $this->setResolvedOptions(
66 9
            $resolver->resolve($options)
67 9
        );
68
69 9
        parent::__construct($environment, $debug);
70 9
    }
71
72
    /**
73
     * Configures $resolver for the $options passed to the {@see __construct
74
     * constructor}.
75
     */
76 12
    public static function configureOptions(OptionsResolverInterface $resolver)
77
    {
78
        $resolver
79 12
            ->setRequired(array(
80 12
                'use_case',
81 12
                'tmp_dir',
82 12
            ))
83 12
            ->setDefaults(array(
84 12
                'use_case' => 'default',
85 12
                'tmp_dir' => sys_get_temp_dir(),
86 12
            ))
87 12
            ->setOptional(array(
88
                'root_dir'
89 12
            ))
90
            // Not Symfony 2.3 compatable
91
            //
92
            // ->setAllowedValues(array(
93
            //     'root_dir' => function ($value) {
94
            //         return empty($value) || is_dir($value);
95
            //     }
96
            // ))
97
        ;
98 12
    }
99
100 9
    protected function setResolvedOptions(array $options)
101
    {
102 9
        $this->options = $options;
103 9
    }
104
105
    /**
106
     */
107 9
    public function getRootDir()
108
    {
109 9
        if (null === $this->rootDir) {
110 9
            if (!empty($this->options['root_dir'])) {
111 7
                $this->rootDir = $this->options['root_dir'];
112 7
            }
113
            else {
114 2
                $this->rootDir = MultiUseCaseTest::getAbsoluteUseCasesDir().
115 2
                    DIRECTORY_SEPARATOR.
116 2
                    $this->options['use_case']
117 2
                ;
118
            }
119 9
        }
120 9
        return $this->rootDir;
121
    }
122
123
    /**
124
     */
125 9
    public function registerBundles()
126
    {
127 5
        return include $this->rootDir.DIRECTORY_SEPARATOR.'bundles.php';
128 9
    }
129
130
    /**
131
     * Register container configuration
132
     *
133
     * @param LoaderInterface $loader Loader
134
     */
135 4
    public function registerContainerConfiguration(LoaderInterface $loader)
136
    {
137 4
        $loader->load($this->rootDir.DIRECTORY_SEPARATOR.'config.yml');
138 4
    }
139
140 6
    public function getTestTempDir()
141
    {
142 6
        if ($this->testTempDir === null) {
143 6
            $this->testTempDir = implode(DIRECTORY_SEPARATOR, array(
144 6
                $this->options['tmp_dir'],
145 6
                self::VERSION,
146 6
                'AxstradUseCaseTestBundleCache',
147 6
                $this->options['use_case'].'-'.$this->getEnvironment(),
148 6
            ));
149 6
        }
150
151 6
        return $this->testTempDir;
152
    }
153
154
    /**
155
     * Return Cache dir
156
     *
157
     * @return string
158
     * @uses getTestTempDir
159
     */
160 6
    public function getCacheDir()
161
    {
162 6
        return $this->getTestTempDir().DIRECTORY_SEPARATOR.'Cache';
163
    }
164
165
    /**
166
     * Return log dir
167
     *
168
     * @return string
169
     * @uses getTestTempDir
170
     */
171 4
    public function getLogDir()
172
    {
173 4
        return $this->getTestTempDir().DIRECTORY_SEPARATOR.'Log';
174
    }
175
}
176