ConsoleKernel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommands() 0 10 2
A verifyCommand() 0 12 3
1
<?php
2
3
namespace Yarak\Console;
4
5
use Yarak\Exceptions\FileNotFound;
6
use Yarak\Exceptions\InvalidCommand;
7
8
class ConsoleKernel
9
{
10
    /**
11
     * User defined commands.
12
     *
13
     * @var array
14
     */
15
    protected $commands = [];
16
17
    /**
18
     * Get all user defined commands.
19
     *
20
     * @return array
21
     */
22
    public function getCommands()
23
    {
24
        if (property_exists($this, 'commands')) {
25
            return array_map(function ($command) {
26
                $this->verifyCommand($command);
27
28
                return $command;
29
            }, $this->commands);
30
        }
31
    }
32
33
    /**
34
     * Verify user defined commands.
35
     *
36
     * @param string $command
37
     *
38
     * @throws FileNotFound|InvalidCommand
39
     */
40
    protected function verifyCommand($command)
41
    {
42
        if (!class_exists($command)) {
43
            throw FileNotFound::commandNotFound($command);
44
        }
45
46
        $class = new $command();
47
48
        if (!is_a($class, Command::class)) {
49
            throw InvalidCommand::doesntExtendCommand();
50
        }
51
    }
52
}
53