Passed
Push — feature/super-model ( a850c4...c51526 )
by axel
03:11
created

StatModel::getScorePercent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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