Completed
Push — dev ( e97233...07afa9 )
by Zach
02:12
created

Kernel::validateCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Yarak;
4
5
use Yarak\Commands\Migrate;
6
use Yarak\Commands\MakeMigration;
7
use Yarak\Exceptions\InvalidInput;
8
use Symfony\Component\Console\Application;
9
10
class Kernel
11
{
12
    /**
13
     * Application config.
14
     *
15
     * @var array
16
     */
17
    private $config;
18
19
    /**
20
     * Construct.
21
     *
22
     * @param array $config
23
     */
24
    public function __construct(array $config)
25
    {
26
        $this->config = $config;
27
    }
28
29
    /**
30
     * Handle an incoming console command.
31
     */
32
    public function handle($input = null, $output = null)
33
    {
34
        $application = new Application();
35
36
        $this->registerCommands($application);
37
38
        if ($input && $output) {
39
            $this->validateCommand($application, $input);
40
41
            $application->setAutoExit(false);
42
43
            return $application->run($input, $output);
44
        }
45
46
        $application->run();
47
    }
48
49
    /**
50
     * Register all Yarak commands.
51
     *
52
     * @param Application $application
53
     */
54
    protected function registerCommands(Application $application)
55
    {
56
        $application->add(new MakeMigration($this->config));
57
        $application->add(new Migrate($this->config));
58
    }
59
60
    protected function validateCommand($application, $input)
61
    {
62
        $command = $input->getFirstArgument();
63
64
        if ($application->has($command) === false) {
65
            throw InvalidInput::invalidCommand($command);
66
        }
67
    }
68
}
69