Passed
Push — feature/super-model ( 13c8a0...a850c4 )
by axel
01:59
created

InfoModel::getStaffRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model\General;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * InfoModel class.
10
 */
11
class InfoModel extends MainModel
12
{
13
    /**
14
     * Type of info. Either anime or manga.
15
     *
16
     * @var string
17
     */
18
	private $_type;
19
20
    /**
21
     * Id of the anime or manga.
22
     *
23
     * @var string|int
24
     */
25
	private $_id;
26
27
    /**
28
     * Default constructor.
29
     *
30
     * @param string $type
31
     * @param string|int $id
32
     * @param string $parserArea
33
     *
34
     * @return void
35
     */
36
	public function __construct($type, $id, $parserArea = '#content')
37
    {
38
    	$this->_type = $type;
39
    	$this->_id = $id;
40
        $this->_url = $this->_myAnimeListUrl.'/'.$type.'/'.$id;
41
    	$this->_parserArea = $parserArea;
42
43
        parent::errorCheck($this);
44
    }
45
46
    /**
47
     * Default call.
48
     *
49
     * @param string $method
50
     * @param array  $arguments
51
     *
52
     * @return array|string|int
53
     */
54
    public function __call($method, $arguments)
55
    {
56
        if ($this->_error)
57
            return $this->_error;
58
        return call_user_func_array([$this, $method], $arguments);
59
    }
60
61
    /**
62
     * Get anime/manga id.
63
     *
64
     * @return string
65
     */
66
    private function getId()
67
    {
68
    	return $this->_id;
69
    }
70
71
    /**
72
     * Get anime/manga cover.
73
     *
74
     * @return string
75
     */
76
    private function getCover()
77
    {
78
        $anime_cover = $this->_parser->find('img.ac', 0);
79
        return $anime_cover ? $anime_cover->src : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $anime_cover ? $anime_cover->src : '' also could return the type boolean which is incompatible with the documented return type string.
Loading history...
80
    }
81
82
    /**
83
     * Get anime/manga title.
84
     *
85
     * @return string
86
     */
87
    private function getTitle()
88
    {
89
        $anime_cover = $this->_parser->find('img.ac', 0);
90
        return $anime_cover ? $anime_cover->alt : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $anime_cover ? $anime_cover->alt : '' also could return the type boolean which is incompatible with the documented return type string.
Loading history...
91
    }
92
93
    /**
94
     * Get anime/manga alternative title.
95
     *
96
     * @return array
97
     */
98
    private function getTitle2()
99
    {
100
        $title2 = [];
101
102
        $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0);
103
104
        preg_match('/(English:<\/span>)([^<]*)/', $anime_info->innertext, $english);
105
        $title2['english'] = trim($english ? $english[2] : '');
106
107
        preg_match('/(Synonyms:<\/span>)([^<]*)/', $anime_info->innertext, $synonym);
108
        $title2['synonym'] = trim($synonym ? $synonym[2] : '');
109
110
        preg_match('/(Japanese:<\/span>)([^<]*)/', $anime_info->innertext, $japanese);
111
        $title2['japanese'] = trim($japanese ? $japanese[2] : '');
112
113
        return $title2;
114
    }
115
116
    /**
117
     * Get anime/manga synopsis.
118
     *
119
     * @return string
120
     */
121
    private function getSynopsis()
122
    {
123
        $synopsis = $this->_parser->find('span[itemprop=description]', 0);
124
        if ($synopsis) {
125
            $synopsis = $synopsis->plaintext;
126
            return trim(preg_replace('/\n[^\S\n]*/', "\n", $synopsis));
127
        } else {
128
            return null;
129
        }
130
    }
131
132
    /**
133
     * Get anime/manga score.
134
     *
135
     * @return string
136
     */
137
    private function getScore()
138
    {
139
        $score = $this->_parser->find('div[class="fl-l score"]', 0)->plaintext;
140
        $score = trim($score);
141
        return $score != 'N/A' ? $score : null;
142
    }
143
144
    /**
145
     * Get number of user who give score.
146
     *
147
     * @return string
148
     */
149
    private function getVoter()
150
    {
151
        $voter = $this->_parser->find('div[class="fl-l score"]', 0)->getAttribute('data-user');
152
        return trim(str_replace(['users', 'user', ','], '', $voter));
153
    }
