Completed
Push — master ( 79187e...a09da0 )
by Dev
14:40
created

Harvest::amIRedirectToHttps()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 4
nop 0
dl 0
loc 10
ccs 4
cts 5
cp 0.8
crap 5.2
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\UrlHarvester;
4
5
use PiedWeb\Curl\Response;
6
use PiedWeb\TextAnalyzer\Analyzer as TextAnalyzer;
7
use phpuri;
8
use simple_html_dom;
9
use Spatie\Robots\RobotsTxt;
10
use PiedWeb\Curl\Request as CurlRequest;
11
12
class Harvest
13
{
14
    use HarvestLinksTrait;
15
16
    const LINK_SELF = 1;
17
    const LINK_INTERNAL = 2;
18
    const LINK_SUB = 3;
19
    const LINK_EXTERNAL = 4;
20
21
    /**
22
     * @var Response
23
     */
24
    protected $response;
25
26
    /**
27
     * @var simple_html_dom
28
     */
29
    protected $dom;
30
31
    /** @var string */
32
    protected $baseUrl;
33
34
    /** @var string */
35
    protected $domain;
36
37
    /** @var RobotsTxt|string (empty string) */
38
    protected $robotsTxt;
39
40
    /** @var string */
41
    private $domainWithScheme;
42
43 15
    public static function fromUrl(
44
        string $url,
45
        string $userAgent = 'Bot: Url Harvester',
46
        string $language = 'en,en-US;q=0.5'
47
    ) {
48
        $response = Request::make($url, $userAgent, '200;html', $language);
49 15
50 15
        if ($response instanceof Response) {
0 ignored issues
show
introduced by
$response is never a sub-type of PiedWeb\Curl\Response.
Loading history...
51
            return new self($response);
52 15
        }
53 15
54
        return $response;
55
    }
56
57
    /**
58
     * @param Response $response
59
     */
60
    public function __construct(Response $response)
61
    {
62 15
        $this->response = $response;
63
    }
64 15
65 15
    public function getResponse()
66
    {
67 6
        return $this->response;
68
    }
69 6
70
    public function getRedirection()
71
    {
72 3
        $headers = $this->response->getHeaders();
73
        $headers = array_change_key_case($headers ? $headers : []);
74 3
        if (isset($headers['location'])) {
75 3
            return phpUri::parse($this->response->getEffectiveUrl())->join($headers['location']);
76 3
        }
77
78
        return false;
79
    }
80 3
81
    public function getDom()
82
    {
83 6
        if (null === $this->dom) {
84
            $this->dom = new simple_html_dom();
85 6
            $this->dom->load($this->response->getContent());
86 6
        }
87 6
88
        return $this->dom;
89
    }
90 6
91
    private function find($selector, $number = null)
92
    {
93 3
        return $this->getDom()->find($selector, $number);
94
    }
95 3
96
    private function findOne($selector)
97
    {
98 3
        return $this->find($selector, 0);
99
    }
100 3
101
    /**
102
     * Return content inside a selector.
103
     *
104
     * @return string
105
     */
106
    public function getTag($selector)
107
    {
108 3
        $found = $this->findOne($selector);
109
110 3
        return null !== $found ? Helper::clean($found->innertext) : null;
111
    }
112 3
113
    public function getUniqueTag($selector = 'title')
114
    {
115 3
        $found = $this->find($selector);
116
        if ($found) {
117 3
            if (count($found) > 1) {
118 3
                return count($found).' `'.$selector.'` !!';
119 3
            } else {
120
                return Helper::clean($found[0]->innertext);
121
            }
122 3
        }
123
    }
124
125
    /**
126
     * Return content inside a meta.
127
     *
128
     * @return string from content attribute
129
     */
130
    public function getMeta(string $name)
131
    {
132 3
        $meta = $this->findOne('meta[name='.$name.']');
133
134 3
        return null !== $meta ? (isset($meta->content) ? Helper::clean($meta->content) : '') : '';
135
    }
136 3
137
    /**
138
     * Renvoie le contenu de l'attribut href de la balise link rel=canonical.
139
     *
140
     * @return string le contenu de l'attribute href sinon NULL si la balise n'existe pas
141
     */
142
    public function getCanonical()
143
    {
144 3
        $canonical = $this->findOne('link[rel=canonical]');
145
146 3
        return null !== $canonical ? (isset($canonical->href) ? $canonical->href : '') : null;
147
    }
148 3
149
    /*
150
     * @return bool
151
     */
152
    public function isCanonicalCorrect()
153
    {
154 3
        $canonical = $this->getCanonical();
155
156 3
        return $canonical ? $this->response->getEffectiveUrl() == $canonical : true;
157
    }
158 3
159
    public function getKws()
160
    {
161 3
        $kws = TextAnalyzer::get(
162
            $this->response->getContent(),
163 3
            true,   // only sentences
164 3
            1,      // no expression, just words
165 3
            0      // keep trail
166 3
        );
167 3
168
        return $kws->getExpressions(10);
169
    }
170 3
171
    /**
172
     * @return int
173
     */
174
    public function getRatioTxtCode(): int
175
    {
176 3
        $textLenght = strlen($this->getDom()->plaintext);
177
        $htmlLenght = strlen(Helper::clean($this->response->getContent()));
178 3
179 3
        return (int) ($htmlLenght > 0 ? round($textLenght / $htmlLenght * 100) : 0);
180
    }
181 3
182
    /**
183
     * Return an array of object with two elements Link and anchor.
184
     *
185
     * @return array or NULL if we didn't found breadcrumb
186
     */
187
    public function getBreadCrumb()
188
    {
189 3
        return ExtractBreadcrumb::get(
190
            $this->response->getContent(),
191 3
            $this->getBaseUrl(),
192 3
            $this->response->getEffectiveUrl()
193 3
        );
194 3
    }
195
196
    /**
197
     * @return string|false
198 3
     */
199
    public function amIRedirectToHttps()
200 3
    {
201 3
        $headers = $this->response->getHeaders();
202 3
        $headers = array_change_key_case(null !== $headers ? $headers : []);
203
        $redirUrl = isset($headers['location']) ? $headers['location'] : null;
204
        if (null !== $redirUrl && ($httpsUrl = preg_replace('#^http://#', 'https://', $this->url, 1)) == $redirUrl) {
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on PiedWeb\UrlHarvester\Harvest. Did you maybe forget to declare it?
Loading history...
205 3
            return $httpsUrl;
206
        }
207
208
        return false;
209 3
    }
210
211
    public function getBaseUrl()
212 9
    {
213
        if (!isset($this->baseUrl)) {
214 9
            $base = $this->findOne('base');
215 9
            if (null !== $base && isset($base->href) && filter_var($base->href, FILTER_VALIDATE_URL)) {
216 9
                $this->baseUrl = $base->href;
217 9
            } else {
218
                $this->baseUrl = $this->response->getEffectiveUrl();
219
            }
220 9
        }
221
222
        return $this->baseUrl;
223
    }
224
225
    public function getDomain()
226 3
    {
227
        if (!isset($this->domain)) {
228 3
            $urlParsed = parse_url($this->response->getEffectiveUrl());
229
            preg_match("/[^\.\/]+(\.com?)?\.[^\.\/]+$/", $urlParsed['host'], $match);
230
            $this->domain = $match[0];
231
        }
232
233
        return $this->domain;
234
    }
235
236 3
    /**
237
     * @return int correspond to a const from Indexable
238 3
     */
239 3
    public function isIndexable(string $userAgent = 'googlebot')
240
    {
241 3
        return Indexable::isIndexable($this, $userAgent);
242
    }
243 3
244
    /**
245 3
     * @return RobotsTxt|string containing the current Robots.txt or NULL if an error occured
246 3
     *                          or empty string if robots is empty file
247 3
     */
248
    public function getRobotsTxt()
249 3
    {
250
        if (null === $this->robotsTxt) {
251 3
            $url = $this->getDomainAndScheme().'/robots.txt';
252
253 3
            $request = new CurlRequest($url);
254
            $request
255
                ->setDefaultSpeedOptions()
256 3
                ->setDownloadOnlyIf(function ($line) {
257
                    return 0 === stripos(trim($line), 'content-type') && false !== stripos($line, 'text/plain');
258
                })
259 6
                ->setUserAgent($this->getResponse()->getRequest()->getUserAgent())
260
            ;
261 6
            $result = $request->exec();
262 6
263
            $noNeedToParse = !$result instanceof \PiedWeb\Curl\Response || empty(trim($result->getContent()));
264
265 6
            $this->robotsTxt = $noNeedToParse ? '' : new RobotsTxt($result->getContent());
266
        }
267
268 6
        return $this->robotsTxt;
269
    }
270 6
271
    public function getDomainAndScheme()
272 6
    {
273
        if (null === $this->domainWithScheme) {
274
            $this->domainWithScheme = self::getDomainAndSchemeFrom($this->response->getEffectiveUrl());
275
        }
276
277
        return $this->domainWithScheme;
278
    }
279
280
    public static function getDomainAndSchemeFrom(string $url)
281
    {
282
        $url = parse_url($url);
283
284
        return $url['scheme'].'://'.$url['host'];
285
    }
286
}
287