Passed
Push — feature/super-model ( 9eaa75...66e03d )
by axel
01:46
created

CharacterStaffModel   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 286
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 286
rs 10
c 0
b 0
f 0
wmc 29

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getId() 0 3 1
A __call() 0 7 2
A getType() 0 3 1
A getCharacterVaId() 0 6 1
A getVa() 0 16 2
B getCharacter() 0 30 8
A getStaff() 0 26 5
A getCharacterVaRole() 0 3 1
A getCharacterName() 0 3 1
A getCharacterVaName() 0 3 1
A getCharacterRole() 0 3 1
A getCharacterId() 0 6 1
A getCharacterImage() 0 5 1
A getCharacterVaImage() 0 5 1
A getAllInfo() 0 8 1
1
<?php
2
3
namespace MalScraper\Model\Additional;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * CharacterStaffModel class.
10
 */
11
class CharacterStaffModel 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 = '.js-scrollfix-bottom-rel')
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
        }
59
60
        return call_user_func_array([$this, $method], $arguments);
61
    }
62
63
    /**
64
     * Get type (anime or manga).
65
     *
66
     * @return string
67
     */
68
    private function getType()
69
    {
70
        return $this->_type;
71
    }
72
73
    /**
74
     * Get anime/manga id.
75
     *
76
     * @return string
77
     */
78
    private function getId()
79
    {
80
        return $this->_id;
81
    }
82
83
    /**
84
     * Get anime/manga character list.
85
     *
86
     * @return array
87
     */
88
    private function getCharacter()
89
    {
90
        $character = [];
91
        $character_index = 0;
92
        $char_table = $this->_parser->find('h2', 0);
93
        if ($char_table->next_sibling()->tag == 'table') {
94
            $char_table = $char_table->next_sibling();
95
            while (true) {
96
                $char_name_area = $char_table->find('td', 1);
97
98
                $character[$character_index]['image'] = $this->getCharacterImage($char_table);
99
                $character[$character_index]['id'] = $this->getCharacterId($char_name_area);
100
                $character[$character_index]['name'] = $this->getCharacterName($char_name_area);
101
                $character[$character_index]['role'] = $this->getCharacterRole($char_name_area);
102
103
                // va name + role
104
                $char_va_area = $char_table->find('td', 2);
105
                if ($char_va_area) {
106
                    $character[$character_index]['va'] = $this->getVa($char_va_area);
107
                }
108
109
                $char_table = $char_table->next_sibling();
110
                if ($char_table->tag == 'br' || $char_table->tag == 'a' || $char_table->tag == 'h2' || $char_table->tag == 'div') {
111
                    break;
112
                }
113
                $character_index++;
114
            }
115
        }
116
117
        return $character;
118
    }
119
120
    /**
121
     * Get anime/manga character image.
122
     *
123
     * @param \simplehtmldom_1_5\simple_html_dom $char_va_area
124
     *
125
     * @return string
126
     */
127
    private function getVa($char_va_area)
128
    {
129
        $va = [];
130
        $va_index = 0;
131
        $char_va_area = $char_va_area->find('table', 0);
132
        foreach ($char_va_area->find('tr') as $each_va) {
133
            $va_name_area = $each_va->find('td', 0);
134
135
            $va[$va_index]['id'] = $this->getCharacterVaId($va_name_area);
136
            $va[$va_index]['name'] = $this->getCharacterVaName($va_name_area);
137
            $va[$va_index]['role'] = $this->getCharacterVaRole($va_name_area);
138
            $va[$va_index]['image'] = $this->getCharacterVaImage($each_va);
139
140
            $va_index++;
141
        }
142
        return $va;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $va returns the type array which is incompatible with the documented return type string.
Loading history...
143
    }
144
145
    /**
146
     * Get anime/manga va character.
147
     *
148
     * @param \simplehtmldom_1_5\simple_html_dom $char_table
149
     *
150
     * @return string
151
     */
152
    private function getCharacterImage($char_table)
153
    {
154
        $char_image = $char_table->find('td .picSurround img', 0)->getAttribute('data-src');
155
156
        return Helper::imageUrlCleaner($char_image);
157
    }
