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

AdminCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 93
Duplicated Lines 30.11 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 28
loc 93
ccs 27
cts 31
cp 0.871
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 28 28 1
A execute() 0 23 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 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