Completed
Pull Request — master (#307)
by
unknown
03:53
created

PBWidget::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 24
ccs 0
cts 16
cp 0
crap 2
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace eXpansion\Bundle\WidgetBestRecords\Plugins\Gui;
4
5
use eXpansion\Framework\Core\Helpers\Time;
6
use eXpansion\Framework\Core\Model\Gui\FmlManialinkFactoryContext;
7
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
8
use eXpansion\Framework\Core\Model\Gui\Widget;
9
use eXpansion\Framework\Core\Plugins\Gui\FmlManialinkFactory;
10
use eXpansion\Framework\Gui\Components\Label;
11
12
class PBWidget extends FmlManialinkFactory
13
{
14
    /** @var int */
15
    private $authorTime;
16
17
    /** @var Time */
18
    private $time;
19
20
    /** @var Label */
21
    private $lblPB;
22
23
    /** @var Label */
24
    private $lblPBTime;
25
26
    /** @var Label */
27
    private $lblAuthorTime;
28
29
    /** @var int[] */
30
    private $timesByLogin = [];
31
32
    /***
33
     * MenuFactory constructor.
34
     *
35
     * @param                            $name
36
     * @param                            $sizeX
37
     * @param                            $sizeY
38
     * @param null                       $posX
39
     * @param null                       $posY
40
     * @param FmlManialinkFactoryContext $context
41
     * @param Time                       $time
42
     */
43
    public function __construct(
44
        $name,
45
        $sizeX,
46
        $sizeY,
47
        $posX,
48
        $posY,
49
        FmlManialinkFactoryContext $context,
50
        Time $time
51
52
    ) {
53
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
54
        $this->time = $time;
55
    }
56
57
    /**
58
     * @param int $authorTime
59
     */
60
    public function setAuthorTime(int $authorTime)
61
    {
62
        $this->authorTime = $authorTime;
63
    }
64
65
    /**
66
     * @param Widget|ManialinkInterface $manialink
67
     */
68
    protected function createContent(ManialinkInterface $manialink)
69
    {
70
        parent::createContent($manialink);
71
72
        $manialink->getFmlManialink()->setScript(null);
73
74
        $line = $this->uiFactory->createLayoutLine(0, 0, [], 0.5);
75
76
        $lbl = $this->createLabel("Author", "0017")->setSize(14.85, 4);
77
        $line->addChild($lbl);
78
79
        $this->lblAuthorTime = $this->createLabel("-:--:---", "0023")->setSize(14.85, 4);
80
        $this->lblAuthorTime->setAlign("center", "center2");
81
        $line->addChild($this->lblAuthorTime);
82
83
        $this->lblPB = $this->createLabel("PersonalBest", "0017")->setSize(14.85, 4);
84
        $line->addChild($this->lblPB);
85
86
        $this->lblPBTime = $this->createLabel("-:--:---", "0023")->setSize(14.85, 4);
87
        $this->lblPBTime->setAlign("center", "center2");
88
        $line->addChild($this->lblPBTime);
89
90
        $manialink->addChild($line);
91
    }
92
93
    /**
94
     * @param string $text
95
     * @param string $color
96
     * @return Label
97
     */
98
    private function createLabel($text, $color)
99
    {
100
        return $this->uiFactory->createLabel($text, Label::TYPE_NORMAL)->setTranslate(false)
101
            ->setAlign("left", "center2")->setTextSize(1)->setScriptEvents(true)
102
            ->setAreaColor($color)->setAreaFocusColor($color)->setTextColor("eff")->setTextPrefix(" ");
103
    }
104
105
    /**
106
     * @param ManialinkInterface $manialink
107
     */
108
    protected function updateContent(ManialinkInterface $manialink)
109
    {
110
        if ($this->authorTime) {
111
            $this->lblAuthorTime->setText($this->time->timeToText($this->authorTime, true));
112
        } else {
113
            $this->lblAuthorTime->setText("-:--:---");
114
        }
115
116
        $recipient = $manialink->getUserGroup()->getLogins();
117
118
        if (count($recipient) > 0) {
119
            $login = $recipient[0];
120
121
            if (isset($this->timesByLogin[$login])) {
122
                $this->lblPBTime->setText($this->time->timeToText($this->timesByLogin[$login], true));
123
            } else {
124
                $this->lblPBTime->setText("-:--:---");
125
            }
126
        } else {
127
            $this->lblPBTime->setText("-:--:---");
128
        }
129
130
    }
131
132
    /**
133
     * @param          $login
134
     * @param int|null $time
135
     */
136
    public function setPB($login, $time)
137
    {
138
        $this->timesByLogin[$login] = $time;
139
    }
140
141
    public function reset()
142
    {
143
        $this->timesByLogin = [];
144
    }
145
146
}
147