Completed
Push — master ( fff0f0...89663a )
by De Cramer
02:26
created

GuiHandler::onExpansionGroupAddUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
3
namespace eXpansion\Framework\Core\Plugins;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\TimerDataListenerInterface;
6
use eXpansion\Framework\Core\DataProviders\Listener\UserGroupDataListenerInterface;
7
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
8
use eXpansion\Framework\Core\Model\UserGroups\Group;
9
use eXpansion\Framework\Core\Services\Console;
10
use Maniaplanet\DedicatedServer\Connection;
11
use Monolog\Logger;
12
use oliverde8\AssociativeArraySimplified\AssociativeArray;
13
14
/**
15
 * Class GuiHandler will send manialinks to player as needed.
16
 *
17
 * @package eXpansion\Framework\Core\Plugins\Gui
18
 * @author Oliver de Cramer
19
 */
20
class GuiHandler implements TimerDataListenerInterface, UserGroupDataListenerInterface
21
{
22
    /** @var Connection */
23
    protected $connection;
24
25
    /** @var Logger */
26
    protected $logger;
27
28
    /** @var Console */
29
    protected $console;
30
31
    /** @var int */
32
    protected $charLimit;
33
34
    /** @var ManialinkInterface[][] */
35
    protected $displayQueu = [];
36
37
    /** @var ManialinkInterface[][] */
38
    protected $individualQueu = [];
39
40
    /** @var ManialinkInterface[][] */
41
    protected $displayeds = [];
42
43
    /** @var ManialinkInterface[][] */
44
    protected $hideQueu = [];
45
46
    /** @var String[][] */
47
    protected $hideIndividualQueu = [];
48
49
    /**
50
     * GuiHandler constructor.
51
     *
52
     * @param Connection $connection
53
     */
54 10
    public function __construct(Connection $connection, Logger $logger, Console $console, $charLimit = 262144)
55
    {
56 10
        $this->connection = $connection;
57
58 10
        $this->connection->sendHideManialinkPage(null);
59
60 10
        $this->logger = $logger;
61 10
        $this->console = $console;
62 10
        $this->charLimit = $charLimit;
63 10
    }
64
65
66
    /**
67
     * Add a manialink to the diplay queue.
68
     *
69
     * @param ManialinkInterface $manialink
70
     */
71 8
    public function addToDisplay(ManialinkInterface $manialink)
72
    {
73 8
        $userGroup = $manialink->getUserGroup()->getName();
74
75 8 View Code Duplication
        if (AssociativeArray::getFromKey($this->hideQueu, [$userGroup, $manialink->getId()])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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 1
            unset($this->hideQueu[$userGroup][$manialink->getId()]);
77
        }
78
79 8
        $this->displayQueu[$userGroup][$manialink->getId()] = $manialink;
80 8
    }
81
82
    /**
83
     * Add a manialink to the destruction queue.
84
     *
85
     * @param ManialinkInterface $manialink
86
     */
87 3
    public function addToHide(ManialinkInterface $manialink)
88
    {
89 3
        $userGroup = $manialink->getUserGroup()->getName();
90
91 3 View Code Duplication
        if (AssociativeArray::getFromKey($this->displayQueu, [$userGroup, $manialink->getId()])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92 1
            unset($this->displayQueu[$userGroup][$manialink->getId()]);
93
        }
94
95 3 View Code Duplication
        if (AssociativeArray::getFromKey($this->displayeds, [$userGroup, $manialink->getId()])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96 1
            unset($this->displayeds[$userGroup][$manialink->getId()]);
97
        }
98
99 3
        $this->hideQueu[$userGroup][$manialink->getId()] = $manialink;
100 3
    }
101
102
    /**
103
     * Display & hide all manialinks.
104
     */
105 9
    protected function displayManialinks()
106
    {
107 9
        $size = 0;
108 9
        foreach ($this->getManialinksToDisplay() as $mlData) {
109 9
            $currentSize = $size;
110 9
            $size += strlen($mlData['ml']);
111
112 9
            if ($currentSize != 0 && $size > $this->charLimit) {
113 2
                $this->executeMultiCall();
114 2
                $size = strlen($mlData['ml']);
115
            }
116
117 9
            $this->connection->sendDisplayManialinkPage($mlData['logins'], $mlData['ml'], 0, false, true);
118
        }
119
120 9
        if ($size > 0) {
121 9
            $this->executeMultiCall();
122
        }
123
124
        // Reset the queues.
125 9
        $this->displayQueu = [];
126 9
        $this->individualQueu = [];
127 9
        $this->hideQueu = [];
128 9
        $this->hideIndividualQueu = [];
129 9
    }
130
131
    /**
132
     * Execute multicall & handle error.
133
     */
134 9
    protected function executeMultiCall()
135
    {
136
        try {
137 9
            $this->connection->executeMulticall();
138 1
        } catch (\Exception $e) {
139 1
            $this->logger->addError("Couldn't deliver all manialinks : " . $e->getMessage(), ['exception' => $e]);
140 1
            $this->console->writeln('$F00ERROR - Couldn\'t deliver all manialinks : ' . $e->getMessage());
141
        }
142 9
    }
143
144
    /**
145
     * Get list of all manialinks that needs to be displayed
146
     *
147
     * @return \Generator
148
     */
149 9
    protected function getManialinksToDisplay()
150
    {
151 9
        foreach ($this->displayQueu as $groupName => $manialinks) {
152 8
            foreach ($manialinks as $id => $manialink) {
153 8
                $logins = $manialink->getUserGroup()->getLogins();
154
155 8
                $this->displayeds[$groupName][$id] = $manialink;
156 8
                if (!empty($logins)) {
157 8
                    yield ['logins' => $logins, 'ml' => $manialink->getXml()];
158
                }
159
            }
160
        }
161
162 9
        foreach ($this->individualQueu as $login => $manialinks) {
163 1
            foreach ($manialinks as $id => $manialink) {
164 1
                $xml = $manialink->getXml();
165 1
                yield ['logins' => $login, 'ml' => $xml];
166
            }
167
        }
168
169 9
        foreach ($this->hideQueu as $manialinks) {
170 3
            foreach ($manialinks as $id => $manialink) {
171 2
                $logins = $manialink->getUserGroup()->getLogins();
172 2
                if (!empty($logins)) {
173 3
                    yield ['logins' => $logins, 'ml' => '<manialink id="' . $id . '" />'];
174
                }
175
            }
176
        }
177
178 9
        foreach ($this->hideIndividualQueu as $login => $manialinks) {
179 1
            foreach ($manialinks as $id => $manialink) {
180 1
                yield ['logins' => $login, 'ml' => '<manialink id="' . $id . '" />'];
181
            }
182
        }
183 9
    }
184
185
    /**
186
     * @inheritdoc
187
     */
188 9
    public function onPostLoop()
189
    {
190 9
        $this->displayManialinks();
191 9
    }
192
193
    /**
194
     * @inheritdoc
195
     */
196 1
    public function onPreLoop()
197
    {
198 1
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203 1
    public function onEverySecond()
204
    {
205 1
    }
206
207
    /**
208
     * @inheritdoc
209
     */
210 1 View Code Duplication
    public function onExpansionGroupAddUser(Group $group, $loginAdded)
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...
211
    {
212 1
        $group = $group->getName();
213
214
        // User was added to group, need to display all manialinks of the group to this user
215 1
        if (isset($this->displayeds[$group])) {
216 1
            foreach ($this->displayeds[$group] as $mlId => $manialink) {
217 1
                $this->individualQueu[$loginAdded][$mlId] = $manialink;
218
            }
219
        }
220 1
    }
221
222
    /**
223
     * @inheritdoc
224
     */
225 1 View Code Duplication
    public function onExpansionGroupRemoveUser(Group $group, $loginRemoved)
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...
226
    {
227 1
        $group = $group->getName();
228
229
        // User was added to group, need to hide all manialinks of the group to this user
230 1
        if (isset($this->displayeds[$group])) {
231 1
            foreach ($this->displayeds[$group] as $mlId => $manialink) {
232 1
                $this->hideIndividualQueu[$loginRemoved][$mlId] = $manialink;
233
            }
234
        }
235 1
    }
236
237
    /**
238
     * @inheritdoc
239
     */
240 1
    public function onExpansionGroupDestroy(Group $group, $lastLogin)
241
    {
242 1
       if (isset($this->displayeds[$group->getName()])) {
243 1
           unset($this->displayeds[$group->getName()]);
244
       }
245 1
    }
246
247
    /**
248
     * List of all manialinks that are currentyl displayed.
249
     *
250
     * @return ManialinkInterface[]
251
     */
252 1
    public function getDisplayeds()
253
    {
254 1
        return $this->displayeds;
255
    }
256
257
    /**
258
     * @param int $charLimit
259
     */
260 2
    public function setCharLimit($charLimit)
261
    {
262 2
        $this->charLimit = $charLimit;
263 2
    }
264
}
265