MainModel::errorCheck()   A
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 12
c 1
b 1
f 0
nc 12
nop 1
dl 0
loc 19
rs 9.2222
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 = str_replace('&lt;', '&l-t;', $html); // handle '<'
87
        $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
88
        $html = str_replace('&l-t;', '&lt;', $html);
89
        $html = HtmlDomParser::str_get_html($html);
90
91
        return $html;
92
    }
93
94
    /**
95
     * Header error check.
96
     *
97
     * @param MainModel $model Any model
98
     *
99
     * @return void
100
     */
101
    public static function errorCheck($model)
102
    {
103
        $className = self::getCleanClassName($model);
104
105
        if (strpos($className, 'Search') !== false) {
106
            if (strlen($model->_query) < 3) {
107
                $model->_error = 400;
108
            }
109
        }
110
111
        if (!$model->_error) {
112
            $header = self::getHeader($model->_url);
113
            if ($header == 200) {
114
                if ($className != 'UserListModel') {
115
                    $additionalSetting = ($className == 'CharacterPeoplePictureModel');
116
                    $model->_parser = self::getParser($model->_url, $model->_parserArea, $additionalSetting);
117
                }
118
            } else {
119
                $model->_error = $header;
120
            }
121
        }
122
    }
123
124
    /**
125
     * Get clean class name.
126
     *
127
     * @param MainModel $model Any model
128
     *
129
     * @return string
130
     */
131
    public static function getCleanClassName($model)
132
    {
133
        $className = get_class($model);
134
        $className = explode('\\', $className);
135
136
        return $className[count($className) - 1];
137
    }
138
}
139