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

MainModel::getAbsoluteUrl()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 11
nop 1
dl 0
loc 29
rs 8.6186
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model;
4
5
define('MAX_FILE_SIZE', 100000000);
6
7
use DateTime;
8
use HtmlDomParser;
9
10
/**
11
 * MainModel class.
12
 *
13
 * Base model for all model class.
14
 */
15
class MainModel
16
{
17
	/**
18
     * MyAnimeList main URL
19
     *
20
     * @var string
21
     */
22
	protected $_myAnimeListUrl = 'https://myanimelist.net';
23
24
	/**
25
     * Trimmed HtmlDomParser.
26
     *
27
     * @var \simplehtmldom_1_5\simple_html_dom
28
     */
29
	protected $_parser;
30
31
	/**
32
     * Area to be parsed.
33
     *
34
     * @var string
35
     */
36
    protected $_parserArea;
37
38
    /**
39
     * Complete MyAnimeList page URL.
40
     *
41
     * @var string
42
     */
43
    protected $_url;
44
45
    /**
46
     * Error response.
47
     *
48
     * @var string|int
49
     */
50
    protected $_error;
51
52
    /**
53
     * Get URL header.
54
     *
55
     * @param string $url URL of full MyAnimeList page
56
     *
57
     * @return int
58
     */
59
	static function getHeader($url)
60
	{
61
		$file_headers = @get_headers($url);
62
	    if (empty($file_headers) || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
63
	        return 404;
64
	    }
65
	    return 200;
66
	}
67
68
    /**
69
     * Get trimmed HtmlDomParser class.
70
     *
71
     * @param string $url URL of full MyAnimeList page
72
     * @param string $contentDiv Specific area to be parsed
73
     *
74
     * @return \simplehtmldom_1_5\simple_html_dom
75
     */
76
	static function getParser($url,$contentDiv, $additionalSetting = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
77
	{
78
		$html = HtmlDomParser::file_get_html($url)->find($contentDiv, 0);
79
		$html = !$additionalSetting ? $html : $html->next_sibling();
80
		$html = $html->outertext;
81
	    $html = str_replace('&quot;', '\"', $html);
82
	    $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
83
	    $html = HtmlDomParser::str_get_html($html);
84
85
    	return $html;
86
	}
87
88
    /**
89
     * Header error check.
90
     *
91
     * @param MainModel $model Any model
92
     *
93
     * @return void
94
     */
95
    static function errorCheck($model)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
96
    {
97
        $header = self::getHeader($model->_url);
98
        if ($header == 200) {
99
        	$className = substr(get_class($model), 17);
100
        	$additionalSetting = ($className == 'CharacterPictureModel') || ($className == 'PeoplePictureModel');
101
            $model->_parser = self::getParser(self::getAbsoluteUrl($model), $model->_parserArea, $additionalSetting);
102
        } else {
103
            $model->_error = $header;
104
        }
105
    }
106
107
    /**
108
     * Get the correct url.
109
     *
110
     * @param MainModel $model Any model
111
     *
112
     * @return string
113
     */
114
    static function getAbsoluteUrl($model)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
115
    {
116
    	$className = substr(get_class($model), 17);
117
    	$additionalUrl = '';
118
119
    	switch ($className) {
120
    		case 'CharacterStaffModel':
121
    			$area = 'li a[href$=characters]';
122
    			break;
123
    		case 'StatModel':
124
    			$area = 'li a[href$=stats]';
125
    			$additionalUrl = '?m=all&show=1';
126
    			break;
127
    		case 'PictureModel':
128
    			$area = 'li a[href$=pics]';
129
    			break;
130
    		case 'CharacterPictureModel':
131
    		case 'PeoplePictureModel':
132
    			$area = 'li a[href$=pictures]';
133
    			break;
134
    		default:
135
    			return $model->_url;
136
    	}
137
138
    	$html = HtmlDomParser::file_get_html($model->_url)->find($area, 0)->href;
139
140
    	if ($model->getType() == 'manga')
0 ignored issues
show
introduced by
The method getType() does not exist on MalScraper\Model\MainModel. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

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

140
    	if ($model->/** @scrutinizer ignore-call */ getType() == 'manga')
Loading history...
141
    		return 'https://myanimelist.net'.$html.$additionalUrl;
142
        return $html.$additionalUrl;
143
    }
144
}