Completed
Push — odev ( a25a56...21e788 )
by De Cramer
02:29
created

AbstractChatCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 94
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A configure() 0 4 1
A getCommand() 0 4 1
A getAliases() 0 4 1
A validate() 0 4 1
execute() 0 1 ?
A getDescription() 0 4 1
A getHelp() 0 4 1
A run() 0 19 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\ChatCommand;
4
5
use eXpansion\Framework\Core\Helpers\ChatOutput;
6
use Symfony\Component\Console\Helper\DescriptorHelper;
7
use Symfony\Component\Console\Input\ArgvInput;
8
use Symfony\Component\Console\Input\InputDefinition;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
12
13
/**
14
 * Class AbstractChatCommand
15
 *
16
 * @package eXpansion\Framework\Core\Model\ChatCommand;
17
 * @author oliver de Cramer <[email protected]>
18
 */
19
abstract class AbstractChatCommand implements ChatCommandInterface
20
{
21
    /** @var string */
22
    protected $command;
23
24
    /** @var string[] */
25
    protected $aliases = [];
26
27
    /** @var InputDefinition  */
28
    protected $inputDefinition;
29
30
    /**
31
     * AbstractChatCommand constructor.
32
     *
33
     * @param $command
34
     * @param array $aliases
35
     */
36 5
    public function __construct($command, array $aliases = [])
37
    {
38 5
        $this->command = $command;
39 5
        $this->aliases = $aliases;
40
41 5
        $this->inputDefinition = new InputDefinition();
42
43 5
        $this->configure();
44 5
    }
45
46
    /**
47
     * Configure input definition.
48
     */
49 5
    protected function configure()
50
    {
51
        // Overwrite to add input definition.
52 5
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 1
    public function getCommand()
58
    {
59 1
        return $this->command;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 1
    public function getAliases()
66
    {
67 1
        return $this->aliases;
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function validate($login, $parameter)
74
    {
75
        return "";
76
    }
77
78 2
    public function getDescription()
79
    {
80 2
        return 'expansion_core.chat_commands.no_description';
81
    }
82
83 1
    public function getHelp()
84
    {
85 1
        return 'expansion_core.chat_commands.no_help';
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 4
    public function run($login, ChatOutput $output, $parameter)
92
    {
93 4
        $parameter = str_getcsv($parameter, " ", '"');
94 4
        $parameter = array_merge([0 => 1], $parameter);
95
96 4
        $input = new ArgvInput($parameter);
97
98 4
        if (true === $input->hasParameterOption(array('--help', '-h'), true)) {
99 2
            $helper = new DescriptorHelper();
100 2
            $output->getChatNotification()->sendMessage($this->getDescription(), $login);
101 2
            $helper->describe($output, $this->inputDefinition);
102 2
            return '';
103
        }
104
105 2
        $input->bind($this->inputDefinition);
106 1
        $input->validate();
107
108 1
        $this->execute($login, $input);
109 1
    }
110
111
    abstract public function execute($login, InputInterface $input);
112
}