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