154
155
    /**
156
     * Get anime/manga rank.
157
     *
158
     * @return string
159
     */
160
    private function getRank()
161
    {
162
        $rank = $this->_parser->find('span[class="numbers ranked"] strong', 0)->plaintext;
163
        $rank = $rank != 'N/A' ? $rank : '';
164
        return str_replace('#', '', $rank);
165
    }
166
167
    /**
168
     * Get anime/manga popularity.
169
     *
170
     * @return string
171
     */
172
    private function getPopularity()
173
    {
174
        $popularity = $this->_parser->find('span[class="numbers popularity"] strong', 0)->plaintext;
175
        return str_replace('#', '', $popularity);
176
    }
177
178
    /**
179
     * Get number of user who watch/read the anime/manga.
180
     *
181
     * @return string
182
     */
183
    private function getMembers()
184
    {
185
        $member = $this->_parser->find('span[class="numbers members"] strong', 0)->plaintext;
186
        return str_replace(',', '', $member);
187
    }
188
189
    /**
190
     * Get number of user who favorite the anime/manga.
191
     *
192
     * @return string
193
     */
194
    private function getFavorite()
195
    {
196
        $favorite = $this->_parser->find('div[data-id=info2]', 0)->next_sibling()->next_sibling()->next_sibling();
197
        $favorite_title = $favorite->find('span', 0)->plaintext;
198
        $favorite = $favorite->plaintext;
199
        $favorite = trim(str_replace($favorite_title, '', $favorite));
200
        $favorite = str_replace(',', '', $favorite);
201
        return preg_replace("/([\s])+/", ' ', $favorite);
202
    }
203
204
    /**
205
     * Get anime/manga detail info.
206
     *
207
     * @return array
208
     */
209
    private function getOtherInfo()
210
    {
211
        $info = [];
212
213
        $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0);
214
        $other_info = (count($anime_info->find('h2')) > 2) ? $anime_info->find('h2', 1) : $anime_info->find('h2', 0);
0 ignored issues
show
Bug introduced by
It seems like $anime_info->find('h2') can also be of type simplehtmldom_1_5\simple_html_dom_node; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

214
        $other_info = (count(/** @scrutinizer ignore-type */ $anime_info->find('h2')) > 2) ? $anime_info->find('h2', 1) : $anime_info->find('h2', 0);
Loading history...
215
        $next_info = $other_info->next_sibling();
0 ignored issues
show
Bug introduced by
The method next_sibling() does not exist on null. ( Ignorable by Annotation )

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

215
        /** @scrutinizer ignore-call */ 
216
        $next_info = $other_info->next_sibling();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
216
        while (true) {
217
            $info_type = $next_info->find('span', 0)->plaintext;
218
            $clean_info_type = strtolower(str_replace(': ', '', $info_type));
219
220
            $info_value = $next_info->plaintext;
221
            $clean_info_value = trim(str_replace($info_type, '', $info_value));
222
            $clean_info_value = preg_replace("/([\s])+/", ' ', $clean_info_value);
223
            $clean_info_value = str_replace([', add some', '?', 'Not yet aired', 'Unknown'], '', $clean_info_value);
224
225
            if ($clean_info_type == 'published' || $clean_info_type == 'aired') {
226
                $start_air = '';
227
                $end_air = '';
228
                if ($clean_info_value != 'Not available') {
229
                    $parsed_airing = explode(' to ', $clean_info_value);
230
231
                    $start_air = ($parsed_airing[0] != '?') ? date('Y-m-d', strtotime($parsed_airing[0])) : '';
232
                    if (count($parsed_airing) > 1) {
233
                        $end_air = ($parsed_airing[1] != '?') ? date('Y-m-d', strtotime($parsed_airing[1])) : '';
234
                    }
235
                }
236
237
                $clean_info_value = [];
238
                $clean_info_value['start'] = $start_air;
239
                $clean_info_value['end'] = $end_air;
240
            }
241
242
            if ($clean_info_type == 'producers'
243
                || $clean_info_type == 'licensors'
244
                || $clean_info_type == 'studios'
245
                || $clean_info_type == 'genres'
246
                || $clean_info_type == 'authors'
247
            ) {
248
                $info_temp = [];
249
                $info_temp_index = 0;
250
                if ($clean_info_value != 'None found') {
251
                    foreach ($next_info->find('a') as $each_info) {
252
                        $temp_id = explode('/', $each_info->href);
253
                        $info_temp[$info_temp_index]['id'] = $clean_info_type == 'authors' ? $temp_id[2] : $temp_id[3];
254
                        $info_temp[$info_temp_index]['name'] = $each_info->plaintext;
255
                        $info_temp_index++;
256
                    }
257
                }
258
                $clean_info_value = $info_temp;
259
            }
260
261
            $info[$clean_info_type] = $clean_info_value;
262
263
            $next_info = $next_info->next_sibling();
264
            if ($next_info->tag == 'h2' || $next_info->tag == 'br') {
265
                break;
266
            }
267
        }
