Passed
Push — develop ( ebac2f...683435 )
by BENARD
03:41
created

GetHtmlTopTrait::getHtmlTopGame()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 57
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 57
rs 8.3306
cc 7
nc 3
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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