Completed
Pull Request — master (#38)
by Jonathan
02:52
created

PHPChunkitApplication::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 2
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PHPChunkit;
6
7
use PHPChunkit\Command;
8
use Symfony\Component\Console\Application;
9
use Symfony\Component\Console\Command\Command as SymfonyCommand;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputDefinition;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * @testClass PHPChunkit\Test\PHPChunkitApplicationTest
18
 */
19
class PHPChunkitApplication
20
{
21
    /**
22
     * @var Container
23
     */
24
    private $container;
25
26
    /**
27
     * @var Application
28
     */
29
    private $symfonyApplication;
30
31
    private static $commands = [
32
        'phpchunkit.command.build_sandbox',
33
        'phpchunkit.command.create_databases',
34
        'phpchunkit.command.test_watcher',
35
        'phpchunkit.command.run',
36
        'phpchunkit.command.generate_test',
37
    ];
38
39 1
    public function __construct(Container $container)
40
    {
41 1
        $this->container = $container;
42 1
        $this->symfonyApplication = $this->container['phpchunkit.symfony_application'];
43 1
    }
44
45 1
    public function run(InputInterface $input, OutputInterface $output) : int
46
    {
47 1
        $this->container['phpchunkit.application.input'] = $input;
48 1
        $this->container['phpchunkit.application.output'] = $output;
49
50 1
        foreach (self::$commands as $serviceName) {
51 1
            $this->registerCommand($serviceName);
52
        }
53
54 1
        return $this->runSymfonyApplication($input, $output);
55
    }
56
57 1
    public function registerCommand(string $serviceName)
58
    {
59 1
        $service = $this->container[$serviceName];
60
61 1
        $symfonyCommand = $this->register($service->getName());
62
63 1
        $service->configure($symfonyCommand);
64
65 1
        $symfonyCommand->setCode([$service, 'execute']);
66 1
    }
67
68
    protected function runSymfonyApplication(
69
        InputInterface $input,
70
        OutputInterface $output) : int
71
    {
72
        return $this->symfonyApplication->run($input, $output);
73
    }
74
75 1
    private function register(string $name) : SymfonyCommand
76
    {
77 1
        return $this->symfonyApplication->register($name);
78
    }
79
}
80