|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\LinkChecker\Reporters; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\UriInterface; |
|
9
|
|
|
use Spatie\Crawler\CrawlObserver; |
|
10
|
|
|
|
|
11
|
|
|
abstract class BaseReporter extends CrawlObserver |
|
12
|
|
|
{ |
|
13
|
|
|
const UNRESPONSIVE_HOST = 'Host did not respond'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $urlsGroupedByStatusCode = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Called when the crawler has crawled the given url successfully. |
|
22
|
|
|
* |
|
23
|
|
|
* @param \Psr\Http\Message\UriInterface $url |
|
24
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
25
|
|
|
* @param null|\Psr\Http\Message\UriInterface $foundOnUrl |
|
26
|
|
|
* |
|
27
|
|
|
* @return int|string |
|
28
|
|
|
*/ |
|
29
|
|
|
public function crawled( |
|
30
|
|
|
UriInterface $url, |
|
31
|
|
|
ResponseInterface $response, |
|
32
|
|
|
?UriInterface $foundOnUrl = null |
|
33
|
|
|
) { |
|
34
|
|
|
$statusCode = $response->getStatusCode(); |
|
35
|
|
|
|
|
36
|
|
|
if (! $this->isExcludedStatusCode($statusCode)) { |
|
37
|
|
|
$this->urlsGroupedByStatusCode[$statusCode][] = $url; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $statusCode; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Called when the crawler had a problem crawling the given url. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Psr\Http\Message\UriInterface $url |
|
47
|
|
|
* @param \GuzzleHttp\Exception\RequestException $requestException |
|
48
|
|
|
* @param \Psr\Http\Message\UriInterface|null $foundOnUrl |
|
49
|
|
|
*/ |
|
50
|
|
|
public function crawlFailed( |
|
51
|
|
|
UriInterface $url, |
|
52
|
|
|
RequestException $requestException, |
|
53
|
|
|
?UriInterface $foundOnUrl = null |
|
54
|
|
|
) { |
|
55
|
|
|
$statusCode = $requestException->getCode(); |
|
56
|
|
|
|
|
57
|
|
|
if (! $this->isExcludedStatusCode($statusCode)) { |
|
58
|
|
|
$this->urlsGroupedByStatusCode[$statusCode][] = $url; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $statusCode; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Determine if the statuscode concerns a successful or |
|
66
|
|
|
* redirect response. |
|
67
|
|
|
* |
|
68
|
|
|
* @param int|string $statusCode |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function isSuccessOrRedirect($statusCode): bool |
|
72
|
|
|
{ |
|
73
|
|
|
return Str::startsWith($statusCode, ['2', '3']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Determine if the crawler saw some bad urls. |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function crawledBadUrls(): bool |
|
80
|
|
|
{ |
|
81
|
|
|
return collect($this->urlsGroupedByStatusCode)->keys()->filter(function ($statusCode) { |
|
82
|
|
|
return ! $this->isSuccessOrRedirect($statusCode); |
|
83
|
|
|
})->count() > 0; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Determine if the statuscode should be excluded' |
|
88
|
|
|
* from the reporter. |
|
89
|
|
|
* |
|
90
|
|
|
* @param int|string $statusCode |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function isExcludedStatusCode($statusCode): bool |
|
95
|
|
|
{ |
|
96
|
|
|
$excludedStatusCodes = config('laravel-link-checker.reporters.exclude_status_codes', []); |
|
97
|
|
|
|
|
98
|
|
|
return in_array($statusCode, $excludedStatusCodes); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|