158
159
    /**
160
     * Get anime/manga character id.
161
     *
162
     * @param \simplehtmldom_1_5\simple_html_dom $char_name_area
163
     *
164
     * @return string
165
     */
166
    private function getCharacterId($char_name_area)
167
    {
168
        $char_id = $char_name_area->find('a', 0)->href;
169
        $char_id = explode('/', $char_id);
170
171
        return $char_id[4];
172
    }
173
174
    /**
175
     * Get anime/manga character name.
176
     *
177
     * @param \simplehtmldom_1_5\simple_html_dom $char_name_area
178
     *
179
     * @return string
180
     */
181
    private function getCharacterName($char_name_area)
182
    {
183
        return $char_name_area->find('a', 0)->plaintext;
184
    }
185
186
    /**
187
     * Get anime/manga character role.
188
     *
189
     * @param \simplehtmldom_1_5\simple_html_dom $char_name_area
190
     *
191
     * @return string
192
     */
193
    private function getCharacterRole($char_name_area)
194
    {
195
        return $char_name_area->find('small', 0)->plaintext;
196
    }
197
198
    /**
199
     * Get anime/manga character va id.
200
     *
201
     * @param \simplehtmldom_1_5\simple_html_dom $va_name_area
202
     *
203
     * @return string
204
     */
205
    private function getCharacterVaId($va_name_area)
206
    {
207
        $va_id = $va_name_area->find('a', 0)->href;
208
        $va_id = explode('/', $va_id);
209
210
        return $va_id[4];
211
    }
212
213
    /**
214
     * Get anime/manga character va name.
215
     *
216
     * @param \simplehtmldom_1_5\simple_html_dom $va_name_area
217
     *
218
     * @return string
219
     */
220
    private function getCharacterVaName($va_name_area)
221
    {
222
        return $va_name_area->find('a', 0)->plaintext;
223
    }
224
225
    /**
226
     * Get anime/manga character va image.
227
     *
228
     * @param \simplehtmldom_1_5\simple_html_dom $each_va
229
     *
230
     * @return string
231
     */
232
    private function getCharacterVaImage($each_va)
233
    {
234
        $va_image = $each_va->find('td', 1)->find('img', 0)->getAttribute('data-src');
235
236
        return Helper::imageUrlCleaner($va_image);
237
    }
238
239
    /**
240
     * Get anime/manga character va role.
241
     *
242
     * @param \simplehtmldom_1_5\simple_html_dom $va_name_area
243
     *
244
     * @return string
245
     */
246
    private function getCharacterVaRole($va_name_area)
247
    {
248
        return $va_name_area->find('small', 0)->plaintext;
249
    }
250
251
    /**
252
     * Get anime/manga staff list.
253
     *
254
     * @return array
255
     */
256
    private function getStaff()
257
    {
258
        $staff = [];
259
        $staff_index = 0;
260
        $staff_table = $this->_parser->find('h2', 1);
261
        if ($staff_table) {
262
            if ($staff_table->next_sibling()->tag == 'table') {
263
                $staff_table = $staff_table->next_sibling();
264
                while (true) {
265
                    $staff_name_area = $staff_table->find('td', 1);
266
267
                    $staff[$staff_index]['image'] = $this->getCharacterImage($staff_table);
268
                    $staff[$staff_index]['id'] = $this->getCharacterId($staff_name_area);
269
                    $staff[$staff_index]['name'] = $this->getCharacterName($staff_name_area);
270
                    $staff[$staff_index]['role'] = $this->getCharacterRole($staff_name_area);
271
272
                    $staff_table = $staff_table->next_sibling();
273
                    if (!$staff_table) {
274
                        break;
275
                    }
276
                    $staff_index++;
277
                }
278
            }
279
        }
280
281
        return $staff;
282
    }
283
284
    /**
285
     * Get anime/manga character + staff complete list.
286
     *
287
     * @return array
288
     */
289
    private function getAllInfo()
290
    {
291
        $data = [
292
            'character' => $this->getCharacter(),
293
            'staff'     => $this->getStaff(),
294
        ];
295
296
        return $data;
297
    }
298
}
299