Completed
Branch master (220ce5)
by De Cramer
16:11
created

GroupsPlugin   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 9.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75.86%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 8
loc 83
rs 10
c 0
b 0
f 0
ccs 22
cts 29
cp 0.7586

6 Methods

Rating   Name   Duplication   Size   Complexity  
A onPlayerDisconnect() 0 4 1
A onPlayerInfoChanged() 0 4 1
A onPlayerAlliesChanged() 0 4 1
A __construct() 0 15 2
A setStatus() 8 8 4
A onPlayerConnect() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace eXpansion\Framework\AdminGroups\Plugins;
4
5
use eXpansion\Framework\AdminGroups\Services\AdminGroupConfiguration;
6
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyPlayer;
7
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
8
use eXpansion\Framework\Core\Plugins\UserGroups\Factory;
9
use eXpansion\Framework\Core\Storage\Data\Player;
10
use eXpansion\Framework\Core\Storage\PlayerStorage;
11
12
/**
13
 * Class GroupsPlugin will keep the admin groups uptodate with the proper player information.
14
 *
15
 * @package eXpansion\Bundle\AdminGroupConfiguration\Plugins;
16
 * @author oliver de Cramer <[email protected]>
17
 */
18
class GroupsPlugin implements ListenerInterfaceMpLegacyPlayer, StatusAwarePluginInterface
19
{
20
    /** @var  AdminGroupConfiguration */
21
    protected $adminGroupConfiguration;
22
23
    /** @var  Factory */
24
    protected $userGroupFactory;
25
26
    /** @var  PlayerStorage */
27
    protected $playerStorage;
28
29
    /**
30
     * GroupsPlugin constructor.
31
     *
32
     * @param AdminGroupConfiguration $adminGroupConfiguration
33
     * @param Factory $userGroupFactory
34
     */
35 2
    public function __construct(
36
        AdminGroupConfiguration $adminGroupConfiguration,
37
        Factory $userGroupFactory,
38
        PlayerStorage $playerStorage
39
    ) {
40 2
        $this->adminGroupConfiguration = $adminGroupConfiguration;
41 2
        $this->userGroupFactory = $userGroupFactory;
42 2
        $this->playerStorage = $playerStorage;
43
44
        // Create a user group for each admin group & for guest players.
45 2
        foreach ($this->adminGroupConfiguration->getGroups() as $groupName) {
46 2
            $this->userGroupFactory->create("admin:$groupName");
47 2
        }
48 2
        $this->userGroupFactory->create('admin:guest');
49 2
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 View Code Duplication
    public function setStatus($status)
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...
55
    {
56
        if ($status && !empty($this->playerStorage->getOnline())) {
57
            foreach ($this->playerStorage->getOnline() as $player) {
58
                $this->onPlayerConnect($player);
59
            }
60
        }
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 1
    public function onPlayerConnect(Player $player)
67
    {
68 1
        $groupName = $this->adminGroupConfiguration->getLoginGroupName($player->getLogin());
69
70 1
        if ($groupName === null) {
71 1
            $this->userGroupFactory->getGroup('admin:guest')->addLogin($player->getLogin());
72 1
        } else {
73 1
            $this->userGroupFactory->getGroup("admin:$groupName")->addLogin($player->getLogin());
74
        }
75 1
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 1
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
81
    {
82
        // Nothing to do here, user group factory will handle it.
83 1
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 1
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
89
    {
90
        // nothing to do here, info changed don't affect admin status of a player.
91 1
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96 1
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
97
    {
98
        // nothing to do here, info changed don't affect admin status of a player.
99 1
    }
100
}
101