Passed
Push — master ( 19405d...437c3a )
by axel
03:06 queued 43s
created

AnimeMangaReviewModel::getReviewScore()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 13
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
 * AnimeMangaReviewModel class.
10
 */
11
class AnimeMangaReviewModel extends MainModel
12
{
13
    /**
14
     * Either anime or manga.
15
     *
16
     * @var string
17
     */
18
    private $_type;
19
20
    /**
21
     * Id of the anime/manga.
22
     *
23
     * @var string|int
24
     */
25
    private $_id;
26
27
    /**
28
     * Page number.
29
     *
30
     * @var int|string
31
     */
32
    private $_page;
33
34
    /**
35
     * Default constructor.
36
     *
37
     * @param string     $type
38
     * @param string|int $id
39
     * @param string|int $page
40
     * @param string     $parserArea
41
     *
42
     * @return void
43
     */
44
    public function __construct($type, $id, $page, $parserArea = '.js-scrollfix-bottom-rel')
45
    {
46
        $this->_type = $type;
47
        $this->_id = $id;
48
        $this->_page = $page;
49
        $this->_url = $this->_myAnimeListUrl.'/'.$type.'/'.$id.'/a/reviews?p='.$page;
50
        $this->_parserArea = $parserArea;
51
52
        parent::errorCheck($this);
53
    }
54
55
    /**
56
     * Default call.
57
     *
58
     * @param string $method
59
     * @param array  $arguments
60
     *
61
     * @return array|string|int
62
     */
63
    public function __call($method, $arguments)
64
    {
65
        if ($this->_error) {
66
            return $this->_error;
67
        }
68
69
        return call_user_func_array([$this, $method], $arguments);
70
    }
71
72
    /**
73
     * Get type.
74
     *
75
     * @return string
76
     */
77
    private function getType()
78
    {
79
        return $this->_type;
80
    }
81
82
    /**
83
     * Get anime/manga id.
84
     *
85
     * @return string
86
     */
87
    private function getId()
88
    {
89
        return $this->_id;
90
    }
91
92
    /**
93
     * Get page.
94
     *
95
     * @return string
96
     */
97
    private function getPage()
98
    {
99
        return $this->_page;
100
    }
101
102
    /**
103
     * Get review user.
104
     *
105
     * @param \simplehtmldom_1_5\simple_html_dom $very_bottom_area
106
     *
107
     * @return string
108
     */
109
    private function getReviewId($very_bottom_area)
110
    {
111
        $id = $very_bottom_area->find('a', 0)->href;
112
        $id = explode('?id=', $id);
113
114
        return $id[1];
115
    }
116
117
    /**
118
     * Get review id.
119
     *
120
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
121
     *
122
     * @return string
123
     */
124
    private function getReviewUser($top_area)
125
    {
126
        $user = $top_area->find('table', 0);
127
128
        return $user->find('td', 1)->find('a', 0)->plaintext;
129
    }
130
131
    /**
132
     * Get review image.
133
     *
134
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
135
     *
136
     * @return string
137
     */
138
    private function getReviewImage($top_area)
139
    {
140
        $image = $top_area->find('table', 0);
141
        $image = $image->find('td', 0)->find('img', 0)->src;
142
143
        return Helper::imageUrlCleaner($image);
144
    }
145
146
    /**
147
     * Get review helful.
148
     *
149
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
150
     *
151
     * @return string
152
     */
153
    private function getReviewHelpful($top_area)
154
    {
155
        $helpful = $top_area->find('table', 0);
156
        $helpful = $helpful->find('td', 1)->find('strong', 0)->plaintext;
157
158
        return trim($helpful);
159
    }
160
161
    /**
162
     * Get review date.
163
     *
164
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
165
     *
166
     * @return array
167
     */
168
    private function getReviewDate($top_area)
169
    {
170
        $date = $top_area->find('div div', 0);
171
172
        return [
173
            'date' => $date->plaintext,
174
            'time' => $date->title,
175
        ];
176
    }
177
178
    /**
179
     * Get review episode seen.
180
     *
181
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
182
     *
183
     * @return string
184
     */
185
    private function getReviewEpisode($top_area)
186
    {
187
        $episode = $top_area->find('div div', 1)->plaintext;
188
        $episode = str_replace(['episodes seen', 'chapters read'], '', $episode);
189
190
        return trim($episode);
191
    }
192
193
    /**
194
     * Get review score.
195
     *
196
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
197
     *
198
     * @return array
199
     */
200
    private function getReviewScore($bottom_area)
201
    {
202
        $score = [];
203
        $score_area = $bottom_area->find('table', 0);
204
        if ($score_area) {
205
            foreach ($score_area->find('tr') as $each_score) {
206
                $score_type = strtolower($each_score->find('td', 0)->plaintext);
207
                $score_value = $each_score->find('td', 1)->plaintext;
208
                $score[$score_type] = $score_value;
209
            }
210
        }
211
212
        return $score;
213
    }
214
215
    /**
216
     * Get review text.
217
     *
218
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
219
     *
220
     * @return string
221
     */
222
    private function getReviewText($bottom_area)
223
    {
224
        $useless_area_1 = $bottom_area->find('div', 0)->plaintext;
225
        $useless_area_2 = $bottom_area->find('div[id^=revhelp_output]', 0)->plaintext;
226
        $useless_area_3 = $bottom_area->find('a[id^=reviewToggle]', 0) ? $bottom_area->find('a[id^=reviewToggle]', 0)->plaintext : null;
227
        $text = str_replace([$useless_area_1, $useless_area_2, $useless_area_3], '', $bottom_area->plaintext);
228
        $text = str_replace('&lt;', '<', $text);
229
230
        return trim(preg_replace('/\h+/', ' ', $text));
231
    }
232
233
    /**
234
     * Get anime/mange review.
235
     *
236
     * @return array
237
     */
238
    private function getAllInfo()
239
    {
240
        $data = [];
241
        $review_area = $this->_parser->find('.borderDark');
242
        foreach ($review_area as $each_review) {
243
            $tmp = [];
244
245
            $top_area = $each_review->find('.spaceit', 0);
246
            $bottom_area = $top_area->next_sibling();
247
            $very_bottom_area = $bottom_area->next_sibling();
248
249
            $tmp['id'] = $this->getReviewId($very_bottom_area);
250
            $tmp['username'] = $this->getReviewUser($top_area);
251
            $tmp['image'] = $this->getReviewImage($top_area);
252
            $tmp['helpful'] = $this->getReviewHelpful($top_area);
253
            $tmp['date'] = $this->getReviewDate($top_area);
254
            if ($this->_type == 'anime') {
255
                $tmp['episode'] = $this->getReviewEpisode($top_area);
256
            } else {
257
                $tmp['chapter'] = $this->getReviewEpisode($top_area);
258
            }
259
            $tmp['score'] = $this->getReviewScore($bottom_area);
260
            $tmp['review'] = $this->getReviewText($bottom_area);
261
262
            $data[] = $tmp;
263
        }
264
265
        return $data;
266
    }
267
}
268