1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\LinkChecker\Reporters; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Logging\Log; |
6
|
|
|
use Spatie\Crawler\Url; |
7
|
|
|
|
8
|
|
|
class LogBrokenLinks extends BaseReporter |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Illuminate\Contracts\Logging\Log |
12
|
|
|
*/ |
13
|
|
|
protected $log; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param \Illuminate\Contracts\Logging\Log $log |
17
|
|
|
*/ |
18
|
|
|
public function __construct(Log $log) |
19
|
|
|
{ |
20
|
|
|
$this->log = $log; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Called when the crawler has crawled the given url. |
25
|
|
|
* |
26
|
|
|
* @param \Spatie\Crawler\Url $url |
27
|
|
|
* @param \Psr\Http\Message\ResponseInterface|null $response |
28
|
|
|
* @param \Spatie\Crawler\Url $foundOnUrl |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function hasBeenCrawled(Url $url, $response, Url $foundOnUrl = null) |
33
|
|
|
{ |
34
|
|
|
$statusCode = parent::hasBeenCrawled($url, $response); |
35
|
|
|
|
36
|
|
|
if ($this->isSuccessOrRedirect($statusCode)) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($this->excludeStatusCode($statusCode)) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$reason = $response ? $response->getReasonPhrase() : ''; |
45
|
|
|
|
46
|
|
|
$logMessage = "{$statusCode} {$reason} - {$url}"; |
47
|
|
|
|
48
|
|
|
if ($foundOnUrl) { |
49
|
|
|
$logMessage .= " (found on {$foundOnUrl}"; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->log->warning($logMessage); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Called when the crawl has ended. |
57
|
|
|
*/ |
58
|
|
|
public function finishedCrawling() |
59
|
|
|
{ |
60
|
|
|
$this->log->info('link checker summary'); |
61
|
|
|
|
62
|
|
|
collect($this->urlsGroupedByStatusCode) |
63
|
|
|
->each(function ($urls, $statusCode) { |
64
|
|
|
if ($this->isSuccessOrRedirect($statusCode)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$count = count($urls); |
69
|
|
|
|
70
|
|
|
if ($statusCode == static::UNRESPONSIVE_HOST) { |
71
|
|
|
$this->log->warning("{$count} url(s) did have unresponsive host(s)"); |
72
|
|
|
|
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->log->warning("Crawled {$count} url(s) with statuscode {$statusCode}"); |
77
|
|
|
|
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|