Completed
Push — master ( e09c01...ac9146 )
by De Cramer
02:18 queued 02:12
created

WindowsGuiHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 80
rs 10
c 0
b 0
f 0
ccs 0
cts 38
cp 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B addToDisplay() 0 17 5
A addToHide() 0 13 4
A onPlayerConnect() 0 4 1
A onPlayerDisconnect() 0 6 2
A onPlayerInfoChanged() 0 4 1
A onPlayerAlliesChanged() 0 4 1
1
<?php
2
3
namespace eXpansion\Bundle\ImmersiveWindows\Plugins;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyPlayer;
6
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
7
use eXpansion\Framework\Core\Model\Gui\Window;
8
use eXpansion\Framework\Core\Plugins\GuiHandlerInterface;
9
use eXpansion\Framework\Core\Storage\Data\Player;
10
11
/**
12
 * Class WindowsGuiHandler, replaces the native GuiHandler only for windows type manialinks in order to :
13
 *  - Prevent more then 1 window to open.
14
 *  - Change the display
15
 *
16
 * @author    de Cramer Oliver<[email protected]>
17
 * @copyright 2017 Smile
18
 * @package eXpansion\Bundle\Menu\Plugins
19
 */
20
class WindowsGuiHandler implements GuiHandlerInterface, ListenerInterfaceMpLegacyPlayer
21
{
22
    /** @var  GuiHandlerInterface */
23
    protected $guiHandler;
24
25
    /** @var Window[] */
26
    protected $userWindows = [];
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function addToDisplay(ManialinkInterface $manialink)
32
    {
33
        $logins = $manialink->getUserGroup()->getLogins();
34
        if (count($logins) == 1 && !$manialink->getUserGroup()->isPersistent()) {
35
            $login = $logins[0];
36
37
            // If a window is already displayed hide it. We wish to have 1 window max.
38
            if (isset($this->userWindows[$login]) && $this->userWindows[$login]->getId() != $manialink->getId()) {
39
                $this->guiHandler->addToHide($this->userWindows[$login]);
40
            }
41
42
            $this->userWindows[$login] = $manialink;
43
        }
44
45
        // TODO edit window content.
46
        $this->guiHandler->addToDisplay($manialink);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function addToHide(ManialinkInterface $manialink)
53
    {
54
        $logins = $manialink->getUserGroup()->getLogins();
55
        if (count($logins) == 1 && !$manialink->getUserGroup()->isPersistent()) {
56
            $login = $logins[0];
57
58
            if (isset($this->userWindows[$login])) {
59
                unset($this->userWindows[$login]);
60
            }
61
        }
62
63
        $this->guiHandler->addToHide($manialink);
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function onPlayerConnect(Player $player)
70
    {
71
        // Nothing
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
78
    {
79
        if (isset($this->userWindows[$player->getLogin()])) {
80
            unset($this->userWindows[$player->getLogin()]);
81
        }
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
88
    {
89
        // Nothing
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
96
    {
97
        // Nothing
98
    }
99
}
100