Passed
Branch feature/super-model (24c950)
by axel
02:55
created

CharacterStaffModel::getCharacter()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 29
nc 4
nop 0
dl 0
loc 41
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model;
4
5
use MalScraper\Helper\Helper;
6
7
/**
8
 * CharacterStaffModel class.
9
 */
10
class CharacterStaffModel extends MainModel
11
{
12
    /**
13
     * Type of info. Either anime or manga.
14
     *
15
     * @var string
16
     */
17
	private $_type;
18
19
    /**
20
     * Id of the anime or manga.
21
     *
22
     * @var string|int
23
     */
24
	private $_id;
25
26
    /**
27
     * Default constructor.
28
     *
29
     * @param string $type
30
     * @param string|int $id
31
     * @param string $parserArea
32
     *
33
     * @return void
34
     */
35
	public function __construct($type, $id, $parserArea = '.js-scrollfix-bottom-rel')
36
    {
37
    	$this->_type = $type;
38
    	$this->_id = $id;
39
        $this->_url = $this->_myAnimeListUrl.'/'.$type.'/'.$id;
40
    	$this->_parserArea = $parserArea;
41
42
        parent::errorCheck($this);
43
    }
44
45
    /**
46
     * Default call.
47
     *
48
     * @param string $method
49
     * @param array  $arguments
50
     *
51
     * @return array|string|int
52
     */
53
    public function __call($method, $arguments)
54
    {
55
        if ($this->_error)
56
            return $this->_error;
57
        return call_user_func_array([$this, $method], $arguments);
58
    }
59
60
    /**
61
     * Get type (anime or manga).
62
     *
63
     * @return string
64
     */
65
    private function getType()
66
    {
67
        return $this->_type;
68
    }
69
70
    /**
71
     * Get anime/manga id.
72
     *
73
     * @return string
74
     */
75
    private function getId()
76
    {
77
    	return $this->_id;
78
    }
79
80
    /**
81
     * Get anime/manga character list.
82
     *
83
     * @return array
84
     */
85
    private function getCharacter()
86
    {
87
        $character = [];
88
        $character_index = 0;
89
        $char_table = $this->_parser->find('h2', 0);
90
        if ($char_table->next_sibling()->tag == 'table') {
91
            $char_table = $char_table->next_sibling();
92
            while (true) {
93
                $char_name_area = $char_table->find('td', 1);
94
95
                $character[$character_index]['image'] = self::getCharacterImage($char_table);
96
                $character[$character_index]['id'] = self::getCharacterId($char_name_area);
97
                $character[$character_index]['name'] = self::getCharacterName($char_name_area);
98
                $character[$character_index]['role'] = self::getCharacterRole($char_name_area);
99
100
                // va name + role
101
                $va = [];
102
                $va_index = 0;
103
                $char_va_area = $char_table->find('td', 2);
104
                if ($char_va_area) {
105
                    $char_va_area = $char_va_area->find('table', 0);
106
                    foreach ($char_va_area->find('tr') as $each_va) {
107
                        $va_name_area = $each_va->find('td', 0);
108
109
                        $va[$va_index]['id'] = self::getCharacterVaId($va_name_area);
110
                        $va[$va_index]['name'] = self::getCharacterVaName($va_name_area);
111
                        $va[$va_index]['role'] = self::getCharacterVaRole($va_name_area);
112
                        $va[$va_index]['image'] = self::getCharacterVaImage($each_va);
113
114
                        $va_index++;
115
                    }
116
                    $character[$character_index]['va'] = $va;
117
                }
118
119
                $char_table = $char_table->next_sibling();
120
                if ($char_table->tag == 'br' || $char_table->tag == 'a' || $char_table->tag == 'h2' || $char_table->tag == 'div')
121
                    break;
122
                $character_index++;
123
            }
124
        }
125
        return $character;
126
    }
127
128
    /**
129
     * Get anime/manga character image.
130
     *
131
     * @param \simplehtmldom_1_5\simple_html_dom $char_table
132
     *
133
     * @return string
134
     */
135
    static private function getCharacterImage($char_table)
136
    {
137
        $char_image = $char_table->find('td .picSurround img', 0)->getAttribute('data-src');
138
        return Helper::imageUrlCleaner($char_image);
139
    }
140
141
    /**
142
     * Get anime/manga character id.
143
     *
144
     * @param \simplehtmldom_1_5\simple_html_dom $char_name_area
145
     *
146
     * @return string
147
     */
148
    static private function getCharacterId($char_name_area)
149
    {
150
        $char_id = $char_name_area->find('a', 0)->href;
151
        $char_id = explode('/', $char_id);
152
        return $char_id[4];
153
    }
154
155
    /**
156
     * Get anime/manga character name.
157
     *
158
     * @param \simplehtmldom_1_5\simple_html_dom $char_name_area
159
     *
160
     * @return string
161
     */
162
    static private function getCharacterName($char_name_area)
163
    {
164
        return $char_name_area->find('a', 0)->plaintext;
165
    }
166
167
    /**
168
     * Get anime/manga character role.
169
     *
170
     * @param \simplehtmldom_1_5\simple_html_dom $char_name_area
171
     *
172
     * @return string
173
     */
174
    static private function getCharacterRole($char_name_area)
175
    {
176
        return $char_name_area->find('small', 0)->plaintext;
177
    }
178
179
    /**
180
     * Get anime/manga character va id.
181
     *
182
     * @param \simplehtmldom_1_5\simple_html_dom $va_name_area
183
     *
184
     * @return string
185
     */
186
    static private function getCharacterVaId($va_name_area)
187
    {
188
        $va_id = $va_name_area->find('a', 0)->href;
189
        $va_id = explode('/', $va_id);
190
        return $va_id[4];
191
    }
192
193
    /**
194
     * Get anime/manga character va name.
195
     *
196
     * @param \simplehtmldom_1_5\simple_html_dom $va_name_area
197
     *
198
     * @return string
199
     */
200
    static private function getCharacterVaName($va_name_area)
201
    {
202
        return $va_name_area->find('a', 0)->plaintext;
203
    }
204
205
    /**
206
     * Get anime/manga character va image.
207
     *
208
     * @param \simplehtmldom_1_5\simple_html_dom $each_va
209
     *
210
     * @return string
211
     */
212
    static private function getCharacterVaImage($each_va)
213
    {
214
        $va_image = $each_va->find('td', 1)->find('img', 0)->getAttribute('data-src');
215
        return Helper::imageUrlCleaner($va_image);
216
    }
217
218
    /**
219
     * Get anime/manga character va role.
220
     *
221
     * @param \simplehtmldom_1_5\simple_html_dom $va_name_area
222
     *
223
     * @return string
224
     */
225
    static private function getCharacterVaRole($va_name_area)
226
    {
227
        return $va_name_area->find('small', 0)->plaintext;
228
    }
229
230
    /**
231
     * Get anime/manga staff list.
232
     *
233
     * @return array
234
     */
235
    private function getStaff()
236
    {
237
        $staff = [];
238
        $staff_index = 0;
239
        $staff_table = $this->_parser->find('h2', 1);
240
        if ($staff_table) {
241
            if ($staff_table->next_sibling()->tag == 'table') {
242
                $staff_table = $staff_table->next_sibling();
243
                while (true) {
244
                    $staff_name_area = $staff_table->find('td', 1);
245
246
                    $staff[$staff_index]['image'] = self::getStaffImage($staff_table);
247
                    $staff[$staff_index]['id'] = self::getStaffId($staff_name_area);
248
                    $staff[$staff_index]['name'] = self::getStaffName($staff_name_area);
249
                    $staff[$staff_index]['role'] = self::getStaffRole($staff_name_area);
250
251
                    $staff_table = $staff_table->next_sibling();
252
                    if (!$staff_table)
253
                        break;
254
                    $staff_index++;
255
                }
256
            }
257
        }
258
        return $staff;
259
    }
260
261
    /**
262
     * Get anime/manga staff image.
263
     *
264
     * @param \simplehtmldom_1_5\simple_html_dom $staff_table
265
     *
266
     * @return string
267
     */
268
    static private function getStaffImage($staff_table)
269
    {
270
        $staff_image = $staff_table->find('td .picSurround img', 0)->getAttribute('data-src');
271
        return Helper::imageUrlCleaner($staff_image);
272
    }
273
274
    /**
275
     * Get anime/manga staff id.
276
     *
277
     * @param \simplehtmldom_1_5\simple_html_dom $staff_name_area
278
     *
279
     * @return string
280
     */
281
    static private function getStaffId($staff_name_area)
282
    {
283
        $staff_id = $staff_name_area->find('a', 0)->href;
284
        $staff_id = explode('/', $staff_id);
285
        return $staff_id[4];
286
    }
287
288
    /**
289
     * Get anime/manga staff name.
290
     *
291
     * @param \simplehtmldom_1_5\simple_html_dom $staff_name_area
292
     *
293
     * @return string
294
     */
295
    static private function getStaffName($staff_name_area)
296
    {
297
        return $staff_name_area->find('a', 0)->plaintext;
298
    }
299
300
    /**
301
     * Get anime/manga staff role.
302
     *
303
     * @param \simplehtmldom_1_5\simple_html_dom $staff_name_area
304
     *
305
     * @return string
306
     */
307
    static private function getStaffRole($staff_name_area)
308
    {
309
        return $staff_name_area->find('small', 0)->plaintext;
310
    }
311
312
    /**
313
     * Get anime/manga character + staff complete list.
314
     *
315
     * @return array
316
     */
317
    private function getAllInfo()
318
    {
319
        $data = [
320
            'character' => self::getCharacter(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\Charact...ffModel::getCharacter() is not static, but was called statically. ( Ignorable by Annotation )

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

320
            'character' => self::/** @scrutinizer ignore-call */ getCharacter(),
Loading history...
321
            'staff'     => self::getStaff(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\CharacterStaffModel::getStaff() is not static, but was called statically. ( Ignorable by Annotation )

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

321
            'staff'     => self::/** @scrutinizer ignore-call */ getStaff(),
Loading history...
322
        ];
323
324
        return $data;
325
    }
326
}