1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\LinkChecker\Reporters; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
use Psr\Http\Message\UriInterface; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
class LogBrokenLinks extends BaseReporter |
10
|
|
|
{ |
11
|
|
|
protected $log; |
12
|
|
|
|
13
|
|
|
public function __construct(LoggerInterface $log) |
14
|
|
|
{ |
15
|
|
|
$this->log = $log; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Called when the crawl has ended. |
20
|
|
|
*/ |
21
|
|
|
public function finishedCrawling() |
22
|
|
|
{ |
23
|
|
|
$this->log->info('link checker summary'); |
24
|
|
|
|
25
|
|
|
collect($this->urlsGroupedByStatusCode) |
26
|
|
|
->each(function ($urls, $statusCode) { |
27
|
|
|
if ($this->isSuccessOrRedirect($statusCode)) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$count = count($urls); |
32
|
|
|
|
33
|
|
|
if ($statusCode == static::UNRESPONSIVE_HOST) { |
34
|
|
|
$this->log->warning("{$count} url(s) did have unresponsive host(s)"); |
35
|
|
|
|
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->log->warning("Crawled {$count} url(s) with statuscode {$statusCode}"); |
40
|
|
|
}); |
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
|
|
|
parent::crawlFailed($url, $requestException, $foundOnUrl); |
56
|
|
|
|
57
|
|
|
$statusCode = $requestException->getCode(); |
58
|
|
|
|
59
|
|
|
if ($this->isExcludedStatusCode($statusCode)) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->log->warning( |
64
|
|
|
$this->formatLogMessage($url, $requestException, $foundOnUrl) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function formatLogMessage( |
69
|
|
|
UriInterface $url, |
70
|
|
|
RequestException $requestException, |
71
|
|
|
?UriInterface $foundOnUrl = null |
72
|
|
|
): string { |
73
|
|
|
$statusCode = $requestException->getCode(); |
74
|
|
|
|
75
|
|
|
$reason = $requestException->getMessage(); |
76
|
|
|
|
77
|
|
|
$logMessage = "{$statusCode} {$reason} - {$url}"; |
78
|
|
|
|
79
|
|
|
if ($foundOnUrl) { |
80
|
|
|
$logMessage .= " (found on {$foundOnUrl}"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $logMessage; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|