Passed
Push — feature/super-model ( 9eaa75...66e03d )
by axel
01:46
created

MainModel::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model;
4
5
define('MAX_FILE_SIZE', 100000000);
6
7
use HtmlDomParser;
8
9
/**
10
 * MainModel class.
11
 *
12
 * Base model for all model class.
13
 */
14
class MainModel
15
{
16
    /**
17
     * MyAnimeList main URL.
18
     *
19
     * @var string
20
     */
21
    protected $_myAnimeListUrl = 'https://myanimelist.net';
22
23
    /**
24
     * Trimmed HtmlDomParser.
25
     *
26
     * @var \simplehtmldom_1_5\simple_html_dom
27
     */
28
    protected $_parser;
29
30
    /**
31
     * Area to be parsed.
32
     *
33
     * @var string
34
     */
35
    protected $_parserArea;
36
37
    /**
38
     * Complete MyAnimeList page URL.
39
     *
40
     * @var string
41
     */
42
    protected $_url;
43
44
    /**
45
     * Error response.
46
     *
47
     * @var string|int
48
     */
49
    protected $_error;
50
51
    /**
52
     * Get URL header.
53
     *
54
     * @param string $url URL of full MyAnimeList page
55
     *
56
     * @return int
57
     */
58
    public static function getHeader($url)
59
    {
60
        $file_headers = @get_headers($url);
61
        if (empty($file_headers) || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
62
            return 404;
63
        }
64
65
        if (empty($file_headers) || $file_headers[0] == 'HTTP/1.1 403 Forbidden') {
66
            return 403;
67
        }
68
69
        return 200;
70
    }
71
72
    /**
73
     * Get trimmed HtmlDomParser class.
74
     *
75
     * @param string $url        URL of full MyAnimeList page
76
     * @param string $contentDiv Specific area to be parsed
77
     *
78
     * @return \simplehtmldom_1_5\simple_html_dom
79
     */
80
    public static function getParser($url, $contentDiv, $additionalSetting = false)
81
    {
82
        $html = HtmlDomParser::file_get_html($url)->find($contentDiv, 0);
83
        $html = !$additionalSetting ? $html : $html->next_sibling();
84
        $html = $html->outertext;
85
        $html = str_replace('&quot;', '\"', $html);
86
        $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
87
        $html = HtmlDomParser::str_get_html($html);
88
89
        return $html;
90
    }
91
92
    /**
93
     * Header error check.
94
     *
95
     * @param MainModel $model Any model
96
     *
97
     * @return void
98
     */
99
    public static function errorCheck($model)
100
    {
101
        $className = self::getCleanClassName($model);
102
103
        if (strpos($className, 'Search') !== false) {
104
            if (strlen($model->_query) < 3) {
105
                $model->_error = 400;
106
            }
107
        }
108
109
        if (!$model->_error) {
110
            $header = self::getHeader($model->_url);
111
            if ($header == 200) {
112
                if ($className != 'UserListModel') {
113
                    $additionalSetting = ($className == 'CharacterPeoplePictureModel');
114
                    $model->_parser = self::getParser(self::getAbsoluteUrl($model), $model->_parserArea, $additionalSetting);
115
                }
116
            } else {
117
                $model->_error = $header;
118
            }
119
        }
120
    }
121
122
    /**
123
     * Get the correct url.
124
     *
125
     * @param MainModel $model Any model
126
     *
127
     * @return string
128
     */
129
    public static function getAbsoluteUrl($model)
130
    {
131
        $className = self::getCleanClassName($model);
132
        $additionalUrl = '';
133
134
        switch ($className) {
135
            case 'CharacterStaffModel':
136
                $area = 'li a[href$=characters]';
137
                break;
138
            case 'StatModel':
139
                $area = 'li a[href$=stats]';
140
                $additionalUrl = '?m=all&show=1';
141
                break;
142
            case 'PictureModel':
143
                $area = 'li a[href$=pics]';
144
                break;
145
            case 'CharacterPeoplePictureModel':
146
                $area = 'li a[href$=pictures]';
147
                break;
148
            default:
149
                return $model->_url;
150
        }
151
152
        $html = HtmlDomParser::file_get_html($model->_url)->find($area, 0)->href;
153
154
        if ($model->getType() == 'manga') {
155
            return 'https://myanimelist.net'.$html.$additionalUrl;
156
        }
157
158
        return $html.$additionalUrl;
159
    }
160
161
    /**
162
     * Get clean class name.
163
     *
164
     * @param MainModel $model Any model
165
     *
166
     * @return string
167
     */
168
    public static function getCleanClassName($model)
169
    {
170
        $className = get_class($model);
171
        $className = explode('\\', $className);
172
173
        return $className[count($className) - 1];
174
    }
175
176
    /**
177
     * Get default type.
178
     *
179
     * @return string
180
     */
181
    public function getType()
182
    {
183
        return 'anime';
184
    }
185
}
186