Completed
Push — master ( bd8b26...543084 )
by Zach
01:43
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
use Symfony\Component\Console\Input\InputInterface;
10
11
class Kernel
12
{
13
    /**
14
     * Application config.
15
     *
16
     * @var array
17
     */
18
    private $config;
19
20
    /**
21
     * Construct.
22
     *
23
     * @param array $config
24
     */
25
    public function __construct(array $config)
26
    {
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * Handle an incoming console command.
32
     */
33
    public function handle($input = null, $output = null)
34
    {
35
        $application = new Application();
36
37
        $this->registerCommands($application);
38
39
        if ($input && $output) {
40
            $this->validateCommand($application, $input);
41
42
            $application->setAutoExit(false);
43
44
            return $application->run($input, $output);
45
        }
46
47
        $application->run();
48
    }
49
50
    /**
51
     * Register all Yarak commands.
52
     *
53
     * @param Application $application
54
     */
55
    protected function registerCommands(Application $application)
56
    {
57
        $application->add(new MakeMigration($this->config));
58
        $application->add(new Migrate($this->config));
59
    }
60
61
    /**
62
     * Validate the given command.
63
     *
64
     * @param Application    $application
65
     * @param InputInterface $input
66
     *
67
     * @throws InvalidInput
68
     */
69
    protected function validateCommand(Application $application, InputInterface $input)
70
    {
71
        $command = $input->getFirstArgument();
72
73
        if ($application->has($command) === false) {
74
            throw InvalidInput::invalidCommand($command);
75
        }
76
    }
77
}
78