Completed
Pull Request — master (#3)
by Jacob
02:20
created

DirectoryCompilerPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createDirectory() 0 10 3
A process() 0 10 3
1
<?php
2
3
namespace As3\Bundle\ModlrBundle\DependencyInjection\Compiler;
4
5
use As3\Bundle\ModlrBundle\DependencyInjection\Utility;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
/**
10
 * Creates required directories when applicable.
11
 *
12
 * @author  Jacob Bare <[email protected]>
13
 */
14
class DirectoryCompilerPass implements CompilerPassInterface
15
{
16
    /**
17
     * Creates a directory (if nonexistent).
18
     *
19
     * @param   string  $dir
20
     * @return  self
21
     * @throws  \RuntimeException
22
     */
23
    private function createDirectory($dir)
24
    {
25
        if (file_exists($dir)) {
26
            return $this;
27
        }
28
        if (false === @mkdir($dir, 0777, true)) {
29
            throw new \RuntimeException(sprintf('Unable to create directory "%s"', $dir));
30
        }
31
        return $this;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function process(ContainerBuilder $container)
38
    {
39
        $name = Utility::getAliasedName('dirs');
40
        if (false === $container->hasParameter($name)) {
41
            return;
42
        }
43
        foreach ($container->getParameter($name) as $dir) {
44
            $this->createDirectory($dir);
45
        }
46
    }
47
}
48