Completed
Pull Request — master (#130)
by De Cramer
07:55
created

MenuContentFactory::createTabsMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace eXpansion\Bundle\Menu\Plugins\Gui;
4
5
use eXpansion\Bundle\Menu\DataProviders\MenuItemProvider;
6
use eXpansion\Bundle\Menu\Gui\MenuTabsFactory;
7
use eXpansion\Bundle\Menu\Model\Menu\ItemInterface;
8
use eXpansion\Bundle\Menu\Model\Menu\ParentItem;
9
use eXpansion\Framework\Core\Model\Gui\Manialink;
10
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
11
use eXpansion\Framework\Core\Model\Gui\ManiaScriptFactory;
12
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
13
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
14
use eXpansion\Framework\Gui\Components\uiButton;
15
use eXpansion\Framework\Gui\Components\uiLabel;
16
use FML\Controls\Frame;
17
use FML\Controls\Label;
18
use FML\Controls\Quad;
19
20
21
/**
22
 * Class MenuContentFactory
23
 *
24
 * @package eXpansion\Bundle\Menu\Plugins\Gui;
25
 * @author  oliver de Cramer <[email protected]>
26
 */
27
class MenuContentFactory extends WidgetFactory
28
{
29
    /** @var  MenuItemProvider */
30
    protected $menuItemProvider;
31
32
    /** @var  ManiaScriptFactory */
33
    protected $menuScriptFactory;
34
35
    /** @var  MenuTabsFactory */
36
    protected $menuTabsFactory;
37
38
    /** @var string */
39
    protected $currentPath = 'admin';
40
41
    /**
42
     * MenuContentFactory constructor.
43
     *
44
     * @param                      $name
45
     * @param                      $sizeX
46
     * @param                      $sizeY
47
     * @param null                 $posX
48
     * @param null                 $posY
49
     * @param WidgetFactoryContext $context
50
     * @param MenuItemProvider     $menuItemProvider
51
     * @param MenuTabsFactory      $menuTabsFactory
52
     */
53
    public function __construct(
54
        $name,
55
        $sizeX,
56
        $sizeY,
57
        $posX,
58
        $posY,
59
        WidgetFactoryContext $context,
60
        ManiaScriptFactory $maniaScriptFactory,
61
        MenuItemProvider $menuItemProvider,
62
        MenuTabsFactory $menuTabsFactory
63
    ) {
64
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
65
66
        $this->menuItemProvider = $menuItemProvider;
67
        $this->menuScriptFactory = $maniaScriptFactory;
68
        $this->menuTabsFactory = $menuTabsFactory;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    protected function createContent(ManialinkInterface $manialink)
75
    {
76
        parent::createContent($manialink);
77
78
        $tabsFrame = new Frame('tabs');
79
        $tabsFrame->setPosition(-144, 82);
80
        $manialink->getContentFrame()->setZ(101);
81
        $manialink->getContentFrame()->addChild($tabsFrame);
82
        $manialink->setData('tabs_frame', $tabsFrame);
83
84
        $contentFrame = new Frame('menu_content');
85
        $contentFrame->setPosition(0, 72);
86
        $manialink->getContentFrame()->addChild($contentFrame);
87
        $manialink->setData('menu_content_frame', $contentFrame);
88
89
        $backGroundFrame = new Frame('background');
90
        $manialink->getContentFrame()->addChild($backGroundFrame);
91
92
93
        /*
94
         * Adding background frame
95
         */
96
        $bgFrame = Frame::create("background");
97
98
        $quad = new Quad();
99
        $quad->addClass("bg")
100
            ->setId("mainBg")
101
            ->setPosition(0, 0)
102
            ->setSize(322, 182);
103
        $quad->setAlign("center", "center")
104
            ->setStyles("Bgs1", "BgDialogBlur");
105
        $bgFrame->addChild($quad);
106
107
        $manialink->getContentFrame()->addChild($bgFrame);
108
109
        /**
110
         * Adding script
111
         */
112
        $manialink->getFmlManialink()->addChild($this->menuScriptFactory->createScript([]));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eXpansion\Framework\Core...\Gui\ManialinkInterface as the method getFmlManialink() does only exist in the following implementations of said interface: eXpansion\Framework\Core\Model\Gui\Widget, eXpansion\Framework\Core\Model\Gui\Window.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    protected function updateContent(ManialinkInterface $manialink)
119
    {
120
        parent::updateContent($manialink);
121
122
        $currentPath = $manialink->getData('current_path');
123
        if (is_null($currentPath)) {
124
            $currentPath = $this->currentPath;
125
            $manialink->setData('current_path', $currentPath);
126
        }
127
        $currentPath = trim($currentPath, '/');
128
129
130
        $rootItem = $this->menuItemProvider->getRootItem();
131
        $pathParts = explode('/', $currentPath);
132
133
        $this->createTabsMenu($manialink, $rootItem, $pathParts[0]);
0 ignored issues
show
Documentation introduced by
$rootItem 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...
134
135
        /** @var Frame $contentFrame */
136
        $contentFrame = $manialink->getData('menu_content_frame');
137
        $contentFrame->removeAllChildren();
138
139
        $displayLevel = 0;
140
        for ($i = count($pathParts) - 1; $i >= 0; $i--) {
141
            $path = implode('/', array_slice($pathParts, 0, $i + 1));
142
143
            /** @var ParentItem $parentItem */
144
            $parentItem = $rootItem->getChild($path);
145
146
            $this->createSubMenu($manialink, $contentFrame, $parentItem, $displayLevel++);
0 ignored issues
show
Compatibility introduced by
$manialink of type object<eXpansion\Framewo...Gui\ManialinkInterface> is not a sub-type of object<eXpansion\Framewo...re\Model\Gui\Manialink>. It seems like you assume a concrete implementation of the interface eXpansion\Framework\Core...\Gui\ManialinkInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
147
        }
148
    }
149
150
    /**
151
     * Create tabs level menu.
152
     *
153
     * @param ManialinkInterface $manialink
154
     * @param ParentItem         $rootItem
155
     * @param                    $openId
156
     */
157
    protected function createTabsMenu(ManialinkInterface $manialink, ParentItem $rootItem, $openId)
158
    {
159
        /** @var Frame $tabsFrame */
160
        $tabsFrame = $manialink->getData('tabs_frame');
161
        $tabsFrame->removeAllChildren();
162
163
        $this->menuTabsFactory->createTabsMenu($manialink, $tabsFrame, $rootItem, $openId);
164
    }
165
166
    /**
167
     * Create content for sub menu.
168
     *
169
     * @param Manialink  $manialink
170
     * @param Frame      $frame
171
     * @param ParentItem $parentItem
172
     * @param            $displayLevel
173
     */
174
    protected function createSubMenu(Manialink $manialink, Frame $frame, ParentItem $parentItem, $displayLevel)
175
    {
176
        $posX = $displayLevel * (-160.0/3);
177
        $posY = ($displayLevel * (-100.0/3)) * 0.5;
178
        $scale = (0.5 / ($displayLevel + 1)) + 0.5;
179
180
        $contentFrame = new Frame();
181
        $contentFrame->setScale($scale);
182
        $contentFrame->setPosition($posX, $posY);
183
        $frame->addChild($contentFrame);
184
185
        if ($displayLevel > 0) {
186
            $overlay = new Quad();
187
            $overlay->setSize(60, 120);
188
            $overlay->setPosition(-30, 0);
189
            $overlay->setStyles("Bgs1", "BgDialogBlur");
190
191
            $action = $this->actionFactory->createManialinkAction(
192
                $manialink,
193
                [$this, 'callbackItemClick'],
194
                ['item' => $parentItem, 'ml' => $manialink]
195
            );
196
197
198
            $contentFrame->addChild($overlay);
199
            $overlay->setAction($action);
200
        }
201
202
        /* TITLE */
203
        $titleLabel = $this->uiFactory->createLabel($parentItem->getLabelId(), uiLabel::TYPE_TITLE);
204
        $titleLabel->setSize(60, 8);
205
        $titleLabel->setPosition(-30, 0);
206
        $titleLabel->setTranslate(true);
207
        $contentFrame->addChild($titleLabel);
208
209
        $titleLine = $this->uiFactory->createLine(-30, -8);
210
        $titleLine->to(30, -8);
211
        $contentFrame->addChild($titleLine);
212
213
        $posY = -12;
214
        foreach ($parentItem->getChilds() as $item) {
215
            if ($item->isVisibleFor($manialink->getUserGroup())) {
0 ignored issues
show
Documentation introduced by
$manialink->getUserGroup() is of type object<eXpansion\Framewo...Model\UserGroups\Group>, but the function expects a string.

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...
216
                $button = $this->uiFactory->createButton($item->getLabelId());
217
                $button->setPosition(-25, $posY);
218
                $button->setSize(50, 8);
219
                $button->setTranslate(true);
220
221
                if ($displayLevel == 0) {
222
                    $action = $this->actionFactory->createManialinkAction(
223
                        $manialink,
224
                        [$this, 'callbackItemClick'],
225
                        ['item' => $item, 'ml' => $manialink]
226
                    );
227
                    $button->setAction($action);
228
                }
229
230
                $contentFrame->addChild($button);
231
                $posY -= 12;
232
            }
233
        }
234
235
        if ($displayLevel == 0) {
236
237
            $button = $this->uiFactory->createButton('expansion_menu.menu_close', uiButton::TYPE_DECORATED);
238
            $button->setBackgroundColor(uiButton::COLOR_WARNING);
239
            $button->setPosition(-25, $posY - 12);
240
            $button->setSize(50, 8);
241
            $button->setTranslate(true);
242
            $action = $this->actionFactory->createManialinkAction($manialink, [$this, 'callbackClose'], ['ml' =>$manialink]);
243
            $button->setAction($action);
244
            $contentFrame->addChild($button);
245
        }
246
    }
247
248
    /**
249
     * Callback when an item of the menu is clicked on.
250
     *
251
     * @param $login
252
     * @param $params
253
     * @param $args
254
     */
255
    public function callbackItemClick($login, $params, $args)
256
    {
257
        /** @var ItemInterface $item */
258
        $item = $args['item'];
259
        $item->execute($this, $args['ml'], $login, $params, $args);
260
    }
261
262
    /**
263
     * Callback when the close button is clicked.
264
     *
265
     * @param $login
266
     * @param $params
267
     * @param $args
268
     */
269
    public function callbackClose($login, $params, $args)
270
    {
271
        $this->destroy($args['ml']->getUserGroup());
272
    }
273
}