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

Factory::onExpansionGroupRemoveUser()   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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
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\UserGroups;
4
5
use eXpansion\Core\DataProviders\Listener\UserGroupDataListenerInterface;
6
use eXpansion\Core\Model\UserGroups\Group;
7
use eXpansion\Core\Services\Application\DispatcherInterface;
8
9
/**
10
 * Class Factory handles non persistent user groups.
11
 *
12
 * @package eXpansion\Core\Plugins
13
 * @author Oliver de Cramer
14
 */
15
class Factory implements UserGroupDataListenerInterface
16
{
17
    /** @var Group[] */
18
    protected $groups = [];
19
20
    /** @var string */
21
    protected $class = "";
22
23
    /** @var DispatcherInterface */
24
    protected $dispatcher;
25
26
    /**
27
     * IndividualUserGroups constructor.
28
     * @param string $class
29
     */
30 3
    public function __construct(DispatcherInterface $dispatcher, $class)
31
    {
32 3
        $this->class = $class;
33 3
        $this->dispatcher = $dispatcher;
34 3
    }
35
36
    /**
37
     * Get the individual group of a pleyer.
38
     *
39
     * @param $login
40
     *
41
     * @return Group
42
     */
43 2
    public function createForPlayer($login)
44
    {
45 2
        if (!isset($this->groups[$login])) {
46 2
            $class = $this->class;
47
            /** @var Group $group */
48 2
            $group = new $class($this->dispatcher);
49
50 2
            $this->groups[$login] = $group;
51 2
            $group->addLogin($login);
52
        }
53
54 2
        return $this->groups[$login];
55
    }
56
57
    /**
58
     * Create a group for
59
     *
60
     * @param Group
61
     */
62 1
    public function createForPlayers($logins)
63
    {
64 1
        $class = $this->class;
65
        /** @var Group $group */
66 1
        $group = new $class($this->dispatcher);
67
68 1
        $this->groups[$group->getName()] = $group;
69 1
        foreach ($logins as $login) {
70 1
            $group->addLogin($login);
71
        }
72
73 1
        return $group;
74
    }
75
76
    /**
77
     * Get a group from it's name if it exist.
78
     *
79
     * @param $groupName
80
     *
81
     * @return Group|null
82
     */
83 1
    public function getGroup($groupName)
84
    {
85 1
        return isset($this->groups[$groupName]) ? $this->groups[$groupName] : null;
86
    }
87
88
    /**
89
     * When a group is destyoed delete object.
90
     *
91
     * @param Group $group
92
     * @param $lastLogin
93
     */
94 1
    public function onExpansionGroupDestroy(Group $group, $lastLogin)
95
    {
96 1
        if (isset($this->groups[$group->getName()])) {
97 1
            unset($this->groups[$group->getName()]);
98
        }
99 1
    }
100
101 1
    public function onExpansionGroupAddUser(Group $group, $loginAdded)
102
    {
103
        // Nothing to
104 1
    }
105
106 2
    public function onExpansionGroupRemoveUser(Group $group, $loginRemoved)
107
    {
108 2
        if (isset($this->groups[$loginRemoved])) {
109 1
            unset($this->groups[$loginRemoved]);
110
        }
111
    }
112
}