Completed
Branch master (6600a4)
by DeGracia
02:20 queued 27s
created

Command::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace DeGraciaMathieu\Clike;
4
5
class Command {
6
7
    /**
8
     * Execute a command
9
     * @param  \DeGraciaMathieu\Clike\Contracts\Command $command
10
     * @return array
11
     */
12 1
    public function execute(Contracts\Command $command) :array
13
    {
14 1
        if ($command->authorized()) {
15 1
            return $this->launch($command);
16
        }
17
18
        return $this->unauthorize($command);
19
    }
20
21
    /**
22
     * Launch an authorized command
23
     * @param  \DeGraciaMathieu\Clike\Contracts\Command $command
24
     * @return array
25
     */
26 1
    protected function launch(Contracts\Command $command) :array
27
    {
28 1
        $command->process();
29
30 1
        $output = new Outputs\Command($command);
31
32 1
        return $this->interpretOutput($output);
33
    }
34
35
    /**
36
     * Response for a unauthorized command
37
     * @param  \DeGraciaMathieu\Clike\Contracts\Command $command
38
     * @return array
39
     */
40
    protected function unauthorize(Contracts\Command $command) :array
41
    {
42
        $output = new Outputs\Unauthorize($command);
43
44
        return $this->interpretOutput($output);
45
    }
46
47
    /**
48
     * return command response
49
     * @param  \DeGraciaMathieu\Clike\Contracts\Output $output
50
     * @return array
51
     */
52 1
    protected function interpretOutput(Contracts\Output $output) :array
53
    {
54 1
        return Interpreter::read($output);
55
    }
56
}
57