Completed
Push — master ( b8b783...188bc3 )
by De Cramer
9s
created

GuiHandler::onExpansionGroupDestroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace eXpansion\Core\Plugins;
4
5
use eXpansion\Core\DataProviders\Listener\TimerDataListenerInterface;
6
use eXpansion\Core\DataProviders\Listener\UserGroupDataListenerInterface;
7
use eXpansion\Core\Model\Gui\ManialinkInerface;
8
use eXpansion\Core\Model\UserGroups\Group;
9
use eXpansion\Core\Services\Console;
10
use Maniaplanet\DedicatedServer\Connection;
11
use Maniaplanet\DedicatedServer\Xmlrpc\GbxRemote;
12
use Monolog\Logger;
13
use oliverde8\AssociativeArraySimplified\AssociativeArray;
14
15
/**
16
 * Class GuiHandler will send manialinks to player as needed.
17
 *
18
 * @package eXpansion\Core\Plugins\Gui
19
 * @author Oliver de Cramer
20
 */
21
class GuiHandler implements TimerDataListenerInterface, UserGroupDataListenerInterface
22
{
23
    /** @var Connection */
24
    protected $connection;
25
26
    /** @var Logger */
27
    protected $logger;
28
29
    /** @var Console */
30
    protected $console;
31
32
    /** @var int */
33
    protected $charLimit;
34
35
    /** @var ManialinkInerface[][] */
36
    protected $displayQueu = [];
37
38
    /** @var ManialinkInerface[][] */
39
    protected $individualQueu = [];
40
41
    /** @var ManialinkInerface[][] */
42
    protected $displayeds = [];
43
44
    /** @var ManialinkInerface[][] */
45
    protected $hideQueu = [];
46
47
    /** @var String[][] */
48
    protected $hideIndividualQueu = [];
49
50
    /**
51
     * GuiHandler constructor.
52
     *
53
     * @param Connection $connection
54
     */
55 10
    public function __construct(Connection $connection, Logger $logger, Console $console, $charLimit = 262144)
56
    {
57 10
        $this->connection = $connection;
58
59 10
        $this->connection->sendHideManialinkPage(null);
60
61 10
        $this->logger = $logger;
62 10
        $this->console = $console;
63 10
        $this->charLimit = $charLimit;
64 10
    }
65
66
67
    /**
68
     * Add a manialink to the diplay queue.
69
     *
70
     * @param ManialinkInerface $manialink
71
     */
72 8
    public function addToDisplay(ManialinkInerface $manialink)
73
    {
74 8
        $userGroup = $manialink->getUserGroup()->getName();
75
76 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...
77 1
            unset($this->hideQueu[$userGroup][$manialink->getId()]);
78
        }
79
80 8
        $this->displayQueu[$userGroup][$manialink->getId()] = $manialink;
81 8
    }
82
83
    /**
84
     * Add a manialink to the destruction queue.
85
     *
86
     * @param ManialinkInerface $manialink
87
     */
88 3
    public function addToHide(ManialinkInerface $manialink)
89
    {
90 3
        $userGroup = $manialink->getUserGroup()->getName();
91
92 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...
93 1
            unset($this->displayQueu[$userGroup][$manialink->getId()]);
94
        }
95
96 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...
97 1
            unset($this->displayeds[$userGroup][$manialink->getId()]);
98
        }
99
100 3
        $this->hideQueu[$userGroup][$manialink->getId()] = $manialink;
101 3
    }
102
103
    /**
104
     * Display & hide all manialinks.
105
     */
106 9
    protected function displayManialinks()
107
    {
108 9
        $size = 0;
109 9
        foreach ($this->getManialinksToDisplay() as $mlData) {
110 9
            $currentSize = $size;
111 9
            $size += strlen($mlData['ml']);
112
113 9
            if ($currentSize != 0 && $size > $this->charLimit) {
114 2
                $this->executeMultiCall();
115 2
                $size = strlen($mlData['ml']);
116
            }
117
118 9
            $this->connection->sendDisplayManialinkPage($mlData['logins'], $mlData['ml'], 0, false, true);
119
        }
120
121 9
        if ($size > 0) {
122 9
            $this->executeMultiCall();
123
        }
124
125
        // Reset the queues.
126 9
        $this->displayQueu = [];
127 9
        $this->individualQueu = [];
128 9
        $this->hideQueu = [];
129 9
        $this->hideIndividualQueu = [];
130 9
    }
131
132
    /**
133
     * Execute multicall & handle error.
134
     */
135 9
    protected function executeMultiCall()
136
    {
137
        try {
138 9
            $this->connection->executeMulticall();
139 1
        } catch (\Exception $e) {
140 1
            $this->logger->addError("Couldn't deliver all manialinks : " . $e->getMessage(), ['exception' => $e]);
141 1
            $this->console->writeln('$F00ERROR - Couldn\'t deliver all manialinks : ' . $e->getMessage());
142
        }
143 9
    }
144
145
    /**
146
     * Get list of all manialinks that needs to be displayed
147
     *
148
     * @return \Generator
149
     */
150 9
    protected function getManialinksToDisplay()
151
    {
152 9
        foreach ($this->displayQueu as $groupName => $manialinks) {
153 8
            foreach ($manialinks as $id => $manialink) {
154 8
                $logins = $manialink->getUserGroup()->getLogins();
155 8
                if (!empty($logins)) {
156 8
                    yield ['logins' => $logins, 'ml' => $manialink->getXml()];
157 8
                    $this->displayeds[$groupName][$id] = $manialink;
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 \eXpansion\Core\Model\Gui\ManialinkInerface[][]
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