268
        return $info;
269
    }
270
271
    /**
272
     * Get anime/manga relation.
273
     *
274
     * @return array
275
     */
276
    private function getRelated()
277
    {
278
        $related = [];
279
        $related_area = $this->_parser->find('.anime_detail_related_anime', 0);
280
        if ($related_area) {
281
            foreach ($related_area->find('tr') as $rel) {
282
                $rel_type = $rel->find('td', 0)->plaintext;
283
                $rel_type = trim(strtolower(str_replace(':', '', $rel_type)));
284
285
                $each_rel = [];
286
                $each_rel_index = 0;
287
                $rel_anime = $rel->find('td', 1);
288
                foreach ($rel_anime->find('a') as $r) {
289
                    $rel_anime_link = $r->href;
290
                    $separated_anime_link = explode('/', $rel_anime_link);
291
292
                    $each_rel[$each_rel_index]['id'] = $separated_anime_link[2];
293
                    $each_rel[$each_rel_index]['title'] = $r->plaintext;
294
                    $each_rel[$each_rel_index]['type'] = $separated_anime_link[1];
295
296
                    $each_rel_index++;
297
                }
298
299
                $related[$rel_type] = $each_rel;
300
            }
301
        }
302
        return $related;
303
    }
304
305
    /**
306
     * Get anime/manga character and its va.
307
     *
308
     * @return array
309
     */
310
    private function getCharacter()
311
    {
312
        $character = [];
313
        $char_index = 0;
314
        $character_area = $this->_parser->find('div[class^=detail-characters-list]', 0);
315
        if ($character_area) {
316
            $character_list = [
317
                $character_area->find('div[class*=fl-l]', 0),
318
                $character_area->find('div[class*=fl-r]', 0)
319
            ];
320
            foreach ($character_list as $character_side) {
321
                if ($character_side) {
322
                    foreach ($character_side->find('table[width=100%]') as $each_char) {
323
324
                        $char = $each_char->find('tr td', 1);
325
                        $va = $each_char->find('table td', 0);
326
327
                        $character[$char_index]['id'] = $this->getStaffId($char);
328
                        $character[$char_index]['name'] = $this->getStaffName($char);
329
                        $character[$char_index]['role'] = $this->getStaffRole($char);
330
                        $character[$char_index]['image'] = $this->getStaffImage($each_char);
331
332
                        $character[$char_index]['va_id'] = $character[$char_index]['va_name'] = '';
333
                        $character[$char_index]['va_role'] = $character[$char_index]['va_image'] = '';
334
335
                        if ($va) {
336
                            $character[$char_index]['va_id'] = $this->getStaffId($va);
337
                            $character[$char_index]['va_name'] = $this->getStaffName($va, true);
338
                            $character[$char_index]['va_role'] = $this->getStaffRole($va);
339
                            $character[$char_index]['va_image'] = $this->getStaffImage($each_char, true);
340
                        }
341
342
                        $char_index++;
343
                    }
344
                }
345
            }
346
        }
347
        return $character;
348
    }
349
350
    /**
351
     * Get anime/manga staff involved.
352
     *
353
     * @return array
354
     */
355
    private function getStaff()
356
    {
357
        $staff = [];
358
        $staff_index = 0;
359
        $staff_area = $this->_parser->find('div[class^=detail-characters-list]', 1);
360
        if ($staff_area) {
361
            $staff_list = [
362
                $staff_area->find('div[class*=fl-l]', 0),
363
                $staff_area->find('div[class*=fl-r]', 0)
364
            ];
365
            foreach ($staff_list as $staff_side) {
366
                if ($staff_side) {
367
                    foreach ($staff_side->find('table[width=100%]') as $each_staff) {
368
                        $st = $each_staff->find('tr td', 1);
369
370
                        $staff[$staff_index]['id'] = $this->getStaffId($st);
371
                        $staff[$staff_index]['name'] = $this->getStaffName($st);
372
                        $staff[$staff_index]['role'] = $this->getStaffRole($st);
373
                        $staff[$staff_index]['image'] = $this->getStaffImage($each_staff);
374
375
                        $staff_index++;
376
                    }
377
                }
378
            }
379
        }
380
        return $staff;
381
    }
