Completed
Pull Request — master (#194)
by
unknown
04:05
created

ReasonUserCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 127
Duplicated Lines 25.2 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 89.74%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 8
dl 32
loc 127
ccs 35
cts 39
cp 0.8974
rs 10
c 0
b 0
f 0

3 Methods

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

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