Completed
Pull Request — master (#393)
by De Cramer
02:39
created

AdminCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 7
cts 7
cp 1
rs 9.472
c 0
b 0
f 0
cc 1
nc 1
nop 10
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Maniaplanet\DedicatedServer\Xmlrpc\FaultException;
14
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Console\Input\InputInterface;
17
18
19
/**
20
 * Class ReasonUserCommand
21
 *
22
 * @author  Reaby
23
 * @package eXpansion\Bundle\AdminChat\ChatCommand
24
 */
25
class AdminCommand extends AbstractConnectionCommand
26
{
27
    /**
28
     * Description of the command.
29
     *
30
     * @var string
31
     */
32
    protected $description;
33
34
    /**
35
     * Message to display in chat.
36
     *
37
     * @var string
38
     */
39
    protected $chatMessage;
40
41
    /**
42
     * Name of the dedicated function to call.
43
     *
44
     * @var string
45
     */
46
    protected $functionName;
47
48
    /**
49
     * AdminCommand constructor.
50
     *
51
     * @param $command
52
     * @param $permission
53
     * @param array $aliases
54
     * @param $functionName
55
     * @param AdminGroups $adminGroupsHelper
56
     * @param Factory $factory
57
     * @param ChatNotification $chatNotification
58
     * @param PlayerStorage $playerStorage
59
     * @param LoggerInterface $logger
60
     * @param Time $timeHelper
61
     */
62 3
    public function __construct(
63
        $command,
64
        $permission,
65
        array $aliases = [],
66
        $functionName,
67
        AdminGroups $adminGroupsHelper,
68
        Factory $factory,
69
        ChatNotification $chatNotification,
70
        PlayerStorage $playerStorage,
71
        LoggerInterface $logger,
72
        Time $timeHelper
73
    ) {
74 3
        parent::__construct(
75 3
            $command,
76
            $permission,
77
            $aliases,
78
            $adminGroupsHelper,
79
            $factory,
80
            $chatNotification,
81
            $playerStorage,
82
            $logger,
83
            $timeHelper
84
        );
85
86 3
        $this->description = 'expansion_admin_chat.'.strtolower($functionName).'.description';
87 3
        $this->chatMessage = 'expansion_admin_chat.'.strtolower($functionName).'.msg';
88 3
        $this->functionName = $functionName;
89 3
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 1
    public function execute($login, InputInterface $input)
95
    {
96 1
        $nickName = $this->playerStorage->getPlayerInfo($login)->getNickName();
97 1
        $group = $this->getGroupLabel($login);
98
        try {
99 1
            $this->factory->getConnection()->{$this->functionName}();
100 1
            $this->chatNotification->sendMessage(
101 1
                $this->chatMessage,
102 1
                $this->isPublic ? null : $login,
103 1
                ['%adminLevel%' => $group, '%admin%' => $nickName]
104
            );
105
106 1
            $logMessage = $this->chatNotification->getMessage($this->chatMessage,
107 1
                ['%adminLevel%' => $group, '%admin%' => $nickName], "en");
108 1
            $this->logger->info("[".$login."] ".TMString::trimStyles($logMessage));
109
110
        } catch (DedicatedException $e) {
111
            $this->logger->error("Error on admin command", ["exception" => $e]);
112
            $this->chatNotification->sendMessage("expansion_admin_chat.dedicatedexception", $login,
113
                ["%message%" => $e->getMessage()]);
114
        }
115
116 1
    }
117
}
118