Completed
Push — master ( d401ab...1f0fb6 )
by Łukasz
02:47
created

Application::getRootPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tworzenieweb\SqlProvisioner;
4
5
use RuntimeException;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
10
11
/**
12
 * @author Luke Adamczewski
13
 * @package Tworzenieweb\SqlProvisioner
14
 */
15
class Application extends \Symfony\Component\Console\Application
16
{
17
    const NAME = 'SQL Provisioner';
18
    const VERSION = '0.2.0';
19
20
    /** @var ContainerBuilder */
21
    private $container;
22
23
    /**
24
     * @param string $name
25
     * @param string $version
26
     */
27 1
    public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
28
    {
29 1
        parent::__construct(self::NAME, self::VERSION);
30 1
        $this->boot();
31 1
    }
32
33
34
35 1
    private function boot()
36
    {
37 1
        $this->container = new ContainerBuilder();
38 1
        $this->container->setParameter('sql_provisioner.root_path', $this->getRootPath());
39
40 1
        $loader = new XmlFileLoader($this->container, new FileLocator($this->getConfigPath()));
41 1
        $loader->load('services.xml');
42 1
        $this->registerCommands();
43 1
        $this->registerChecks();
44 1
        $this->container->compile();
45 1
    }
46
47 1
    protected function registerCommands()
48
    {
49 1
        foreach ($this->container->findTaggedServiceIds('console.command') as $commandId => $command) {
50 1
            $commandService = $this->getCommandForId($commandId);
51
52 1
            if (null === $commandService) {
53
                throw new RuntimeException(sprintf("Couldn't fetch service %s from container.", $commandId));
54
            }
55
56 1
            $this->add($commandService);
57 1
        }
58 1
    }
59
60
61
62
    /**
63
     * @return string
64
     */
65 1
    private function getConfigPath()
66
    {
67 1
        return __DIR__ . '/../config';
68
    }
69
70
    /**
71
     * @return string
72
     */
73 1
    private function getRootPath()
74
    {
75 1
        return __DIR__ . '/..';
76
    }
77
78
79
80
    /**
81
     *
82
     */
83 1
    private function registerChecks()
84
    {
85 1
        foreach ($this->container->findTaggedServiceIds('provision.check') as $serviceId => $command) {
86 1
            $this->container->get('processor.candidate')->addCheck($this->container->get($serviceId));
87 1
        }
88
89 1
        foreach ($this->container->findTaggedServiceIds('provision.check.post') as $serviceId => $command) {
90 1
            $this->container->get('processor.candidate')->addPostCheck($this->container->get($serviceId));
91 1
        }
92 1
    }
93
94
    /**
95
     * @param string $commandId
96
     * @return Command|Object
97
     */
98 1
    protected function getCommandForId($commandId)
99
    {
100 1
        if (!$this->container->has($commandId)) {
101
            throw new RuntimeException(sprintf('There is no command class for id %s', $commandId));
102
        }
103
104 1
        return $this->container->get($commandId);
105
    }
106
}
107