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

WindowFrameFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 0
loc 111
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B build() 0 86 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui\Factory;
4
5
use eXpansion\Framework\Core\Model\Gui\ManiaScriptFactory;
6
use FML\Controls\Control;
7
use FML\Controls\Frame;
8
use FML\Controls\Label;
9
use FML\Controls\Quad;
10
use FML\ManiaLink;
11
use FML\Types\Container;
12
13
/**
14
 * Class WindowFrameFactory
15
 *
16
 * @author    de Cramer Oliver<[email protected]>
17
 * @copyright 2017 Smile
18
 * @package eXpansion\Framework\Core\Model\Gui\Factory
19
 */
20
class WindowFrameFactory
21
{
22
    /** @var ManiaScriptFactory */
23
    protected $windowManiaScriptFactory;
24
25
    /**
26
     * WindowFrameFactory constructor.
27
     *
28
     * @param ManiaScriptFactory $windowManiaScriptFactory
29
     */
30
    public function __construct(ManiaScriptFactory $windowManiaScriptFactory)
31
    {
32
        $this->windowManiaScriptFactory = $windowManiaScriptFactory;
33
    }
34
35
    /**
36
     * Build the window frame content.
37
     *
38
     * @param Container $mainFrame to build into
39
     * @param float $sizeX Size of the inner frame to build the window frame around
40
     * @param float $sizeY Size of the inner frame to build the window frame around
41
     *
42
     * @return Control
43
     */
44
    public function build(ManiaLink $manialink, Frame $mainFrame, $name, $sizeX, $sizeY)
45
    {
46
        $titleHeight = 5.5;
47
        $closeButtonWidth = 9.5;
48
        $titlebarColor = "000e";
49
        $titleTextColor = "eff";
50
51
        // Creating sub frame to keep all the pieces together.
52
        $frame = new Frame();
53
        $frame->setPosition(-2, (-1 * $titleHeight) - 2);
54
        $mainFrame->addChild($frame);
55
56
        // Size of the actual window.
57
        $sizeX += 4;
58
        $sizeY += $titleHeight + 2;
59
60
        // Title bar & title.
61
        $titleLabel = new Label();
62
        $titleLabel->setPosition(3, -$titleHeight / 3 - 1)
63
            ->setAlign(Label::LEFT, Label::CENTER2)
64
            ->setTextId($name)
65
            ->setTextColor($titleTextColor)
66
            ->setTextSize(2)
67
            ->setTranslate(true)
68
            ->setTextFont('RajdhaniMono')
69
            ->setId("TitleText");
70
        $frame->addChild($titleLabel);
71
72
        $titleBar = new Quad();
73
        $titleBar->setSize($sizeX, 0.33)
74
            ->setPosition(0, -$titleHeight)
75
            ->setBackgroundColor('fff');
76
        $frame->addChild($titleBar);
77
78
        $titleBar = new Quad();
79
        $titleBar->setSize($sizeX / 4, 0.5)
80
            ->setPosition(0, -$titleHeight)
81
            ->setBackgroundColor('fff');
82
        $frame->addChild($titleBar);
83
84
        $titleBar = new Quad('Title');
85
        $titleBar->setSize($sizeX - $closeButtonWidth, $titleHeight)
86
            ->setBackgroundColor($titlebarColor)
87
            ->setScriptEvents(true);
88
        $frame->addChild($titleBar);
89
90
        $closeButton = new Label('Close');
91
        $closeButton->setSize($closeButtonWidth, $titleHeight)
92
            ->setPosition($sizeX - $closeButtonWidth + ($closeButtonWidth / 2), -$titleHeight / 2)
93
            ->setAlign(Label::CENTER, Label::CENTER2)
94
            ->setText("✖")
95
            ->setTextColor('fff')
96
            ->setTextSize(2)
97
            ->setTextFont('OswaldMono')
98
            ->setScriptEvents(true)
99
            ->setAreaColor($titlebarColor)
100
            ->setAreaFocusColor('f22');
101
        $frame->addChild($closeButton);
102
103
        //body
104
        $body = new Quad();
105
        $body->setSize($sizeX, $sizeY - $titleHeight)
106
            ->setPosition(0, -$titleHeight)
107
            ->setBackgroundColor("222")
108
            ->setOpacity(0.8);
109
        $frame->addChild($body);
110
111
        $body = new Quad();
112
        $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...
113
            ->setPosition(0, -$titleHeight)
114
            ->setStyles('Bgs1', 'BgDialogBlur')
115
            ->setId('WindowBg')
116
            ->setScriptEvents(true);
117
        $frame->addChild($body);
118
119
        $body = new Quad();
120
        $body->setSize($sizeX + 10, $sizeY + 10)
121
            ->setPosition(-5, 5)
122
            ->setStyles('Bgs1InRace', 'BgButtonShadow');
123
        $frame->addChild($body);
124
125
        // Add maniascript for window handling.
126
        $manialink->addChild($this->windowManiaScriptFactory->createScript(['']));
127
128
        return $closeButton;
129
    }
130
}