Completed
Pull Request — master (#130)
by
unknown
03:02
created

WindowFrameFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 133
ccs 0
cts 77
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B build() 0 87 1
A setManialinkInterface() 0 4 1
A setCloseAction() 0 4 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui\Factory;
4
5
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
6
use eXpansion\Framework\Core\Model\Gui\ManiaScriptFactory;
7
use eXpansion\Framework\Core\Model\Gui\WindowFrameFactoryInterface;
8
use eXpansion\Framework\Gui\Components\uiButton;
9
use FML\Controls\Control;
10
use FML\Controls\Frame;
11
use FML\Controls\Label;
12
use FML\Controls\Quad;
13
use FML\ManiaLink;
14
use FML\Types\Container;
15
16
/**
17
 * Class WindowFrameFactory
18
 *
19
 * @author    de Cramer Oliver<[email protected]>
20
 * @copyright 2017 Smile
21
 * @package eXpansion\Framework\Core\Model\Gui\Factory
22
 */
23
class WindowFrameFactory implements WindowFrameFactoryInterface
24
{
25
    /** @var ManiaScriptFactory */
26
    protected $windowManiaScriptFactory;
27
28
    /** @var ManialinkInterface */
29
    protected $manialinkInterface;
30
31
    /** @var uiButton */
32
    private $closeButton;
33
34
    /**
35
     * WindowFrameFactory constructor.
36
     *
37
     * @param ManiaScriptFactory $maniaScriptFactory
38
     */
39
    public function __construct(ManiaScriptFactory $maniaScriptFactory)
40
    {
41
        $this->windowManiaScriptFactory = $maniaScriptFactory;
42
    }
43
44
    /**
45
     * Build the window frame content.
46
     *
47
     * @param ManiaLink $manialink
48
     * @param Frame|Container $mainFrame to build into
49
     * @param $name
50
     * @param float $sizeX Size of the inner frame to build the window frame around
51
     * @param float $sizeY Size of the inner frame to build the window frame around
52
     * @return void
53
     */
54
    public function build(ManiaLink $manialink, Frame $mainFrame, $name, $sizeX, $sizeY)
55
    {
56
        $titleHeight = 5.5;
57
        $closeButtonWidth = 9.5;
58
        $titlebarColor = "000e";
59
        $titleTextColor = "eff";
60
61
        // Creating sub frame to keep all the pieces together.
62
        $frame = new Frame();
63
        $frame->setPosition(-2, ($titleHeight) + 2);
64
        $mainFrame->addChild($frame);
65
66
        // Size of the actual window.
67
        $sizeX += 4;
68
        $sizeY += $titleHeight + 2;
69
70
        // Title bar & title.
71
        $titleLabel = new Label();
72
        $titleLabel->setPosition(3, -$titleHeight / 3 - 1)
73
            ->setAlign(Label::LEFT, Label::CENTER2)
74
            ->setTextId($name)
75
            ->setTextColor($titleTextColor)
76
            ->setTextSize(2)
77
            ->setTranslate(true)
78
            ->setTextFont('RajdhaniMono')
79
            ->setId("TitleText");
80
        $frame->addChild($titleLabel);
81
82
        $titleBar = new Quad();
83
        $titleBar->setSize($sizeX, 0.33)
84
            ->setPosition(0, -$titleHeight)
85
            ->setBackgroundColor('fff');
86
        $frame->addChild($titleBar);
87
88
        $titleBar = new Quad();
89
        $titleBar->setSize($sizeX / 4, 0.5)
90
            ->setPosition(0, -$titleHeight)
91
            ->setBackgroundColor('fff');
92
        $frame->addChild($titleBar);
93
94
        $titleBar = new Quad('Title');
95
        $titleBar->setSize($sizeX - $closeButtonWidth, $titleHeight)
96
            ->setBackgroundColor($titlebarColor)
97
            ->setScriptEvents(true);
98
        $frame->addChild($titleBar);
99
100
        $closeButton = new Label('Close');
101
        $closeButton->setSize($closeButtonWidth, $titleHeight)
102
            ->setPosition($sizeX - $closeButtonWidth + ($closeButtonWidth / 2), -$titleHeight / 2)
103
            ->setAlign(Label::CENTER, Label::CENTER2)
104
            ->setText("✖")
105
            ->setTextColor('fff')
106
            ->setTextSize(2)
107
            ->setTextFont('OswaldMono')
108
            ->setScriptEvents(true)
109
            ->setAreaColor($titlebarColor)
110
            ->setAreaFocusColor('f22');
111
        $this->closeButton = $closeButton;
0 ignored issues
show
Documentation Bug introduced by
It seems like $closeButton of type object<FML\Controls\Label> is incompatible with the declared type object<eXpansion\Framewo...ui\Components\uiButton> of property $closeButton.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
112
        $frame->addChild($this->closeButton);
113
114
115
        //body
116
        $body = new Quad();
117
        $body->setSize($sizeX, $sizeY - $titleHeight)
118
            ->setPosition(0, -$titleHeight)
119
            ->setBackgroundColor("222")
120
            ->setOpacity(0.8);
121
        $frame->addChild($body);
122
123
        $body = new Quad();
124
        $body->setSize($sizeX, $sizeY - $titleHeight)
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FML\Controls\Control as the method setScriptEvents() does only exist in the following sub-classes of FML\Controls\Control: FML\Controls\Audio, FML\Controls\Entry, FML\Controls\FileEntry, FML\Controls\Frame3d, FML\Controls\Label, FML\Controls\Labels\Label_Button, FML\Controls\Labels\Label_Text, FML\Controls\Quad, FML\Controls\Quads\Quad_321Go, FML\Controls\Quads\Quad_BgRaceScore2, FML\Controls\Quads\Quad_Bgs1, FML\Controls\Quads\Quad_Bgs1InRace, FML\Controls\Quads\Quad_BgsButtons, FML\Controls\Quads\Quad_BgsChallengeMedals, FML\Controls\Quads\Quad_BgsPlayerCard, FML\Controls\Quads\Quad_Copilot, FML\Controls\Quads\Quad_Emblems, FML\Controls\Quads\Quad_EnergyBar, FML\Controls\Quads\Quad_Hud3dEchelons, FML\Controls\Quads\Quad_Hud3dIcons, FML\Controls\Quads\Quad_Icons128x128_1, FML\Controls\Quads\Quad_Icons128x128_Blink, FML\Controls\Quads\Quad_Icons128x32_1, FML\Controls\Quads\Quad_Icons64x64_1, FML\Controls\Quads\Quad_Icons64x64_2, FML\Controls\Quads\Quad_ManiaPlanetLogos, FML\Controls\Quads\Quad_ManiaPlanetMainMenu, FML\Controls\Quads\Quad_ManiaplanetSystem, FML\Controls\Quads\Quad_MedalsBig, FML\Controls\Quads\Quad_TitleLogos, FML\Controls\Quads\Quad_...structionBullet_Buttons, FML\Controls\Quads\Quad_UIConstruction_Buttons, FML\Controls\Quads\Quad_UIConstruction_Buttons2, FML\Controls\Quads\Quad_UiSMSpectatorScoreBig, FML\Controls\TextEdit, FML\Controls\Video, eXpansion\Framework\Gui\Components\uiLabel. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
125
            ->setPosition(0, -$titleHeight)
126
            ->setStyles('Bgs1', 'BgDialogBlur')
127
            ->setId('WindowBg')
128
            ->setScriptEvents(true);
129
        $frame->addChild($body);
130
131
        $body = new Quad();
132
        $body->setSize($sizeX + 10, $sizeY + 10)
133
            ->setPosition(-5, 5)
134
            ->setStyles('Bgs1InRace', 'BgButtonShadow');
135
        $frame->addChild($body);
136
137
        // Add maniascript for window handling.
138
        $manialink->addChild($this->windowManiaScriptFactory->createScript(['']));
139
140
    }
141
142
    /**
143
     * @param ManialinkInterface $manialinkInterface
144
     */
145
    public function setManialinkInterface(ManialinkInterface $manialinkInterface)
146
    {
147
        $this->manialinkInterface = $manialinkInterface;
148
    }
149
150
    public function setCloseAction($action)
151
    {
152
        $this->closeButton->addDataAttribute("action", $action);
153
    }
154
155
}
156