Completed
Push — master ( e4b07c...c935ac )
by De Cramer
10:27 queued 07:51
created

AbstractChatCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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