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

ReasonUserCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 140
Duplicated Lines 22.86 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 91.49%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 9
dl 32
loc 140
ccs 43
cts 47
cp 0.9149
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 32 32 1
A configure() 0 11 1
B execute() 0 33 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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