Completed
Push — master ( bc1002...bc42f8 )
by Łukasz
03:30
created

Application::getComposerBinPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    /** @var ContainerBuilder */
18
    private $container;
19
20
    /**
21
     * @param string $name
22
     * @param string $version
23
     */
24
    public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
25
    {
26
        parent::__construct($name, $version);
27
        $this->boot();
28
    }
29
30
31
32
    private function boot()
33
    {
34
        $this->container = new ContainerBuilder();
35
        $this->container->setParameter('sql_provisioner.composer_bin_path', $this->getComposerBinPath());
36
37
        $loader = new XmlFileLoader($this->container, new FileLocator($this->getConfigPath()));
38
        $loader->load('services.xml');
39
        $this->registerCommands();
40
        $this->registerChecks();
41
        $this->container->compile();
42
    }
43
44
    protected function registerCommands()
45
    {
46
        foreach ($this->container->findTaggedServiceIds('console.command') as $commandId => $command) {
47
            $this->add($this->getCommandForId($commandId));
0 ignored issues
show
Bug introduced by
It seems like $this->getCommandForId($commandId) can be null; however, add() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
48
        }
49
    }
50
51
52
53
    /**
54
     * @return string
55
     */
56
    private function getConfigPath()
57
    {
58
        return __DIR__ . '/../config';
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    private function getComposerBinPath()
65
    {
66
        return __DIR__ . '/../vendor/bin';
67
    }
68
69
70
71
    private function registerChecks()
72
    {
73
        foreach ($this->container->findTaggedServiceIds('provision.check') as $serviceId => $command) {
74
            $this->container->get('processor.candidate')->addCheck($this->container->get($serviceId));
75
        }
76
    }
77
78
    /**
79
     * @param string $commandId
80
     * @return Command
81
     */
82
    protected function getCommandForId($commandId)
83
    {
84
        if (!$this->container->has($commandId)) {
85
            throw new RuntimeException(sprintf('There is no command class for id %s', $commandId));
86
        }
87
88
        return $this->container->get($commandId);
89
    }
90
}