|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\AdminChat\ChatCommand; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups; |
|
6
|
|
|
use eXpansion\Framework\Core\Helpers\ChatNotification; |
|
7
|
|
|
use eXpansion\Framework\Core\Helpers\Time; |
|
8
|
|
|
use eXpansion\Framework\Core\Helpers\TMString; |
|
9
|
|
|
use eXpansion\Framework\Core\Storage\PlayerStorage; |
|
10
|
|
|
use Maniaplanet\DedicatedServer\Connection; |
|
11
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\Exception as DedicatedException; |
|
12
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\FaultException; |
|
13
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ReasonUserCommand |
|
20
|
|
|
* |
|
21
|
|
|
* @author Reaby |
|
22
|
|
|
* @package eXpansion\Bundle\AdminChat\ChatCommand |
|
23
|
|
|
*/ |
|
24
|
|
|
class AdminCommand extends AbstractConnectionCommand |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Description of the command. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $description; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Message to display in chat. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $chatMessage; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Name of the dedicated function to call. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $functionName; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* AdminCommand constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param $command |
|
51
|
|
|
* @param string $permission |
|
52
|
|
|
* @param array $aliases |
|
53
|
|
|
* @param string $functionName |
|
54
|
|
|
* @param AdminGroups $adminGroupsHelper |
|
55
|
|
|
* @param Connection $connection |
|
56
|
|
|
* @param ChatNotification $chatNotification |
|
57
|
|
|
* @param PlayerStorage $playerStorage |
|
58
|
|
|
* @param LoggerInterface $logger |
|
59
|
|
|
* @param Time $timeHelper |
|
60
|
|
|
*/ |
|
61
|
3 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
62
|
|
|
$command, |
|
63
|
|
|
$permission, |
|
64
|
|
|
array $aliases = [], |
|
65
|
|
|
$functionName, |
|
66
|
|
|
AdminGroups $adminGroupsHelper, |
|
67
|
|
|
Connection $connection, |
|
68
|
|
|
ChatNotification $chatNotification, |
|
69
|
|
|
PlayerStorage $playerStorage, |
|
70
|
|
|
LoggerInterface $logger, |
|
71
|
|
|
Time $timeHelper |
|
72
|
|
|
) { |
|
73
|
3 |
|
parent::__construct( |
|
74
|
3 |
|
$command, |
|
75
|
3 |
|
$permission, |
|
76
|
3 |
|
$aliases, |
|
77
|
3 |
|
$adminGroupsHelper, |
|
78
|
3 |
|
$connection, |
|
79
|
3 |
|
$chatNotification, |
|
80
|
3 |
|
$playerStorage, |
|
81
|
3 |
|
$logger, |
|
82
|
3 |
|
$timeHelper |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
3 |
|
$this->description = 'expansion_admin_chat.' . strtolower($functionName) . '.description'; |
|
86
|
3 |
|
$this->chatMessage = 'expansion_admin_chat.' . strtolower($functionName) . '.msg'; |
|
87
|
3 |
|
$this->functionName = $functionName; |
|
88
|
3 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritdoc |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function execute($login, InputInterface $input) |
|
94
|
|
|
{ |
|
95
|
1 |
|
$nickName = $this->playerStorage->getPlayerInfo($login)->getNickName(); |
|
96
|
1 |
|
$group = $this->getGroupLabel($login); |
|
97
|
|
|
try { |
|
98
|
1 |
|
$this->connection->{$this->functionName}(); |
|
99
|
1 |
|
$this->chatNotification->sendMessage( |
|
100
|
1 |
|
$this->chatMessage, |
|
101
|
1 |
|
$this->isPublic ? null : $login, |
|
102
|
1 |
|
['%adminLevel%' => $group, '%admin%' => $nickName] |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
1 |
|
$logMessage = $this->chatNotification->getMessage($this->chatMessage, |
|
106
|
1 |
|
['%adminLevel%' => $group, '%admin%' => $nickName], "en"); |
|
107
|
1 |
|
$this->logger->info("[". $login. "] " . TMString::trimStyles($logMessage)); |
|
108
|
|
|
|
|
109
|
|
|
} catch (DedicatedException $e) { |
|
110
|
|
|
$this->logger->error("Error on admin command", ["exception" => $e]); |
|
111
|
|
|
$this->chatNotification->sendMessage("expansion_admin_chat.dedicatedexception", $login, |
|
112
|
|
|
["%message%" => $e->getMessage()]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.