Passed
Branch feature/super-model (24c950)
by axel
02:55
created

StatModel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model;
4
5
use MalScraper\Helper\Helper;
6
7
/**
8
 * StatModel class.
9
 */
10
class StatModel extends MainModel
11
{
12
    /**
13
     * Type of info. Either anime or manga.
14
     *
15
     * @var string
16
     */
17
	private $_type;
18
19
    /**
20
     * Id of the anime or manga.
21
     *
22
     * @var string|int
23
     */
24
	private $_id;
25
26
    /**
27
     * Default constructor.
28
     *
29
     * @param string $type
30
     * @param string|int $id
31
     * @param string $parserArea
32
     *
33
     * @return void
34
     */
35
	public function __construct($type, $id, $parserArea = '.js-scrollfix-bottom-rel')
36
    {
37
    	$this->_type = $type;
38
    	$this->_id = $id;
39
        $this->_url = $this->_myAnimeListUrl.'/'.$type.'/'.$id;
40
    	$this->_parserArea = $parserArea;
41
42
        parent::errorCheck($this);
43
    }
44
45
    /**
46
     * Default call.
47
     *
48
     * @param string $method
49
     * @param array  $arguments
50
     *
51
     * @return array|string|int
52
     */
53
    public function __call($method, $arguments)
54
    {
55
        if ($this->_error)
56
            return $this->_error;
57
        return call_user_func_array([$this, $method], $arguments);
58
    }
59
60
    /**
61
     * Get type (anime or manga).
62
     *
63
     * @return string
64
     */
65
    private function getType()
66
    {
67
        return $this->_type;
68
    }
69
70
    /**
71
     * Get anime/manga id.
72
     *
73
     * @return string
74
     */
75
    private function getId()
76
    {
77
    	return $this->_id;
78
    }
79
80
    /**
81
     * Get anime/manga stat summary.
82
     *
83
     * @return array
84
     */
85
    private function getSummary()
86
    {
87
        $summary = [];
88
        $summary_area = $this->_parser->find('h2', 0)->next_sibling();
89
        if ($summary_area->tag == 'div') {
90
            while (true) {
91
92
                // status
93
                $temp_type = $summary_area->find('span', 0)->plaintext;
94
                $summary_type = trim(str_replace(':', '', strtolower($temp_type)));
95
96
                // count
97
                $status_area = $summary_area->plaintext;
98
                $count = str_replace($temp_type, '', $status_area);
99
                $summary[$summary_type] = trim(str_replace(',', '', $count));
100
101
                $summary_area = $summary_area->next_sibling();
102
                if ($summary_area->tag != 'div') {
103
                    break;
104
                }
105
            }
106
        }
107
        return $summary;
108
    }
109
110
    /**
111
     * Get anime/manga stat score.
112
     *
113
     * @return array
114
     */
115
    private function getScore()
116
    {
117
        $score = [];
118
        $score_area = $this->_parser->find('h2', 1)->next_sibling();
119
        if ($score_area->tag == 'table') {
120
            foreach ($score_area->find('tr') as $each_score) {
121
                $temp_score = [];
122
123
                // type
124
                $score_type = $each_score->find('td', 0)->plaintext;
125
                $temp_score['type'] = $score_type;
126
127
                // vote
128
                $temp_vote = $each_score->find('td', 1)->find('span small', 0)->plaintext;
129
                $vote = substr($temp_vote, 1, strlen($temp_vote) - 2);
130
                $temp_score['vote'] = str_replace(' votes', '', $vote);
131
132
                // percent
133
                $percent = $each_score->find('td', 1)->find('span', 0)->plaintext;
134
                $percent = str_replace([$temp_vote, '%'], '', $percent);
135
                $temp_score['percent'] = trim($percent);
136
137
                $score[] = $temp_score;
138
            }
139
        }
140
        return $score;
141
    }
142
143
    /**
144
     * Get anime/manga stat user.
145
     *
146
     * @return array
147
     */
148
    private function getUser()
149
    {
150
        $user = [];
151
        $user_area = $this->_parser->find('.table-recently-updated', 0);
152
        if ($user_area) {
153
            foreach ($user_area->find('tr') as $each_user) {
154
                if (!$each_user->find('td', 0)->find('div', 0))
155
                    continue;
156
157
                $temp_user = [];
158
159
                $username_area = $each_user->find('td', 0);
160
161
                $temp_user['image'] = self::getUserImage($username_area);
162
                $temp_user['username'] = self::getUsername($username_area);
163
                $temp_user['score'] = self::getUserScore($each_user);
164
                $temp_user['status'] = self::getUserStatus($each_user);
165
166
                if ($this->_type == 'anime') {
167
                    $temp_user['episode'] = self::getUserProgress($each_user, 3);
168
                    $temp_user['date'] = self::getUserDate($each_user, 4);
169
                } else {
170
                    $temp_user['volume'] = self::getUserProgress($each_user, 3);
171
                    $temp_user['chapter'] = self::getUserProgress($each_user, 4);
172
                    $temp_user['date'] = self::getUserDate($each_user, 5);
173
                }
174
175
                $user[] = $temp_user;
176
            }
177
        }
178
        return $user;
179
    }
180
181
    /**
182
     * Get username.
183
     *
184
     * @param \simplehtmldom_1_5\simple_html_dom $username_area
185
     *
186
     * @return string
187
     */
188
    static private function getUsername($username_area)
189
    {
190
        return $username_area->find('a', 1)->plaintext;
191
    }
192
193
    /**
194
     * Get user image.
195
     *
196
     * @param \simplehtmldom_1_5\simple_html_dom $username_area
197
     *
198
     * @return string
199
     */
200
    static private function getUserImage($username_area)
201
    {
202
        $user_image = $username_area->find('a', 0)->style;
203
        $user_image = substr($user_image, 21, strlen($user_image) - 22);
204
        return Helper::imageUrlCleaner($user_image);
205
    }
206
207
    /**
208
     * Get user image.
209
     *
210
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
211
     *
212
     * @return string
213
     */
214
    static private function getUserScore($each_user)
215
    {
216
        return $each_user->find('td', 1)->plaintext;
217
    }
218
219
    /**
220
     * Get user status.
221
     *
222
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
223
     *
224
     * @return string
225
     */
226
    static private function getUserStatus($each_user)
227
    {
228
        return strtolower($each_user->find('td', 2)->plaintext);
229
    }
230
231
    /**
232
     * Get user progress.
233
     *
234
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
235
     * @param int $count
236
     *
237
     * @return string
238
     */
239
    static private function getUserProgress($each_user, $count = 3)
240
    {
241
        $progress = $each_user->find('td', $count)->plaintext;
242
        return str_replace(' ', '', $progress);
243
    }
244
245
    /**
246
     * Get user date.
247
     *
248
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
249
     * @param int $count
250
     *
251
     * @return string
252
     */
253
    static private function getUserDate($each_user, $count = 4)
254
    {
255
        return $each_user->find('td', $count)->plaintext;
256
    }
257
258
    /**
259
     * Get anime/manga stat info.
260
     *
261
     * @return array
262
     */
263
    private function getAllInfo()
264
    {
265
        $data = [
266
            'summary' => self::getSummary(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\StatModel::getSummary() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

266
            'summary' => self::/** @scrutinizer ignore-call */ getSummary(),
Loading history...
267
            'score'   => self::getScore(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\StatModel::getScore() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

267
            'score'   => self::/** @scrutinizer ignore-call */ getScore(),
Loading history...
268
            'user'    => self::getUser(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\StatModel::getUser() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

268
            'user'    => self::/** @scrutinizer ignore-call */ getUser(),
Loading history...
269
        ];
270
271
        return $data;
272
    }
273
}