Completed
Pull Request — master (#51)
by De Cramer
02:42
created

WindowFactory::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 0
cts 28
cp 0
rs 8.8571
cc 1
eloc 25
nc 1
nop 11
crap 2

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
use eXpansion\Framework\Core\Helpers\Translations;
5
use eXpansion\Framework\Core\Model\Gui\Manialink;
6
use eXpansion\Framework\Core\Model\Gui\ManiaScriptFactory;
7
use eXpansion\Framework\Core\Model\Gui\Window;
8
use eXpansion\Framework\Core\Model\UserGroups\Group;
9
use eXpansion\Framework\Core\Plugins\GuiHandler;
10
use eXpansion\Framework\Core\Plugins\UserGroups\Factory;
11
use FML\Controls\Control;
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 WindowFactory extends ManialinkFactory {
20
21
    /** @var ManiaScriptFactory */
22
    protected $windowManiaScriptFactory;
23
24
    /** @var Translations */
25
    protected $translationsHelper;
26
27
    public function __construct(
28
        $name,
29
        $sizeX,
30
        $sizeY,
31
        $posX,
32
        $posY,
33
        GuiHandler $guiHandler,
34
        Factory $groupFactory,
35
        ActionFactory $actionFactory,
36
        ManiaScriptFactory $windowManiaScriptFactory,
37
        Translations $translationsHelper,
38
        $className = Window::class
39
    ) {
40
        // Hack for FML to use default MP alignements.
41
        Control::clearDefaultAlign();
42
43
        parent::__construct(
44
            $name,
45
            $sizeX,
46
            $sizeY,
47
            $posX,
48
            $posY,
49
            $guiHandler,
50
            $groupFactory,
51
            $actionFactory,
52
            $className
53
        );
54
55
        $this->windowManiaScriptFactory = $windowManiaScriptFactory;
56
        $this->translationsHelper = $translationsHelper;
57
    }
58
59
    /**
60
     * @param Group $group
61
     *
62
     * @return Window
63
     */
64
    protected function createManialink(Group $group)
65
    {
66
        $className = $this->className;
67
        $manialink = new $className(
68
            $group,
69
            $this->windowManiaScriptFactory,
70
            $this->translationsHelper,
71
            $this->name,
72
            $this->sizeX,
73
            $this->sizeY,
74
            $this->posX,
75
            $this->posY
76
        );
77
78
        $actionId = $this->actionFactory->createManialinkAction(
79
            $manialink,
80
            [$this, 'closeManialink'],
81
            ['manialink' => $manialink]
82
        );
83
84
        $manialink->setCloseAction($actionId);
85
86
        return $manialink;
87
    }
88
89
    public function closeManialink($login, $answerValues, $arguments)
90
    {
91
        /** @var Manialink $manialink */
92
        $manialink = $arguments['manialink'];
93
        $this->destroy($manialink->getUserGroup());
94
    }
95
}
96