Completed
Pull Request — master (#130)
by De Cramer
02:47
created

WindowFrameFactory::setManialinkInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace eXpansion\Bundle\ImmersiveWindows\Model\Gui\Factory;
4
5
use eXpansion\Bundle\ImmersiveWindows\Plugins\WindowsGuiHandler;
6
use eXpansion\Bundle\Menu\DataProviders\MenuItemProvider;
7
use eXpansion\Bundle\Menu\Gui\MenuTabsFactory;
8
use eXpansion\Bundle\Menu\Model\Menu\ItemInterface;
9
use eXpansion\Bundle\Menu\Plugins\Gui\MenuContentFactory;
10
use eXpansion\Framework\Core\Model\Gui\Factory\WindowFrameFactory as OriginalWindowFrameFactory;
11
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
12
use eXpansion\Framework\Core\Model\Gui\ManiaScriptFactory;
13
use eXpansion\Framework\Core\Model\Gui\WindowFrameFactoryInterface;
14
use eXpansion\Framework\Gui\Components\uiButton;
15
use eXpansion\Framework\Gui\Ui\Factory;
16
use FML\Controls\Frame;
17
use FML\Controls\Label;
18
use FML\Controls\Quad;
19
use FML\ManiaLink;
20
21
/**
22
 * Class WindowFrameFactory
23
 *
24
 * @author    de Cramer Oliver<[email protected]>
25
 * @copyright 2017 Smile
26
 * @package eXpansion\Bundle\ImmersiveWindows\Model\Gui\Factory
27
 */
28
class WindowFrameFactory extends OriginalWindowFrameFactory implements WindowFrameFactoryInterface
29
{
30
    /** @var MenuTabsFactory */
31
    protected $menuTabsFactory;
32
33
    /** @var MenuItemProvider */
34
    protected $menuItemProvider;
35
36
    /** @var MenuContentFactory */
37
    protected $menuContentFactory;
38
39
    /** @var WindowsGuiHandler */
40
    protected $windowsGuiHandler;
41
42
    /**
43
     * @var ManiaScriptFactory
44
     */
45
    protected $maniaScriptFactory;
46
    /**
47
     * @var ManialinkInterface
48
     */
49
    protected $manialinkInterface;
50
    /**
51
     * @var Factory
52
     */
53
    protected $uiFactory;
54
55
    /**
56
     * WindowFrameFactory constructor.
57
     *
58
     * @param ManiaScriptFactory $maniaScriptFactory
59
     * @param MenuTabsFactory $menuTabsFactory
60
     * @param MenuItemProvider $menuItemProvider
61
     * @param Factory $uiFactory
62
     */
63
    public function __construct(
64
        ManiaScriptFactory $maniaScriptFactory,
65
        MenuTabsFactory $menuTabsFactory,
66
        MenuItemProvider $menuItemProvider,
67
        Factory $uiFactory,
68
        MenuContentFactory $menuContentFactory,
69
        WindowsGuiHandler $windowsGuiHandler
70
    ) {
71
        parent::__construct($maniaScriptFactory);
72
        $this->maniaScriptFactory = $maniaScriptFactory;
73
        $this->menuTabsFactory = $menuTabsFactory;
74
        $this->menuItemProvider = $menuItemProvider;
75
        $this->uiFactory = $uiFactory;
76
        $this->menuContentFactory = $menuContentFactory;
77
        $this->windowsGuiHandler = $windowsGuiHandler;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function build(
84
        ManiaLink $manialink,
85
        Frame $mainFrame,
86
        $name,
87
        $sizeX,
88
        $sizeY
89
    ) {
90
        // Creating sub frame to keep all the pieces together. Position needs to be top left corner.
91
        $frame = new Frame();
92
        $frame->setPosition(-160 - $mainFrame->getX(), 90 - $mainFrame->getY());
93
94
        $tabsFrame = new Frame();
95
        $tabsFrame->setPosition(-144 - $mainFrame->getX(), 82- $mainFrame->getY());
96
97
        // Creating the tabs.
98
        $mainFrame->addChild(
99
            $this->menuTabsFactory->createTabsMenu(
0 ignored issues
show
Documentation introduced by
$this->menuTabsFactory->...Click'), 'admin/admin') is of type object<FML\Types\Container>, but the function expects a object<FML\Types\Renderable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
                $this->manialinkInterface,
101
                $tabsFrame,
102
                $this->menuItemProvider->getRootItem(),
0 ignored issues
show
Documentation introduced by
$this->menuItemProvider->getRootItem() is of type object<eXpansion\Bundle\...enu\ItemInterface>|null, but the function expects a object<eXpansion\Bundle\...\Model\Menu\ParentItem>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
                [$this, 'callbackItemClick'],
104
                'admin/admin'
105
            )
106
        );
107
        $mainFrame->addChild($tabsFrame);
108
        $mainFrame->addChild($frame);
109
110
        $closeButton = $this->uiFactory->createButton('Close', uiButton::TYPE_DECORATED);
111
        $closeButton->setBorderColor(uiButton::COLOR_WARNING)->setFocusColor(uiButton::COLOR_WARNING);
112
        $closeButton->setPosition(300, -20);
113
        $frame->addChild($closeButton);
114
115
116
        /*
117
         * Adding background frame
118
         */
119
        $bgFrame = Frame::create("background");
120
        $quad = new Quad();
121
        $quad->addClass("bg")
122
            ->setId("mainBg")
123
            ->setPosition(0, 0)
124
            ->setSize(322, 182);
125
        $quad->setAlign("left", "top")
126
            ->setStyles("Bgs1", "BgDialogBlur");
127
        $bgFrame->addChild($quad);
128
129
        $frame->addChild($bgFrame);
130
131
132
        $manialink->addChild($this->maniaScriptFactory->createScript(['']));
133
134
        return $closeButton;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $closeButton; (eXpansion\Framework\Gui\Components\uiButton) is incompatible with the return type declared by the interface eXpansion\Framework\Core...FactoryInterface::build of type FML\Controls\Control.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
135
    }
136
137
    /**
138
     * @param ManialinkInterface $manialinkInterface
139
     */
140
    public function setManialinkInterface(ManialinkInterface $manialinkInterface)
141
    {
142
        $this->manialinkInterface = $manialinkInterface;
143
    }
144
145
    /**
146
     * Callback when an item of the menu is clicked on.
147
     *
148
     * @param ManialinkInterface $manialink
149
     * @param $login
150
     * @param $params
151
     * @param $args
152
     */
153
    public function callbackItemClick(ManialinkInterface $manialink, $login, $params, $args)
154
    {
155
        $this->windowsGuiHandler->addToHide($manialink);
156
        $this->menuContentFactory->create($login);
157
158
        /** @var ItemInterface $item */
159
        $item = $args['item'];
160
        $item->execute($this->menuContentFactory, $manialink, $login, $params, $args);
161
    }
162
}
163