Completed
Push — dev ( 8eacd8...04be10 )
by De Cramer
02:44
created

Factory::onPlayerDisconnect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 6
1
<?php
2
3
namespace eXpansion\Framework\Core\Plugins\UserGroups;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\PlayerDataListenerInterface;
6
use eXpansion\Framework\Core\DataProviders\Listener\UserGroupDataListenerInterface;
7
use eXpansion\Framework\Core\Model\UserGroups\Group;
8
use eXpansion\Framework\Core\Services\Application\DispatcherInterface;
9
use eXpansion\Framework\Core\Storage\Data\Player;
10
11
/**
12
 * Class Factory handles non persistent user groups.
13
 *
14
 * @package eXpansion\Framework\Core\Plugins
15
 * @author Oliver de Cramer
16
 */
17
class Factory implements UserGroupDataListenerInterface, PlayerDataListenerInterface
18
{
19
    /** @var Group[] */
20
    protected $groups = [];
21
22
    /** @var string */
23
    protected $class = "";
24
25
    /** @var DispatcherInterface */
26
    protected $dispatcher;
27
28
    /**
29
     * IndividualUserGroups constructor.
30
     * @param string $class
31
     */
32 12
    public function __construct(DispatcherInterface $dispatcher, $class)
33
    {
34 12
        $this->class = $class;
35 12
        $this->dispatcher = $dispatcher;
36 12
    }
37
38
    /**
39
     * Get the individual group of a pleyer.
40
     *
41
     * @param $login
42
     *
43
     * @return Group
44
     */
45 2
    public function createForPlayer($login)
46
    {
47 2
        if (!isset($this->groups[$login])) {
48 2
            $class = $this->class;
49
            /** @var Group $group */
50 2
            $group = new $class($this->dispatcher);
51
52 2
            $this->groups[$login] = $group;
53 2
            $group->addLogin($login);
54
        }
55
56 2
        return $this->groups[$login];
57
    }
58
59
    /**
60
     * Create a group for
61
     *
62
     * @param string[]
63
     *
64
     * @return Group
65
     */
66 4
    public function createForPlayers($logins)
67
    {
68 4
        $class = $this->class;
69
        /** @var Group $group */
70 4
        $group = new $class($this->dispatcher);
71
72 4
        $this->groups[$group->getName()] = $group;
73 4
        foreach ($logins as $login) {
74 4
            $group->addLogin($login);
75
        }
76
77 4
        return $group;
78
    }
79
80
    /**
81
     * Create a persistend group.
82
     *
83
     * @param $name
84
     * @return Group
85
     */
86 4
    public function create($name)
87
    {
88 4
        $class = $this->class;
89
        /** @var Group $group */
90 4
        $group = new $class($this->dispatcher, $name);
91 4
        $this->groups[$group->getName()] = $group;
92
93 4
        return $group;
94
    }
95
96
    /**
97
     * Get a group from it's name if it exist.
98
     *
99
     * @param $groupName
100
     *
101
     * @return Group|null
102
     */
103 4
    public function getGroup($groupName)
104
    {
105 4
        return isset($this->groups[$groupName]) ? $this->groups[$groupName] : null;
106
    }
107
108
    /**
109
     * When a group is destyoed delete object.
110
     *
111
     * @param Group $group
112
     * @param $lastLogin
113
     */
114 1
    public function onExpansionGroupDestroy(Group $group, $lastLogin)
115
    {
116 1
        if (isset($this->groups[$group->getName()])) {
117 1
            unset($this->groups[$group->getName()]);
118
        }
119 1
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124 1
    public function onExpansionGroupAddUser(Group $group, $loginAdded)
125
    {
126
        // Nothing to
127 1
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132 2
    public function onExpansionGroupRemoveUser(Group $group, $loginRemoved)
133
    {
134 2
        if (isset($this->groups[$loginRemoved])) {
135 1
            unset($this->groups[$loginRemoved]);
136
        }
137 2
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    public function onPlayerConnect(Player $player)
143
    {
144
        // Nothing to do as the plugin don't now the rules to add or remove players to groups;
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
151
    {
152
        foreach ($this->groups as $group) {
153
            $group->removeLogin($player->getLogin());
154
        }
155
    }
156
157
    /**
158
     * @inheritdoc
159
     */
160
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
161
    {
162
        // Nothing to do as the plugin don't now the rules to add or remove players to groups;
163
    }
164
165
    /**
166
     * @inheritdoc
167
     */
168
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
169
    {
170
        // Nothing to do as the plugin don't now the rules to add or remove players to groups;
171
    }
172
}