GetHtmlTopTrait   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 103
dl 0
loc 198
rs 10
c 3
b 0
f 0
wmc 23

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHtmlBottom1() 0 3 1
A getHtmlBottom2() 0 3 1
A getHtmLine() 0 3 1
B getHtmlTopPlayer() 0 57 8
B getHtmlTopGame() 0 57 7
A diff() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\DwhBundle\Traits\Top;
6
7
trait GetHtmlTopTrait
8
{
9
    /**
10
     * @param        $data
11
     * @param string $locale
12
     * @return string
13
     */
14
    private function getHtmlTopGame($data, string $locale = 'en'): string
15
    {
16
        $html = '';
17
18
        if (count($data['list']) > 0) {
19
            $html .= '<div class="article-top article-top__games">';
20
21
            for ($i = 0; $i <= 2; $i++) {
22
                if (array_key_exists($i, $data['list'])) {
23
                    $html .= sprintf(
24
                        '<a href="%s"><img src="https://backoffice.video-games-records.com/game/%d/picture" alt="%s" class="article-top__game" /></a>',
25
                        '/' . $locale . '/' . $data['list'][$i]['game']->getUrl(),
26
                        $data['list'][$i]['game']->getId(),
27
                        $data['list'][$i]['game']->getName()
28
                    );
29
                }
30
                if ($i == 0) {
31
                    $html .= '<br />';
32
                }
33
            }
34
35
            $html .= '<table class="article-top__table">';
36
            $html .= '<thead>';
37
            $html .= '<tr>';
38
            $html .= '<th scope="col"><abbr title="Rank">#</abbr></th>';
39
            $html .= '<th scope="col">Game</th>';
40
            $html .= '<th scope="col">Posts submitted</th>';
41
            $html .= '<th scope="col">Position change</th>';
42
            $html .= '</tr>';
43
            $html .= '</tr>';
44
            $html .= '<tbody>';
45
46
            foreach ($data['list'] as $row) {
47
                $html .= sprintf(
48
                    $this->getHtmLine(),
49
                    $row['rank'],
50
                    '/' . $locale . '/' . $row['game']->getUrl(),
51
                    $row['game']->getName(),
52
                    $row['nb'],
53
                    $this->diff($row, count($data['list']))
54
                );
55
            }
56
            if ($data['nbTotalPost'] > $data['nbPostFromList']) {
57
                $html .= sprintf(
58
                    $this->getHtmlBottom1(),
59
                    count($data['list']) + 1,
60
                    $data['nbItem'],
61
                    $data['nbTotalPost'] - $data['nbPostFromList']
62
                );
63
            }
64
            $html .= sprintf($this->getHtmlBottom2(), $data['nbTotalPost']);
65
            $html .= '</tbody>';
66
            $html .= '</table>';
67
            $html .= '</div>';
68
        }
69
70
        return $html;
71
    }
72
73
    /**
74
     * @param        $data
75
     * @param string $locale
76
     * @return string
77
     */
78
    private function getHtmlTopPlayer($data, string $locale = 'en'): string
79
    {
80
        $html = '';
81
82
        if (count($data['list']) > 0) {
83
            $html .= '<div class="article-top article-top__players">';
84
            for ($i = 0; $i <= 2; $i++) {
85
                if (array_key_exists($i, $data['list'])) {
86
                    $html .= sprintf(
87
                        '<a href="%s"><img src="https://backoffice.video-games-records.com/users/%d/avatar" alt="%s" class="article-top__player" /></a>',
88
                        '/' . $locale . '/' . $data['list'][$i]['player']->getUrl(),
89
                        $data['list'][$i]['player']->getId(),
90
                        $data['list'][$i]['player']->getPseudo()
91
                    );
92
                }
93
                if ($i == 0) {
94
                    $html .= '<br />';
95
                }
96
            }
97
98
            $html .= '<table class="article-top__table">';
99
            $html .= '<thead>';
100
            $html .= '<tr>';
101
            $html .= '<th scope="col"><abbr title="Rank">#</abbr></th>';
102
            $html .= '<th scope="col">Player</th>';
103
            $html .= '<th scope="col">Posts submitted</th>';
104
            $html .= '<th scope="col">Position change</th>';
105
            $html .= '</tr>';
106
            $html .= '</tr>';
107
            $html .= '<tbody>';
108
109
            foreach ($data['list'] as $row) {
110
                $html .= sprintf(
111
                    $this->getHtmLine(),
112
                    $row['rank'],
113
                    '/' . $locale . '/' . $row['player']->getUrl(),
114
                    (($row['player'] != null) ? $row['player']->getPseudo() : '???'),
115
                    $row['nb'],
116
                    $this->diff($row, count($data['list']))
117
                );
118
            }
119
120
            if ($data['nbTotalPost'] > $data['nbPostFromList']) {
121
                $html .= sprintf(
122
                    $this->getHtmlBottom1(),
123
                    count($data['list']) + 1,
124
                    $data['nb'],
125
                    $data['nbTotalPost'] - $data['nbPostFromList']
126
                );
127
            }
128
            $html .= sprintf($this->getHtmlBottom2(), $data['nbTotalPost']);
129
            $html .= '</tbody>';
130
            $html .= '</table>';
131
            $html .= '</div>';
132
        }
133
134
        return $html;
135
    }
136
137
    /**
138
     * @param $row
139
     * @param $nbGame
140
     * @return string
141
     */
142
    private function diff($row, $nbGame): string
143
    {
144
        if ($row['oldRank'] != null) {
145
            if ($row['rank'] < $row['oldRank']) {
146
                if ($row['oldRank'] > $nbGame) {
147
                    $col = '<span class="article-top--new"><abbr title="New">N</abbr></span>';
148
                } else {
149
                    $col = sprintf(
150
                        '<span class="article-top--up">+%d <span class="screen-reader-text">position</span></span>',
151
                        $row['oldRank'] - $row['rank']
152
                    );
153
                }
154
            } elseif ($row['rank'] > $row['oldRank']) {
155
                $col = sprintf(
156
                    '<span class="article-top--down">-%d <span class="screen-reader-text">position</span></span>',
157
                    $row['rank'] - $row['oldRank']
158
                );
159
            } else {
160
                $col = '<span class="article-top--equal"><abbr title="Same position">=</abbr></span>';
161
            }
162
        } else {
163
            $col = '<span class="article-top--new"><abbr title="New">N</abbr></span>';
164
        }
165
        return $col;
166
    }
167
168
169
    /**
170
     * @return string
171
     */
172
    private function getHtmLine(): string
173
    {
174
        return '
175
            <tr>
176
                <td>%d</td>
177
                <td>
178
		            <a href="%s">%s</a>
179
	            </td>
180
	            <td>%s posts</td>
181
	            <td>
182
	                %s
183
	            </td>
184
	        </tr>';
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    private function getHtmlBottom1(): string
191
    {
192
        return '
193
            <tr>
194
                <td colspan="2" class="article-top__bottom-left">%d - %d</td>
195
                <td colspan="2" class="article-top__bottom-right">%d posts</td>
196
            </tr>';
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    private function getHtmlBottom2(): string
203
    {
204
        return '
205
            <tfooter>
206
                <tr>
207
                    <th scope="row" colspan="2" class="article-top__bottom-left">Total</th>
208
                    <td colspan="2" class="article-top__bottom-right">%d posts</td>
209
                </tr>
210
            </tfooter>';
211
    }
212
}
213