Completed
Pull Request — master (#95)
by
unknown
04:44
created

ManialinkFactory::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 3
eloc 23
nc 4
nop 9
crap 3

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\ManialinkFactoryInterface;
8
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
9
use eXpansion\Framework\Core\Model\UserGroups\Group;
10
use eXpansion\Framework\Core\Plugins\GuiHandler;
11
use eXpansion\Framework\Core\Plugins\UserGroups\Factory;
12
13
/**
14
 * Class ManialiveFactory allow the creation of manialinks.
15
 *
16
 * @package eXpansion\Framework\Core\Plugins\Gui
17
 * @author Oliver de Cramer
18
 */
19
class ManialinkFactory implements ManialinkFactoryInterface, ListenerInterfaceExpUserGroup
20
{
21
    /** @var  GuiHandler */
22
    protected $guiHandler;
23
24
    /** @var Factory */
25
    protected $groupFactory;
26
27
    /** @var ActionFactory */
28
    protected $actionFactory;
29
30
    /** @var  string */
31
    protected $name;
32
33
    /** @var  string */
34
    protected $className;
35
36
    /** @var ManialinkInterface[] */
37
    protected $manialinks = [];
38
39
    /** @var Group[] */
40
    protected $groups = [];
41
42
    /** @var float|int */
43
    protected $sizeX;
44
45
    /** @var float|int */
46
    protected $sizeY;
47
48
    /** @var float|int */
49
    protected $posX;
50
51
    /** @var float|int */
52
    protected $posY;
53
54
    /**
55
     * GroupManialinkFactory constructor.
56
     *
57
     * @param GuiHandler $guiHandler
58
     * @param Factory $groupFactory
59
     * @param ActionFactory $actionFactory
60
     * @param $name
61
     * @param $sizeX
62
     * @param $sizeY
63
     * @param null $posX
64
     * @param null $posY
65
     * @param string $className
66
     */
67 6
    public function __construct(
68
        $name,
69
        $sizeX,
70
        $sizeY,
71
        $posX = null,
72
        $posY = null,
73
        GuiHandler $guiHandler,
74
        Factory $groupFactory,
75
        ActionFactory $actionFactory,
76
        $className = Manialink::class
77
    ) {
78 6
        if (is_null($posX)) {
79 6
            $posX = $sizeX / -2;
80
        }
81
82 6
        if (is_null($posY)) {
83 6
            $posY = $sizeY / 2;
84
        }
85
86 6
        $this->guiHandler = $guiHandler;
87 6
        $this->groupFactory = $groupFactory;
88 6
        $this->actionFactory = $actionFactory;
89 6
        $this->name = $name;
90 6
        $this->className = $className;
91 6
        $this->sizeX = $sizeX;
92 6
        $this->sizeY = $sizeY;
93 6
        $this->posX = $posX;
94 6
        $this->posY = $posY;
95 6
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 5
    final public function create($group)
101
    {
102 5
        if (is_string($group)) {
103 1
            $group = $this->groupFactory->createForPlayer($group);
104
        } else {
105 4
            if (is_array($group)) {
106 3
                $group = $this->groupFactory->createForPlayers($group);
107
            }
108
        }
109
110 5
        if (isset($this->manialinks[$group->getName()])) {
111
            $this->update($group);
112
113
            return $group;
114
        }
115
116 5
        $this->manialinks[$group->getName()] = $this->createManialink($group);
117 5
        $this->guiHandler->addToDisplay($this->manialinks[$group->getName()]);
118
119 5
        $this->createContent($this->manialinks[$group->getName()]);
120 5
        $this->updateContent($this->manialinks[$group->getName()]);
121
122 5
        return $group;
123
    }
124
125
    /**
126
     * @inheritdoc
127
     * @param Group $group
128
     */
129 4
    final public function update($group)
130
    {
131 4
        if (isset($this->manialinks[$group->getName()])) {
132
            $this->guiHandler->addToDisplay($this->manialinks[$group->getName()]);
133
            $this->updateContent($this->manialinks[$group->getName()]);
134
        }
135 4
    }
136
137
    /**
138
     * Create content in the manialink.
139
     *
140
     * @param ManialinkInterface $manialink
141
     *
142
     */
143 5
    protected function createContent(ManialinkInterface $manialink)
144
    {
145
        // Put content in the manialink here.
146 5
    }
147
148
    /**
149
     * Update content in the manialink.
150
     *
151
     * @param ManialinkInterface $manialink
152
     *
153
     */
154 5
    protected function updateContent(ManialinkInterface $manialink)
155
    {
156
        // Put content in the manialink here.
157 5
    }
158
159
    /**
160
     * @inheritdoc
161
     */
162 1
    final public function destroy(Group $group)
163
    {
164 1
        if (isset($this->manialinks[$group->getName()])) {
165 1
            $this->guiHandler->addToHide($this->manialinks[$group->getName()]);
166 1
            $this->actionFactory->destroyManialinkActions($this->manialinks[$group->getName()]);
167 1
            unset($this->manialinks[$group->getName()]);
168
        }
169 1
    }
170
171
    /**
172
     * Create manialink object for user group.
173
     *
174
     * @param Group $group
175
     *
176
     * @return Manialink
177
     */
178 5
    protected function createManialink(Group $group)
179
    {
180 5
        $className = $this->className;
181
182 5
        return new $className($group, $this->name, $this->sizeX, $this->sizeY, $this->posX, $this->posY);
183
    }
184
185
    /**
186
     * When a gup is destroyed, destroy manialinks of the group.
187
     *
188
     * @param Group $group
189
     * @param $lastLogin
190
     *
191
     * @return void
192
     */
193 1
    public function onExpansionGroupDestroy(Group $group, $lastLogin)
194
    {
195 1
        if (isset($this->manialinks[$group->getName()])) {
196
            // Gui Handler will handle delete by it's own.
197 1
            $this->actionFactory->destroyManialinkActions($this->manialinks[$group->getName()]);
198 1
            unset($this->manialinks[$group->getName()]);
199
        }
200 1
    }
201
202 1
    public function onExpansionGroupAddUser(Group $group, $loginAdded)
203
    {
204
        // nothing to do here.
205 1
    }
206
207 1
    public function onExpansionGroupRemoveUser(Group $group, $loginRemoved)
208
    {
209
        // nothing to do here.
210 1
    }
211
212
}
213