StatModel::getUsername()   A
last analyzed

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\Additional;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * StatModel class.
10
 */
11
class StatModel 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.'/a/stats';
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 stat summary.
85
     *
86
     * @return array
87
     */
88
    private function getSummary()
89
    {
90
        $summary = [];
91
        $summary_area = $this->_parser->find('h2', 0)->next_sibling();
92
        if ($summary_area->tag == 'div') {
93
            while (true) {
94
95
                // status
96
                $temp_type = $summary_area->find('span', 0)->plaintext;
97
                $summary_type = trim(str_replace(':', '', strtolower($temp_type)));
98
99
                // count
100
                $status_area = $summary_area->plaintext;
101
                $count = str_replace($temp_type, '', $status_area);
102
                $summary[$summary_type] = trim(str_replace(',', '', $count));
103
104
                $summary_area = $summary_area->next_sibling();
105
                if ($summary_area->tag != 'div') {
106
                    break;
107
                }
108
            }
109
        }
110
111
        return $summary;
112
    }
113
114
    /**
115
     * Get anime/manga stat score.
116
     *
117
     * @return array
118
     */
119
    private function getScore()
120
    {
121
        $score = [];
122
        $score_area = $this->_parser->find('h2', 1)->next_sibling();
123
        if ($score_area->tag == 'table') {
124
            foreach ($score_area->find('tr') as $each_score) {
125
                $temp_score = [];
126
127
                $temp_score['type'] = $this->getScoreType($each_score);
128
                $temp_score['vote'] = $this->getScoreVote($each_score);
129
                $temp_score['percent'] = $this->getScorePercent($each_score);
130
131
                $score[] = $temp_score;
132
            }
133
        }
134
135
        return $score;
136
    }
137
138
    /**
139
     * Get score type.
140
     *
141
     * @param \simplehtmldom_1_5\simple_html_dom $each_score
142
     *
143
     * @return string
144
     */
145
    private function getScoreType($each_score)
146
    {
147
        return $each_score->find('td', 0)->plaintext;
148
    }
149
150
    /**
151
     * Get score vote.
152
     *
153
     * @param \simplehtmldom_1_5\simple_html_dom $each_score
154
     *
155
     * @return string
156
     */
157
    private function getScoreVote($each_score)
158
    {
159
        $vote = $each_score->find('td', 1)->find('span small', 0)->plaintext;
160
        $vote = substr($vote, 1, strlen($vote) - 2);
161
162
        return str_replace(' votes', '', $vote);
163
    }
164
165
    /**
166
     * Get score percent.
167
     *
168
     * @param \simplehtmldom_1_5\simple_html_dom $each_score
169
     *
170
     * @return string
171
     */
172
    private function getScorePercent($each_score)
173
    {
174
        $temp_vote = $each_score->find('td', 1)->find('span small', 0)->plaintext;
175
        $percent = $each_score->find('td', 1)->find('span', 0)->plaintext;
176
        $percent = str_replace([$temp_vote, '%', "\xc2\xa0"], '', $percent);
177
178
        return trim($percent);
179
    }
180
181
    /**
182
     * Get anime/manga stat user.
183
     *
184
     * @return array
185
     */
186
    private function getUser()
187
    {
188
        $user = [];
189
        $user_area = $this->_parser->find('.table-recently-updated', 0);
190
        if ($user_area) {
191
            foreach ($user_area->find('tr') as $each_user) {
192
                if (!$each_user->find('td', 0)->find('div', 0)) {
193
                    continue;
194
                }
195
196
                $temp_user = [];
197
198
                $username_area = $each_user->find('td', 0);
199
200
                $temp_user['image'] = $this->getUserImage($username_area);
201
                $temp_user['username'] = $this->getUsername($username_area);
202
                $temp_user['score'] = $this->getUserScore($each_user);
203
                $temp_user['status'] = $this->getUserStatus($each_user);
204
205
                if ($this->_type == 'anime') {
206
                    $temp_user['episode'] = $this->getUserProgress($each_user, 3);
207
                    $temp_user['date'] = $this->getUserDate($each_user, 4);
208
                } else {
209
                    $temp_user['volume'] = $this->getUserProgress($each_user, 3);
210
                    $temp_user['chapter'] = $this->getUserProgress($each_user, 4);
211
                    $temp_user['date'] = $this->getUserDate($each_user, 5);
212
                }
213
214
                $user[] = $temp_user;
215
            }
216
        }
217
218
        return $user;
219
    }
220
221
    /**
222
     * Get username.
223
     *
224
     * @param \simplehtmldom_1_5\simple_html_dom $username_area
225
     *
226
     * @return string
227
     */
228
    private function getUsername($username_area)
229
    {
230
        return $username_area->find('a', 1)->plaintext;
231
    }
232
233
    /**
234
     * Get user image.
235
     *
236
     * @param \simplehtmldom_1_5\simple_html_dom $username_area
237
     *
238
     * @return string
239
     */
240
    private function getUserImage($username_area)
241
    {
242
        $user_image = $username_area->find('a', 0)->style;
243
        $user_image = substr($user_image, 21, strlen($user_image) - 22);
244
245
        return Helper::imageUrlCleaner($user_image);
246
    }
247
248
    /**
249
     * Get user image.
250
     *
251
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
252
     *
253
     * @return string
254
     */
255
    private function getUserScore($each_user)
256
    {
257
        return $each_user->find('td', 1)->plaintext;
258
    }
259
260
    /**
261
     * Get user status.
262
     *
263
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
264
     *
265
     * @return string
266
     */
267
    private function getUserStatus($each_user)
268
    {
269
        return strtolower($each_user->find('td', 2)->plaintext);
270
    }
271
272
    /**
273
     * Get user progress.
274
     *
275
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
276
     * @param int                                $count
277
     *
278
     * @return string
279
     */
280
    private function getUserProgress($each_user, $count = 3)
281
    {
282
        $progress = $each_user->find('td', $count)->plaintext;
283
284
        return str_replace(' ', '', $progress);
285
    }
286
287
    /**
288
     * Get user date.
289
     *
290
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
291
     * @param int                                $count
292
     *
293
     * @return string
294
     */
295
    private function getUserDate($each_user, $count = 4)
296
    {
297
        return $each_user->find('td', $count)->plaintext;
298
    }
299
300
    /**
301
     * Get anime/manga stat info.
302
     *
303
     * @return array
304
     */
305
    private function getAllInfo()
306
    {
307
        $data = [
308
            'summary' => $this->getSummary(),
309
            'score'   => $this->getScore(),
310
            'user'    => $this->getUser(),
311
        ];
312
313
        return $data;
314
    }
315
}
316