Passed
Push — feature/super-model ( 24c950...13c8a0 )
by axel
02:30
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\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
                // type
125
                $score_type = $each_score->find('td', 0)->plaintext;
126
                $temp_score['type'] = $score_type;
127
128
                // vote
129
                $temp_vote = $each_score->find('td', 1)->find('span small', 0)->plaintext;
130
                $vote = substr($temp_vote, 1, strlen($temp_vote) - 2);
131
                $temp_score['vote'] = str_replace(' votes', '', $vote);
132
133
                // percent
134
                $percent = $each_score->find('td', 1)->find('span', 0)->plaintext;
135
                $percent = str_replace([$temp_vote, '%'], '', $percent);
136
                $temp_score['percent'] = trim($percent);
137
138
                $score[] = $temp_score;
139
            }
140
        }
141
        return $score;
142
    }
143
144
    /**
145
     * Get anime/manga stat user.
146
     *
147
     * @return array
148
     */
149
    private function getUser()
150
    {
151
        $user = [];
152
        $user_area = $this->_parser->find('.table-recently-updated', 0);
153
        if ($user_area) {
154
            foreach ($user_area->find('tr') as $each_user) {
155
                if (!$each_user->find('td', 0)->find('div', 0))
156
                    continue;
157
158
                $temp_user = [];
159
160
                $username_area = $each_user->find('td', 0);
161
162
                $temp_user['image'] = self::getUserImage($username_area);
163
                $temp_user['username'] = self::getUsername($username_area);
164
                $temp_user['score'] = self::getUserScore($each_user);
165
                $temp_user['status'] = self::getUserStatus($each_user);
166
167
                if ($this->_type == 'anime') {
168
                    $temp_user['episode'] = self::getUserProgress($each_user, 3);
169
                    $temp_user['date'] = self::getUserDate($each_user, 4);
170
                } else {
171
                    $temp_user['volume'] = self::getUserProgress($each_user, 3);
172
                    $temp_user['chapter'] = self::getUserProgress($each_user, 4);
173
                    $temp_user['date'] = self::getUserDate($each_user, 5);
174
                }
175
176
                $user[] = $temp_user;
177
            }
178
        }
179
        return $user;
180
    }
181
182
    /**
183
     * Get username.
184
     *
185
     * @param \simplehtmldom_1_5\simple_html_dom $username_area
186
     *
187
     * @return string
188
     */
189
    static private function getUsername($username_area)
190
    {
191
        return $username_area->find('a', 1)->plaintext;
192
    }
193
194
    /**
195
     * Get user image.
196
     *
197
     * @param \simplehtmldom_1_5\simple_html_dom $username_area
198
     *
199
     * @return string
200
     */
201
    static private function getUserImage($username_area)
202
    {
203
        $user_image = $username_area->find('a', 0)->style;
204
        $user_image = substr($user_image, 21, strlen($user_image) - 22);
205
        return Helper::imageUrlCleaner($user_image);
206
    }
207
208
    /**
209
     * Get user image.
210
     *
211
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
212
     *
213
     * @return string
214
     */
215
    static private function getUserScore($each_user)
216
    {
217
        return $each_user->find('td', 1)->plaintext;
218
    }
219
220
    /**
221
     * Get user status.
222
     *
223
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
224
     *
225
     * @return string
226
     */
227
    static private function getUserStatus($each_user)
228
    {
229
        return strtolower($each_user->find('td', 2)->plaintext);
230
    }
231
232
    /**
233
     * Get user progress.
234
     *
235
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
236
     * @param int $count
237
     *
238
     * @return string
239
     */
240
    static private function getUserProgress($each_user, $count = 3)
241
    {
242
        $progress = $each_user->find('td', $count)->plaintext;
243
        return str_replace(' ', '', $progress);
244
    }
245
246
    /**
247
     * Get user date.
248
     *
249
     * @param \simplehtmldom_1_5\simple_html_dom $each_user
250
     * @param int $count
251
     *
252
     * @return string
253
     */
254
    static private function getUserDate($each_user, $count = 4)
255
    {
256
        return $each_user->find('td', $count)->plaintext;
257
    }
258
259
    /**
260
     * Get anime/manga stat info.
261
     *
262
     * @return array
263
     */
264
    private function getAllInfo()
265
    {
266
        $data = [
267
            'summary' => self::getSummary(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\Additio...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

267
            'summary' => self::/** @scrutinizer ignore-call */ getSummary(),
Loading history...
268
            'score'   => self::getScore(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\Additional\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

268
            'score'   => self::/** @scrutinizer ignore-call */ getScore(),
Loading history...
269
            'user'    => self::getUser(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\Additional\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

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