Completed
Push — odev ( 21e788...b6090c )
by De Cramer
02:32
created

GroupsPlugin::onPlayerDisconnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace eXpansion\Framework\AdminGroups\Plugins;
4
5
use eXpansion\Framework\AdminGroups\Services\AdminGroupConfiguration;
6
use eXpansion\Framework\Core\DataProviders\Listener\PlayerDataListenerInterface;
7
use eXpansion\Framework\Core\Plugins\UserGroups\Factory;
8
use eXpansion\Framework\Core\Storage\Data\Player;
9
10
/**
11
 * Class GroupsPlugin will keep the admin groups uptodate with the proper player information.
12
 *
13
 * @package eXpansion\Bundle\AdminGroupConfiguration\Plugins;
14
 * @author oliver de Cramer <[email protected]>
15
 */
16
class GroupsPlugin implements PlayerDataListenerInterface
17
{
18
    /** @var  AdminGroupConfiguration */
19
    protected $adminGroupConfiguration;
20
21
    /** @var  Factory */
22
    protected $userGroupFactory;
23
24
    /**
25
     * GroupsPlugin constructor.
26
     *
27
     * @param AdminGroupConfiguration $adminGroupConfiguration
28
     * @param Factory $userGroupFactory
29
     */
30 2
    public function __construct(
31
        AdminGroupConfiguration $adminGroupConfiguration,
32
        Factory $userGroupFactory
33
    ) {
34 2
        $this->adminGroupConfiguration = $adminGroupConfiguration;
35 2
        $this->userGroupFactory = $userGroupFactory;
36
37 2
        foreach ($this->adminGroupConfiguration->getGroups() as $groupName){
38 2
            $this->userGroupFactory->create("admin:$groupName");
39
        }
40
41 2
        $this->userGroupFactory->create('admin:guest');
42 2
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 1
    public function onPlayerConnect(Player $player)
48
    {
49 1
        $groupName = $this->adminGroupConfiguration->getLoginGroupName($player->getLogin());
50
51 1
        if (empty($groupName)) {
52 1
            $this->userGroupFactory->getGroup('admin:guest')->addLogin($player->getLogin());
53
        } else {
54 1
            $this->userGroupFactory->getGroup("admin:$groupName")->addLogin($player->getLogin());
55
        }
56 1
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 1
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
62
    {
63
        // Nothing to do here, user group factory will handle it.
64 1
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 1
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
70
    {
71
        // nothing to do here, info changed don't affect admin status of a player.
72 1
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 1
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
78
    {
79
        // nothing to do here, info changed don't affect admin status of a player.
80
    }
81
}