Passed
Branch develop (6d6783)
by BENARD
03:42
created

ArticleService   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 339
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 177
dl 0
loc 339
rs 10
c 0
b 0
f 0
wmc 27

10 Methods

Rating   Name   Duplication   Size   Complexity  
A postTopYear() 0 36 1
B getHtmlTopPlayer() 0 57 8
A postTopMonth() 0 35 1
B getHtmlTopGame() 0 57 7
A postTopWeek() 0 35 1
A getHtmlBottom2() 0 3 1
A getHtmlBottom1() 0 3 1
A getHtmLine() 0 3 1
A __construct() 0 8 1
A diff() 0 18 5
1
<?php
2
namespace VideoGamesRecords\DwhBundle\Service;
3
4
use DateInterval;
5
use DateTime;
6
use Exception;
7
use ProjetNormandie\ArticleBundle\Service\Writer;
8
9
class ArticleService
10
{
11
    private TopGameProvider $topGameProvider;
12
    private TopPlayerProvider $topPlayerProvider;
13
    private Writer $writer;
14
15
    public function __construct(
16
        TopGameProvider $topGameProvider,
17
        TopPlayerProvider $topPlayerProvider,
18
        Writer $writer
19
    ) {
20
        $this->topGameProvider = $topGameProvider;
21
        $this->topPlayerProvider = $topPlayerProvider;
22
        $this->writer = $writer;
23
    }
24
25
    /**
26
     * @param $day
27
     * @throws Exception
28
     */
29
    public function postTopWeek($day): void
30
    {
31
        $date1Begin = new DateTime($day);
32
        $date1End = new DateTime($day);
33
34
        $date1End->sub(new DateInterval('P1D'));
35
        $date1Begin->sub(new DateInterval('P7D'));
36
37
        $date2Begin = clone($date1Begin);
38
        $date2End = clone($date1End);
39
40
        $date2Begin->sub(new DateInterval('P7D'));
41
        $date2End->sub(new DateInterval('P7D'));
42
43
        $week = $date1Begin->format('W');
44
45
        $gamesData  = $this->topGameProvider->getTop($date1Begin, $date1End, $date2Begin, $date2End, 20);
46
        $gamesHtmlEn = $this->getHtmlTopGame($gamesData, 'en');
47
        $gamesHtmlFr = $this->getHtmlTopGame($gamesData, 'fr');
48
49
        $playersData  = $this->topPlayerProvider->getTop($date1Begin, $date1End, $date2Begin, $date2End, 20);
50
        $playersHtmlEn = $this->getHtmlTopPlayer($playersData, 'en');
51
        $playersHtmlFr = $this->getHtmlTopPlayer($playersData, 'fr');
52
53
        $textEn = $gamesHtmlEn . '<br /><br />' . $playersHtmlEn;
54
        $textFr = $gamesHtmlFr . '<br /><br />' . $playersHtmlFr;
55
56
        $this->writer->write(
0 ignored issues
show
Bug introduced by
The call to ProjetNormandie\ArticleB...Service\Writer::write() has too few arguments starting with author. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        $this->writer->/** @scrutinizer ignore-call */ 
57
                       write(

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
57
            array(
58
                'en' => 'Top of week #' . $week,
59
                'fr' => 'Top de la semaine #' . $week,
60
            ),
61
            array(
62
                'en' => $textEn,
63
                'fr' => $textFr,
64
            )
65
        );
66
    }
67
68
69
    /**
70
     * @param $day
71
     * @throws Exception
72
     */
73
    public function postTopMonth($day): void
74
    {
75
        $date1Begin = new DateTime($day);
76
        $date1End = new DateTime($day);
77
78
        $date1End->sub(new DateInterval('P1D'));
79
        $date1Begin->sub(new DateInterval('P1M'));
80
81
        $date2Begin = clone($date1Begin);
82
        $date2End = clone($date1End);
83
84
        $date2Begin->sub(new DateInterval('P1M'));
85
        $date2End->sub(new DateInterval('P1M'));
86
87
        $month = $date1Begin->format('F');
88
89
        $gamesData  = $this->topGameProvider->getTop($date1Begin, $date1End, $date2Begin, $date2End, 50);
90
        $gamesHtmlEn = $this->getHtmlTopGame($gamesData, 'en');
91
        $gamesHtmlFr = $this->getHtmlTopGame($gamesData, 'fr');
92
93
        $playersData  = $this->topPlayerProvider->getTop($date1Begin, $date1End, $date2Begin, $date2End, 50);
94
        $playersHtmlEn = $this->getHtmlTopPlayer($playersData, 'en');
95
        $playersHtmlFr = $this->getHtmlTopPlayer($playersData, 'fr');
96
97
        $textEn = $gamesHtmlEn . '<br /><br />' . $playersHtmlEn;
98
        $textFr = $gamesHtmlFr . '<br /><br />' . $playersHtmlFr;
99
100
        $this->writer->write(
0 ignored issues
show
Bug introduced by
The call to ProjetNormandie\ArticleB...Service\Writer::write() has too few arguments starting with author. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        $this->writer->/** @scrutinizer ignore-call */ 
101
                       write(

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
101
            array(
102
                'en' => 'Top of month #' . $month,
103
                'fr' => 'Top du mois #' . $month,
104
            ),
105
            array(
106
                'en' => $textEn,
107
                'fr' => $textFr,
108
            )
109
        );
110
    }
111
112
113
     /**
114
     * @param $day
115
     * @throws Exception
116
     */
117
    public function postTopYear($day): void
118
    {
119
        $date1Begin = new DateTime($day);
120
        $date1End = new DateTime($day);
121
122
        $date1End->sub(new DateInterval('P1D'));
123
        $date1Begin->sub(new DateInterval('P1Y'));
124
125
        $date2Begin = clone($date1Begin);
126
        $date2End = clone($date1End);
127
128
        $date2Begin->sub(new DateInterval('P1Y'));
129
        $date2End->sub(new DateInterval('P1Y'));
130
131
        $year = $date1Begin->format('Y');
132
133
        $gamesData  = $this->topGameProvider->getTop($date1Begin, $date1End, $date2Begin, $date2End, 100);
134
        $gamesHtmlEn = $this->getHtmlTopGame($gamesData, 'en');
135
        $gamesHtmlFr = $this->getHtmlTopGame($gamesData, 'fr');
136
137
138
        $playersData  = $this->topPlayerProvider->getTop($date1Begin, $date1End, $date2Begin, $date2End, 100);
139
        $playersHtmlEn = $this->getHtmlTopPlayer($playersData, 'en');
140
        $playersHtmlFr = $this->getHtmlTopPlayer($playersData, 'fr');
141
142
        $textEn = $gamesHtmlEn . '<br /><br />' . $playersHtmlEn;
143
        $textFr = $gamesHtmlFr . '<br /><br />' . $playersHtmlFr;
144
145
        $this->writer->write(
0 ignored issues
show
Bug introduced by
The call to ProjetNormandie\ArticleB...Service\Writer::write() has too few arguments starting with author. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        $this->writer->/** @scrutinizer ignore-call */ 
146
                       write(

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
146
            array(
147
                'en' => 'Top of year #' . $year,
148
                'fr' => 'Top de l\'année #' . $year,
149
            ),
150
            array(
151
                'en' => $textEn,
152
                'fr' => $textFr,
153
            )
154
        );
155
    }
156
157
158
    /**
159
     * @param        $data
160
     * @param string $locale
161
     * @return string
162
     */
163
    public function getHtmlTopPlayer($data, string $locale = 'en'): string
164
    {
165
        $html = '';
166
167
        if (count($data['list']) > 0) {
168
            $html .= '<div class="article-top article-top__players">';
169
            for ($i = 0; $i <= 2; $i++) {
170
                if (array_key_exists($i, $data['list'])) {
171
                    $html .= sprintf(
172
                        '<a href="%s"><img src="%s" alt="%s" class="article-top__player" /></a>',
173
                        '/' . $locale . '/' . $data['list'][$i]['player']->getUrl(),
174
                        'https://picture.video-games-records.com/user/' . $data['list'][$i]['player']->getAvatar(),
175
                        $data['list'][$i]['player']->getPseudo()
176
                    );
177
                }
178
                if ($i == 0) {
179
                    $html .= '<br />';
180
                }
181
            }
182
            
183
            $html .= '<table class="article-top__table">';
184
            $html .= '<thead>';
185
            $html .= '<tr>';
186
            $html .= '<th scope="col"><abbr title="Rank">#</abbr></th>';
187
            $html .= '<th scope="col">Player</th>';
188
            $html .= '<th scope="col">Posts submitted</th>';
189
            $html .= '<th scope="col">Position change</th>';
190
            $html .= '</tr>';
191
            $html .= '</tr>';
192
            $html .= '<tbody>';
193
194
            foreach ($data['list'] as $row) {
195
                $html .= sprintf(
196
                    $this->getHtmLine(),
197
                    $row['rank'],
198
                    '/' . $locale . '/' . $row['player']->getUrl(),
199
                    (($row['player'] != null) ? $row['player']->getPseudo() : '???'),
200
                    $row['nb'],
201
                    $this->diff($row, count($data['list']))
202
                );
203
            }
204
205
            if ($data['nbTotalPost'] > $data['nbPostFromList']) {
206
                $html .= sprintf(
207
                    $this->getHtmlBottom1(),
208
                    count($data['list']) + 1,
209
                    $data['nb'],
210
                    $data['nbTotalPost'] - $data['nbPostFromList']
211
                );
212
            }
213
            $html .= sprintf($this->getHtmlBottom2(), $data['nbTotalPost']);
214
            $html .= '</tbody>';
215
            $html .= '</table>';
216
            $html .= '</div>';
217
        }
218
219
        return $html;
220
    }
221
222
    /**
223
     * @param        $data
224
     * @param string $locale
225
     * @return string
226
     */
227
    private function getHtmlTopGame($data, string $locale = 'en'): string
228
    {
229
        $html = '';
230
231
        if (count($data['list']) > 0) {
232
            $html .= '<div class="article-top article-top__games">';
233
234
            for ($i = 0; $i <= 2; $i++) {
235
                if (array_key_exists($i, $data['list'])) {
236
                    $html .= sprintf(
237
                        '<a href="%s"><img src="%s" alt="%s" class="article-top__game" /></a>',
238
                        '/' . $locale . '/' . $data['list'][$i]['game']->getUrl(),
239
                        'https://picture.video-games-records.com/game/' . $data['list'][$i]['game']->getPicture(),
240
                        $data['list'][$i]['game']->getName()
241
                    );
242
                }
243
                if ($i == 0) {
244
                    $html .= '<br />';
245
                }
246
            }
247
248
            $html .= '<table class="article-top__table">';
249
            $html .= '<thead>';
250
            $html .= '<tr>';
251
            $html .= '<th scope="col"><abbr title="Rank">#</abbr></th>';
252
            $html .= '<th scope="col">Game</th>';
253
            $html .= '<th scope="col">Posts submitted</th>';
254
            $html .= '<th scope="col">Position change</th>';
255
            $html .= '</tr>';
256
            $html .= '</tr>';
257
            $html .= '<tbody>';
258
259
            foreach ($data['list'] as $row) {
260
                $html .= sprintf(
261
                    $this->getHtmLine(),
262
                    $row['rank'],
263
                    '/' . $locale . '/' . $row['game']->getUrl(),
264
                    $row['game']->getName(),
265
                    $row['nb'],
266
                    $this->diff($row, count($data['list']))
267
                );
268
            }
269
            if ($data['nbTotalPost'] > $data['nbPostFromList']) {
270
                $html .= sprintf(
271
                    $this->getHtmlBottom1(),
272
                    count($data['list']) + 1,
273
                    $data['nbItem'],
274
                    $data['nbTotalPost'] - $data['nbPostFromList']
275
                );
276
            }
277
            $html .= sprintf($this->getHtmlBottom2(), $data['nbTotalPost']);
278
            $html .= '</tbody>';
279
            $html .= '</table>';
280
            $html .= '</div>';
281
        }
282
283
        return $html;
284
    }
285
286
    /**
287
     * @param $row
288
     * @param $nbGame
289
     * @return string
290
     */
291
    private function diff($row, $nbGame): string
292
    {
293
        if ($row['oldRank'] != null) {
294
            if ($row['rank'] < $row['oldRank']) {
295
                if ($row['oldRank'] > $nbGame) {
296
                    $col = '<span class="article-top--new"><abbr title="New">N</abbr></span>';
297
                } else {
298
                    $col = sprintf('<span class="article-top--up">+%d <span class="screen-reader-text">position</span></span>', $row['oldRank'] - $row['rank']);
299
                }
300
            } elseif ($row['rank'] > $row['oldRank']) {
301
                $col = sprintf('<span class="article-top--down">-%d <span class="screen-reader-text">position</span></span>', $row['rank'] - $row['oldRank']);
302
            } else {
303
                $col = '<span class="article-top--equal"><abbr title="Same position">=</abbr></span>';
304
            }
305
        } else {
306
            $col = '<span class="article-top--new"><abbr title="New">N</abbr></span>';
307
        }
308
        return $col;
309
    }
310
311
312
    /**
313
     * @return string
314
     */
315
    private function getHtmLine(): string
316
    {
317
        return '
318
            <tr>
319
                <td>%d</td>
320
                <td>
321
		            <a href="%s">%s</a>
322
	            </td>
323
	            <td>%s posts</td>
324
	            <td>
325
	                %s
326
	            </td>
327
	        </tr>';
328
    }
329
330
    /**
331
     * @return string
332
     */
333
    private function getHtmlBottom1(): string
334
    {
335
        return '
336
            <tr>
337
                <td colspan="2" class="article-top__bottom-left">%d - %d</td>
338
                <td colspan="2" class="article-top__bottom-right">%d posts</td>
339
            </tr>';
340
    }
341
342
    /**
343
     * @return string
344
     */
345
    private function getHtmlBottom2(): string
346
    {
347
        return '
348
            <tfooter>
349
                <tr>
350
                    <th scope="row" colspan="2" class="article-top__bottom-left">Total</th>
351
                    <td colspan="2" class="article-top__bottom-right">%d posts</td>
352
                </tr>
353
            </tfooter>';
354
    }
355
}