Completed
Branch master (83a2f0)
by De Cramer
02:40
created

ManialinkFactory::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Plugins\Gui;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpUserGroup;
6
use eXpansion\Framework\Core\Model\Gui\Manialink;
7
use eXpansion\Framework\Core\Model\Gui\ManialinkFactoryContext;
8
use eXpansion\Framework\Core\Model\Gui\ManialinkFactoryInterface;
9
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
10
use eXpansion\Framework\Core\Model\UserGroups\Group;
11
use eXpansion\Framework\Core\Plugins\GuiHandler;
12
use eXpansion\Framework\Core\Plugins\UserGroups\Factory;
13
14
/**
15
 * Class ManialiveFactory allow the creation of manialinks.
16
 *
17
 * @package eXpansion\Framework\Core\Plugins\Gui
18
 * @author Oliver de Cramer
19
 */
20
class ManialinkFactory implements ManialinkFactoryInterface, ListenerInterfaceExpUserGroup
21
{
22
    /** @var  GuiHandler */
23
    protected $guiHandler;
24
25
    /** @var Factory */
26
    protected $groupFactory;
27
28
    /** @var ActionFactory */
29
    protected $actionFactory;
30
31
    /** @var  string */
32
    protected $name;
33
34
    /** @var  string */
35
    protected $className;
36
37
    /** @var Group[] */
38
    protected $groups = [];
39
40
    /** @var float|int */
41
    protected $sizeX;
42
43
    /** @var float|int */
44
    protected $sizeY;
45
46
    /** @var float|int */
47
    protected $posX;
48
49
    /** @var float|int */
50
    protected $posY;
51
52
    /**
53
     * ManialinkFactory constructor.
54
     *
55
     * @param                         $name
56
     * @param                         $sizeX
57
     * @param                         $sizeY
58
     * @param null $posX
59
     * @param null $posY
60
     * @param ManialinkFactoryContext $context
61
     */
62 6
    public function __construct(
63
        $name,
64
        $sizeX,
65
        $sizeY,
66
        $posX = null,
67
        $posY = null,
68
        ManialinkFactoryContext $context
69
    ) {
70 6
        if (is_null($posX)) {
71 6
            $posX = $sizeX / -2;
72
        }
73
74 6
        if (is_null($posY)) {
75 6
            $posY = $sizeY / 2;
76
        }
77
78 6
        $this->guiHandler = $context->getGuiHandler();
79 6
        $this->groupFactory = $context->getGroupFactory();
80 6
        $this->actionFactory = $context->getActionFactory();
81 6
        $this->className = $context->getClassName();
82 6
        $this->name = $name;
83 6
        $this->sizeX = $sizeX;
84 6
        $this->sizeY = $sizeY;
85 6
        $this->posX = $posX;
86 6
        $this->posY = $posY;
87 6
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getId()
93
    {
94
        return spl_object_hash($this);
95
    }
96
97
    /**
98
     * @inheritdoc
99
     * @param string|array|Group $group
100
     */
101 4
    final public function create($group)
102
    {
103 4 View Code Duplication
        if (is_string($group)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
104 1
            $group = $this->groupFactory->createForPlayer($group);
105
        } else {
106 3
            if (is_array($group)) {
107 2
                $group = $this->groupFactory->createForPlayers($group);
108
            }
109
        }
110
111 4
        if (!is_null($this->guiHandler->getManialink($group, $this))) {
112 1
            $this->update($group);
113 1
            return $group;
114
        }
115
116
117 3
        $ml = $this->createManialink($group);
118 3
        $this->guiHandler->addToDisplay($ml, $this);
119
120 3
        $this->createContent($ml);
121 3
        $this->updateContent($ml);
122
123 3
        return $group;
124
    }
125
126
    /**
127
     * @inheritdoc
128
     * @param string|array|Group $group
129
     */
130 1
    final public function update($group)
131
    {
132 1 View Code Duplication
        if (is_string($group)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
133
            $group = $this->groupFactory->createForPlayer($group);
134
        } else {
135 1
            if (is_array($group)) {
136
                $group = $this->groupFactory->createForPlayers($group);
137
            }
138
        }
139
140 1
        $ml = $this->guiHandler->getManialink($group, $this);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ml is correct as $this->guiHandler->getManialink($group, $this) (which targets eXpansion\Framework\Core...Handler::getManialink()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
141 1
        if ($ml) {
142 1
            $this->updateContent($ml);
143 1
            $this->guiHandler->addToDisplay($ml, $this);
144
        }
145 1
    }
146
147
    /**
148
     * Create content in the manialink.
149
     *
150
     * @param ManialinkInterface $manialink
151
     *
152
     */
153 3
    protected function createContent(ManialinkInterface $manialink)
154
    {
155
        // Put content in the manialink here.
156 3
    }
157
158
    /**
159
     * Update content in the manialink.
160
     *
161
     * @param ManialinkInterface $manialink
162
     *
163
     */
164 4
    protected function updateContent(ManialinkInterface $manialink)
165
    {
166
        // Put content in the manialink here.
167 4
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172 1
    final public function destroy(Group $group)
173
    {
174 1
        $ml = $this->guiHandler->getManialink($group, $this);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ml is correct as $this->guiHandler->getManialink($group, $this) (which targets eXpansion\Framework\Core...Handler::getManialink()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
175 1
        if ($ml) {
176 1
            $this->guiHandler->addToHide($ml, $this);
177
        }
178 1
    }
179
180
    /**
181
     * Create manialink object for user group.
182
     *
183
     * @param Group $group
184
     *
185
     * @return Manialink
186
     */
187 3
    protected function createManialink(Group $group)
188
    {
189 3
        $className = $this->className;
190
191 3
        return new $className($group, $this->name, $this->sizeX, $this->sizeY, $this->posX, $this->posY);
192
    }
193
194 1
    public function onExpansionGroupDestroy(Group $group, $lastLogin)
195
    {
196
        // nothing to do here.
197 1
    }
198
199 1
    public function onExpansionGroupAddUser(Group $group, $loginAdded)
200
    {
201
        // nothing to do here.
202 1
    }
203
204 1
    public function onExpansionGroupRemoveUser(Group $group, $loginRemoved)
205
    {
206
        // nothing to do here.
207 1
    }
208
209
}
210