Completed
Pull Request — master (#252)
by
unknown
05:45
created

AdminCommand::execute()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 16
cp 0.75
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 16
nc 5
nop 2
crap 3.1406
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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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