ReviewModel::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
114
        return strtolower($type);
115
    }
116
117
    /**
118
     * Get source id.
119
     *
120
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
121
     *
122
     * @return string
123
     */
124
    private function getSourceId($source_area)
125
    {
126
        $id = $source_area->find('strong a', 0)->href;
127
        $id = explode('/', $id);
128
129
        return $id[4];
130
    }
131
132
    /**
133
     * Get source title.
134
     *
135
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
136
     *
137
     * @return string
138
     */
139
    private function getSourceTitle($source_area)
140
    {
141
        $title = $source_area->find('strong', 0)->plaintext;
142
143
        return trim($title);
144
    }
145
146
    /**
147
     * Get source image.
148
     *
149
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
150
     *
151
     * @return string
152
     */
153
    private function getSourceImage($bottom_area)
154
    {
155
        $image = $bottom_area->find('.picSurround img', 0)->getAttribute('data-src');
156
157
        return Helper::imageUrlCleaner($image);
158
    }
159
160
    /**
161
     * Get review id.
162
     *
163
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
164
     *
165
     * @return string
166
     */
167
    private function getReviewUser($top_area)
168
    {
169
        $user = $top_area->find('table', 0);
170
171
        return $user->find('td', 1)->find('a', 0)->plaintext;
172
    }
173
174
    /**
175
     * Get review image.
176
     *
177
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
178
     *
179
     * @return string
180
     */
181
    private function getReviewImage($top_area)
182
    {
183
        $image = $top_area->find('table', 0);
184
        $image = $image->find('td', 0)->find('img', 0)->src;
185
186
        return Helper::imageUrlCleaner($image);
187
    }
188
189
    /**
190
     * Get review helful.
191
     *
192
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
193
     *
194
     * @return string
195
     */
196
    private function getReviewHelpful($top_area)
197
    {
198
        $helpful = $top_area->find('table', 0);
199
        $helpful = $helpful->find('td', 1)->find('strong', 0)->plaintext;
200
201
        return trim($helpful);
202
    }
203
204
    /**
205
     * Get review date.
206
     *
207
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
208
     *
209
     * @return array
210
     */
211
    private function getReviewDate($top_area)
212
    {
213
        $date = $top_area->find('div div', 0);
214
215
        return [
216
            'date' => $date->plaintext,
217
            'time' => $date->title,
218
        ];
219
    }
220
221
    /**
222
     * Get review episode seen.
223
     *
224
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
225
     *
226
     * @return string
227
     */
228
    private function getReviewEpisode($top_area)
229
    {
230
        $episode = $top_area->find('div div', 1)->plaintext;
231
        $episode = str_replace(['episodes seen', 'chapters read'], '', $episode);
232
233
        return trim($episode);
234
    }
235
236
    /**
237
     * Get review score.
238
     *
239
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
240
     *
241
     * @return array
242
     */
243
    private function getReviewScore($bottom_area)
244
    {
245
        $score = [];
246
        $score_area = $bottom_area->find('table', 0);
247
        if ($score_area) {
248
            foreach ($score_area->find('tr') as $each_score) {
249
                $score_type = strtolower($each_score->find('td', 0)->plaintext);
250
                $score_value = $each_score->find('td', 1)->plaintext;
251
                $score[$score_type] = $score_value;
252
            }
253
        }
254
255
        return $score;
256
    }
257
258
    /**
259
     * Get review text.
260
     *
261
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
262
     *
263
     * @return string
264
     */
265
    private function getReviewText($bottom_area)
266
    {
267
        $useless_area = $bottom_area->find('div', 0);
268
        $useless_area_1 = $useless_area->plaintext;
269
        $useless_area_2 = $useless_area->next_sibling()->plaintext;
270
        $useless_area_3 = $bottom_area->find('div[id^=revhelp_output]', 0)->plaintext;
271
        $useless_area_4 = $bottom_area->find('a[id^=reviewToggle]', 0) ? $bottom_area->find('a[id^=reviewToggle]', 0)->plaintext : null;
272
        $text = str_replace([$useless_area_1, $useless_area_2, $useless_area_3, $useless_area_4], '', $bottom_area->plaintext);
273
        $text = str_replace('&lt;', '<', $text);
274
275
        return trim(preg_replace('/\h+/', ' ', $text));
276
    }
277
278
    /**
279
     * Get anime/mange review.
280
     *
281
     * @return array
282
     */
283
    private function getAllInfo()
284
    {
285
        $data = [];
286
        $review_area = $this->_parser->find('.borderDark', 0);
287
288
        $top_area = $review_area->find('.spaceit', 0);
289
        $bottom_area = $top_area->next_sibling();
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