Application   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 9
c 3
b 1
f 0
lcom 1
cbo 5
dl 0
loc 102
ccs 32
cts 32
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getKernel() 0 4 1
A __construct() 0 18 2
A doRun() 0 15 3
A registerCommands() 0 8 3
1
<?php
2
3
/**
4
 * This file is part of the CLIFramework package.
5
 *
6
 * (c) Arnaud VEBER <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CLIFramework;
13
14
use Symfony\Component\Console\Application as BaseApplication;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19
use Symfony\Component\HttpKernel\KernelInterface;
20
21
/**
22
 * Application.
23
 */
24
class Application extends BaseApplication
25
{
26
    /**
27
     * name
28
     *
29
     * @const NAME
30
     */
31
    const NAME = 'CLIFramework';
32
33
    /**
34
     * version
35
     *
36
     * @const VERSION
37
     */
38
    const VERSION = '0.0.1-dev';
39
40
    /**
41
     * The kernel.
42
     *
43
     * @var \Symfony\Component\HttpKernel\KernelInterface $kernel
44
     */
45
    private $kernel;
46
47
    /**
48
     * The container.
49
     *
50
     * @var \Symfony\Component\DependencyInjection\ContainerInterface $container
51
     */
52
    private $container;
53
54
    /**
55
     * Construct the application.
56
     *
57
     * @param \Symfony\Component\HttpKernel\KernelInterface $kernel
58
     */
59 4
    public function __construct(KernelInterface $kernel)
60
    {
61 4
        $this->kernel = $kernel;
62 4
        $this->container = $kernel->getContainer();
63
64 4
        parent::__construct(self::NAME, self::VERSION);
65
66 4
        if ($this->container->has('event_dispatcher')) {
67 3
            $this->setDispatcher($this->container->get('event_dispatcher'));
68 3
        }
69
70 4
        $this->getDefinition()->addOption(
71 4
            new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev')
72 4
        );
73 4
        $this->getDefinition()->addOption(
74 4
            new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.')
75 4
        );
76 4
    }
77
78
    /**
79
     * Get kernel.
80
     *
81
     * @return \Symfony\Component\HttpKernel\KernelInterface
82
     */
83 1
    public function getKernel()
84
    {
85 1
        return $this->kernel;
86
    }
87
88
    /**
89
     * Runs the current application.
90
     *
91
     * @param \Symfony\Component\Console\Input\InputInterface $input
92
     * @param \Symfony\Component\Console\Output\OutputInterface $output
93
     *
94
     * @return int
95
     */
96 2
    public function doRun(InputInterface $input, OutputInterface $output)
97
    {
98
        // register commands
99 2
        $this->registerCommands();
100
101
        // inject container
102 2
        $container = $this->kernel->getContainer();
103 2
        foreach ($this->all() as $command) {
104 2
            if ($command instanceof ContainerAwareInterface) {
105 1
                $command->setContainer($container);
106 1
            }
107 2
        }
108
109 2
        return parent::doRun($input, $output);
110
    }
111
112
    /**
113
     * Register commands into the application
114
     *
115
     * @return void
116
     */
117 2
    protected function registerCommands()
118
    {
119 2
        if ($this->container->hasParameter('app.command.ids')) {
120 2
            foreach ($this->container->getParameter('app.command.ids') as $id) {
121 1
                $this->add($this->container->get($id));
122 2
            }
123 2
        }
124 2
    }
125
}
126