Completed
Pull Request — master (#194)
by De Cramer
24:15 queued 02:45
created

ReasonUserCommand::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
ccs 17
cts 17
cp 1
cc 1
eloc 28
nc 1
nop 12
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\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
    public function __construct(
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