382
383
    /**
384
     * Get staff id
385
     *
386
     * @param \simplehtmldom_1_5\simple_html_dom $st
387
     *
388
     * @return string
389
     */
390
    private function getStaffId($st)
391
    {
392
        $staff_id = $st->find('a', 0)->href;
393
        $staff_id = explode('/', $staff_id);
394
        return $staff_id[4];
395
    }
396
397
    /**
398
     * Get staff name
399
     *
400
     * @param \simplehtmldom_1_5\simple_html_dom $st
401
     * @param bool $va (Optional)
402
     *
403
     * @return string
404
     */
405
    private function getStaffName($st, $va = false)
406
    {
407
        if ($va) {
408
            return $st->find('a', 0)->plaintext;
409
        }
410
        return trim(preg_replace('/\s+/', ' ', $st->find('a', 0)->plaintext));
411
    }
412
413
    /**
414
     * Get staff role
415
     *
416
     * @param \simplehtmldom_1_5\simple_html_dom $st
417
     *
418
     * @return string
419
     */
420
    private function getStaffRole($st)
421
    {
422
        return trim($st->find('small', 0)->plaintext);
423
    }
424
425
    /**
426
     * Get staff image
427
     *
428
     * @param \simplehtmldom_1_5\simple_html_dom $each_staff
429
     * @param bool $va (Optional)
430
     *
431
     * @return string
432
     */
433
    private function getStaffImage($each_staff, $va = false)
434
    {
435
        if ($va) {
436
            $staff_image = $each_staff->find('table td', 1)->find('img', 0)->getAttribute('data-src');
437
        } else {
438
            $staff_image = $each_staff->find('tr td', 0)->find('img', 0)->getAttribute('data-src');
439
        }
440
        return Helper::imageUrlCleaner($staff_image);
441
    }
442
443
    /**
444
     * Get anime/manga opening and ending song.
445
     *
446
     * @return array
447
     */
448
    private function getSong()
449
    {
450
        $song = [];
451
        $song_area = $this->_parser->find('div[class*="theme-songs opnening"]', 0);
452
        if ($song_area) {
453
            foreach ($song_area->find('span.theme-song') as $each_song) {
454
                $each_song = trim(preg_replace('/#\d*:\s/', '', $each_song->plaintext));
455
                $song['opening'][] = $each_song;
456
            }
457
        }
458
459
        $song_area = $this->_parser->find('div[class*="theme-songs ending"]', 0);
460
        if ($song_area) {
461
            foreach ($song_area->find('span.theme-song') as $each_song) {
462
                $each_song = trim(preg_replace('/#\d*:\s/', '', $each_song->plaintext));
463
                $song['closing'][] = $each_song;
464
            }
465
        }
466
        return $song;
467
    }
468
469
    /**
470
     * Get anime/manga all information.
471
     *
472
     * @return array
473
     */
474
    private function getAllInfo()
475
    {
476
        $data = [
477
            'id'        => $this->getId(),
478
            'cover'     => $this->getCover(),
479
            'title'     => $this->getTitle(),
480
            'title2'    => $this->getTitle2(),
481
            'synopsis'  => $this->getSynopsis(),
482
            'score'     => $this->getScore(),
483
            'voter'     => $this->getVoter(),
484
            'rank'      => $this->getRank(),
485
            'popularity'=> $this->getPopularity(),
486
            'members'   => $this->getMembers(),
487
            'favorite'  => $this->getFavorite(),
488
        ];
489
490
        $data = array_merge($data, $this->getOtherInfo());
491
492
        $data2 = [
493
            'related'   => $this->getRelated(),
494
            'character' => $this->getCharacter(),
495
            'staff'     => $this->getStaff(),
496
            'song'      => $this->getSong()
497
        ];
498
499
        $data = array_merge($data, $data2);
500
501
    	return $data;
502
    }
503
}