Completed
Pull Request — master (#300)
by De Cramer
04:10
created

ReasonUserCommand::execute()   B

Complexity

Conditions 3
Paths 5

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 23
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 23
nc 5
nop 2
crap 12
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\Services\DedicatedConnection\Factory;
10
use eXpansion\Framework\Core\Storage\PlayerStorage;
11
use Maniaplanet\DedicatedServer\Connection;
12
use Maniaplanet\DedicatedServer\Xmlrpc\Exception as DedicatedException;
13
use Psr\Log\LoggerInterface;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
17
/**
18
 * Class ReasonUserCommand
19
 *
20
 * @author    de Cramer Oliver<[email protected]>
21
 * @copyright 2017 eXpansion
22
 * @package   eXpansion\Bundle\AdminChat\ChatCommand
23
 */
24
class ReasonUserCommand extends AbstractConnectionCommand
25
{
26
    /**
27
     * Description of the login parameter
28
     *
29
     * @var string
30
     */
31
    protected $parameterLoginDescription;
32
33
    /**
34
     * Description of the reason parameter.
35
     *
36
     * @var string
37
     */
38
    protected $parameterReasonDescription;
39
40
    /**
41
     * Description of the command.
42
     *
43
     * @var string
44
     */
45
    protected $description;
46
47
    /**
48
     * Message to display in chat.
49
     *
50
     * @var string
51
     */
52
    protected $chatMessage;
53
54
    /**
55
     * Name of the dedicated function to call.
56
     *
57
     * @var string
58
     */
59
    protected $functionName;
60
61
    /**
62
     * ReasonUserCommand constructor.
63
     *
64
     * @param $command
65
     * @param $permission
66
     * @param array $aliases
67
     * @param $functionName
68
     * @param $parameterLoginDescription
69
     * @param $parameterReasonDescription
70
     * @param AdminGroups $adminGroupsHelper
71
     * @param Factory $factory
72
     * @param ChatNotification $chatNotification
73
     * @param PlayerStorage $playerStorage
74
     * @param LoggerInterface $logger
75
     * @param Time $timeHelper
76
     */
77 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...
78
        $command,
79
        $permission,
80
        array $aliases = [],
81
        $functionName,
82
        $parameterLoginDescription,
83
        $parameterReasonDescription,
84
        AdminGroups $adminGroupsHelper,
85
        Factory $factory,
86
        ChatNotification $chatNotification,
87
        PlayerStorage $playerStorage,
88
        LoggerInterface $logger,
89
        Time $timeHelper
90
    ) {
91
        parent::__construct(
92
            $command,
93
            $permission,
94
            $aliases,
95
            $adminGroupsHelper,
96
            $factory,
97
            $chatNotification,
98
            $playerStorage,
99
            $logger,
100
            $timeHelper
101
        );
102
103
        $this->description = 'expansion_admin_chat.'.strtolower($functionName).'.description';
104
        $this->chatMessage = 'expansion_admin_chat.'.strtolower($functionName).'.msg';
105
        $this->functionName = $functionName;
106
        $this->parameterLoginDescription = $parameterLoginDescription;
107
        $this->parameterReasonDescription = $parameterReasonDescription;
108
    }
109
110
111
    /**
112
     * @inheritdoc
113
     */
114
    protected function configure()
115
    {
116
        parent::configure();
117
118
        $this->inputDefinition->addArgument(
119
            new InputArgument('login', InputArgument::REQUIRED, $this->parameterLoginDescription)
120
        );
121
        $this->inputDefinition->addArgument(
122
            new InputArgument('reason', InputArgument::REQUIRED, $this->parameterReasonDescription)
123
        );
124
    }
125
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function execute($login, InputInterface $input)
131
    {
132
        $nickName = $this->playerStorage->getPlayerInfo($login)->getNickName();
133
        $playerLogin = $input->getArgument('login');
134
        $reason = $input->getArgument('reason');
135
        $group = $this->getGroupLabel($login);
136
137
        $playerNickName = $this->playerStorage->getPlayerInfo($playerLogin)->getNickName();
138
        try {
139
            $this->factory->getConnection()->{$this->functionName}($playerLogin, $reason);
140
            $this->chatNotification->sendMessage(
141
                $this->chatMessage,
142
                $this->isPublic ? null : $login,
143
                ['%adminLevel%' => $group, '%admin%' => $nickName, '%player%' => $playerNickName, "%reason%" => $reason]
144
            );
145
146
            $logMessage = $this->chatNotification->getMessage($this->chatMessage,
147
                [
148
                    '%adminLevel%' => $group,
149
                    '%admin%' => $nickName,
150
                    '%player%' => $playerNickName,
151
                    "%reason%" => $reason
152
                ], "en");
153
            $this->logger->info("[".$login."] ".TMString::trimStyles($logMessage));
154
155
156
        } catch (DedicatedException $e) {
157
            $this->logger->error("Error on admin command", ["exception" => $e]);
158
            $this->chatNotification->sendMessage("expansion_admin_chat.dedicatedexception", $login,
159
                ["%message%" => $e->getMessage()]);
160
        }
161
    }
162
}
163