Passed
Push — develop ( 5eb8d1...52e7a8 )
by axel
02:21
created

ReviewModel   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 295
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 295
rs 10
c 0
b 0
f 0
wmc 22

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getReviewScore() 0 13 3
A __call() 0 7 2
A getReviewEpisode() 0 6 1
A getId() 0 3 1
A getReviewSource() 0 9 1
A getSourceImage() 0 5 1
A getSourceType() 0 6 1
A __construct() 0 7 1
A getReviewImage() 0 6 1
A getReviewText() 0 11 2
A getSourceId() 0 6 1
A getReviewUser() 0 5 1
A getAllInfo() 0 24 2
A getReviewDate() 0 7 1
A getType() 0 3 1
A getSourceTitle() 0 5 1
A getReviewHelpful() 0 6 1
1
<?php
2
3
namespace MalScraper\Model\General;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * ReviewModel class.
10
 */
11
class ReviewModel extends MainModel
12
{
13
    /**
14
     * Id of the review.
15
     *
16
     * @var string|int
17
     */
18
    private $_id;
19
20
    /**
21
     * Either anime, manga.
22
     *
23
     * @var string
24
     */
25
    private $_type;
26
27
    /**
28
     * Default constructor.
29
     *
30
     * @param string|int $id
31
     * @param string     $parserArea
32
     *
33
     * @return void
34
     */
35
    public function __construct($id, $parserArea = '#content')
36
    {
37
        $this->_id = $id;
38
        $this->_url = $this->_myAnimeListUrl.'/reviews.php?id='.$id;
39
        $this->_parserArea = $parserArea;
40
41
        parent::errorCheck($this);
42
    }
43
44
    /**
45
     * Default call.
46
     *
47
     * @param string $method
48
     * @param array  $arguments
49
     *
50
     * @return array|string|int
51
     */
52
    public function __call($method, $arguments)
53
    {
54
        if ($this->_error) {
55
            return $this->_error;
56
        }
57
58
        return call_user_func_array([$this, $method], $arguments);
59
    }
60
61
    /**
62
     * Get type.
63
     *
64
     * @return string
65
     */
66
    private function getType()
67
    {
68
        return $this->_type;
69
    }
70
71
    /**
72
     * Get id.
73
     *
74
     * @return string
75
     */
76
    private function getId()
77
    {
78
        return $this->_id;
79
    }
80
81
    /**
82
     * Get review source.
83
     *
84
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
85
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
86
     *
87
     * @return array
88
     */
89
    private function getReviewSource($top_area, $bottom_area)
90
    {
91
        $source_area = $top_area->find('.mb8', 1);
92
93
        return [
94
            'type' => $this->getSourceType($source_area),
95
            'id'   => $this->getSourceId($source_area),
96
            'title'=> $this->getSourceTitle($source_area),
97
            'image'=> $this->getSourceImage($bottom_area),
98
        ];
99
    }
100
101
    /**
102
     * Get source type.
103
     *
104
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
105
     *
106
     * @return string
107
     */
108
    private function getSourceType($source_area)
109
    {
110
        $type = $source_area->find('small', 0)->plaintext;
111
        $type = str_replace(['(', ')'], '', $type);
112
        $this->_type = strtolower($type);
113
        return strtolower($type);
114
    }
115
116
    /**
117
     * Get source id.
118
     *
119
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
120
     *
121
     * @return string
122
     */
123
    private function getSourceId($source_area)
124
    {
125
        $id = $source_area->find('strong a', 0)->href;
126
        $id = explode('/', $id);
127
128
        return $id[4];
129
    }
130
131
    /**
132
     * Get source title.
133
     *
134
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
135
     *
136
     * @return string
137
     */
138
    private function getSourceTitle($source_area)
139
    {
140
        $title = $source_area->find('strong', 0)->plaintext;
141
142
        return trim($title);
143
    }
144
145
    /**
146
     * Get source image.
147
     *
148
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
149
     *
150
     * @return string
151
     */
152
    private function getSourceImage($bottom_area)
153
    {
154
        $image = $bottom_area->find('.picSurround img', 0)->getAttribute('data-src');
155
156
        return Helper::imageUrlCleaner($image);
157
    }
158
159
    /**
160
     * Get review id.
161
     *
162
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
163
     *
164
     * @return string
165
     */
166
    private function getReviewUser($top_area)
167
    {
168
        $user = $top_area->find('table', 0);
169
170
        return $user->find('td', 1)->find('a', 0)->plaintext;
171
    }
172
173
    /**
174
     * Get review image.
175
     *
176
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
177
     *
178
     * @return string
179
     */
180
    private function getReviewImage($top_area)
181
    {
182
        $image = $top_area->find('table', 0);
183
        $image = $image->find('td', 0)->find('img', 0)->src;
184
185
        return Helper::imageUrlCleaner($image);
186
    }
187
188
    /**
189
     * Get review helful.
190
     *
191
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
192
     *
193
     * @return string
194
     */
195
    private function getReviewHelpful($top_area)
196
    {
197
        $helpful = $top_area->find('table', 0);
198
        $helpful = $helpful->find('td', 1)->find('strong', 0)->plaintext;
199
200
        return trim($helpful);
201
    }
202
203
    /**
204
     * Get review date.
205
     *
206
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
207
     *
208
     * @return array
209
     */
210
    private function getReviewDate($top_area)
211
    {
212
        $date = $top_area->find('div div', 0);
213
214
        return [
215
            'date' => $date->plaintext,
216
            'time' => $date->title,
217
        ];
218
    }
219
220
    /**
221
     * Get review episode seen.
222
     *
223
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
224
     *
225
     * @return string
226
     */
227
    private function getReviewEpisode($top_area)
228
    {
229
        $episode = $top_area->find('div div', 1)->plaintext;
230
        $episode = str_replace(['episodes seen', 'chapters read'], '', $episode);
231
232
        return trim($episode);
233
    }
234
235
    /**
236
     * Get review score.
237
     *
238
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
239
     *
240
     * @return array
241
     */
242
    private function getReviewScore($bottom_area)
243
    {
244
        $score = [];
245
        $score_area = $bottom_area->find('table', 0);
246
        if ($score_area) {
247
            foreach ($score_area->find('tr') as $each_score) {
248
                $score_type = strtolower($each_score->find('td', 0)->plaintext);
249
                $score_value = $each_score->find('td', 1)->plaintext;
250
                $score[$score_type] = $score_value;
251
            }
252
        }
253
254
        return $score;
255
    }
256
257
    /**
258
     * Get review text.
259
     *
260
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
261
     *
262
     * @return string
263
     */
264
    private function getReviewText($bottom_area)
265
    {
266
        $useless_area = $bottom_area->find('div', 0);
267
        $useless_area_1 = $useless_area->plaintext;
268
        $useless_area_2 = $useless_area->next_sibling()->plaintext;
269
        $useless_area_3 = $bottom_area->find('div[id^=revhelp_output]', 0)->plaintext;
270
        $useless_area_4 = $bottom_area->find('a[id^=reviewToggle]', 0) ? $bottom_area->find('a[id^=reviewToggle]', 0)->plaintext : null;
271
        $text = str_replace([$useless_area_1, $useless_area_2, $useless_area_3, $useless_area_4], '', $bottom_area->plaintext);
272
        $text = str_replace('&lt;', '<', $text);
273
274
        return trim(preg_replace('/\h+/', ' ', $text));
275
    }
276
277
    /**
278
     * Get anime/mange review.
279
     *
280
     * @return array
281
     */
282
    private function getAllInfo()
283
    {
284
        $data = [];
285
        $review_area = $this->_parser->find('.borderDark', 0);
286
287
        $top_area = $review_area->find('.spaceit', 0);
288
        $bottom_area = $top_area->next_sibling();
289
        $very_bottom_area = $bottom_area->next_sibling();
0 ignored issues
show
Unused Code introduced by
The assignment to $very_bottom_area is dead and can be removed.
Loading history...
290
291
        $data['id'] = $this->getId();
292
        $data['source'] = $this->getReviewSource($top_area, $bottom_area);
293
        $data['username'] = $this->getReviewUser($top_area);
294
        $data['image'] = $this->getReviewImage($top_area);
295
        $data['helpful'] = $this->getReviewHelpful($top_area);
296
        $data['date'] = $this->getReviewDate($top_area);
297
        if ($this->_type == 'anime') {
298
            $data['episode'] = $this->getReviewEpisode($top_area);
299
        } else {
300
            $data['chapter'] = $this->getReviewEpisode($top_area);
301
        }
302
        $data['score'] = $this->getReviewScore($bottom_area);
303
        $data['review'] = $this->getReviewText($bottom_area);
304
305
        return $data;
306
    }
307
}
308