GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CrawlLogger   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 4
dl 0
loc 208
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A willCrawl() 0 3 1
A finishedCrawling() 0 24 4
A getColorTagForStatusCode() 0 12 3
A startsWith() 0 10 4
A setOutputFile() 0 4 1
A crawled() 0 17 2
A crawlFailed() 0 11 2
B addResult() 0 28 7
B addRedirectedResult() 0 40 5
1
<?php
2
3
namespace Spatie\HttpStatusCheck;
4
5
use GuzzleHttp\Exception\RequestException;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\UriInterface;
8
use Spatie\Crawler\CrawlObserver;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class CrawlLogger extends CrawlObserver
12
{
13
    const UNRESPONSIVE_HOST = 'Host did not respond';
14
    const REDIRECT = 'Redirect';
15
16
    /**
17
     * @var \Symfony\Component\Console\Output\OutputInterface
18
     */
19
    protected $consoleOutput;
20
21
    /**
22
     * @var array
23
     */
24
    protected $crawledUrls = [];
25
26
    /**
27
     * @var string|null
28
     */
29
    protected $outputFile = null;
30
31
    /**
32
     * @param \Symfony\Component\Console\Output\OutputInterface $consoleOutput
33
     */
34
    public function __construct(OutputInterface $consoleOutput)
35
    {
36
        $this->consoleOutput = $consoleOutput;
37
    }
38
39
    /**
40
     * Called when the crawl will crawl the url.
41
     *
42
     * @param \Psr\Http\Message\UriInterface $url
43
     */
44
    public function willCrawl(UriInterface $url)
45
    {
46
    }
47
48
    /**
49
     * Called when the crawl has ended.
50
     */
51
    public function finishedCrawling()
52
    {
53
        $this->consoleOutput->writeln('');
54
        $this->consoleOutput->writeln('Crawling summary');
55
        $this->consoleOutput->writeln('----------------');
56
57
        ksort($this->crawledUrls);
58
59
        foreach ($this->crawledUrls as $statusCode => $urls) {
60
            $colorTag = $this->getColorTagForStatusCode($statusCode);
61
62
            $count = count($urls);
63
64
            if (is_numeric($statusCode)) {
65
                $this->consoleOutput->writeln("<{$colorTag}>Crawled {$count} url(s) with statuscode {$statusCode}</{$colorTag}>");
66
            }
67
68
            if ($statusCode == static::UNRESPONSIVE_HOST) {
69
                $this->consoleOutput->writeln("<{$colorTag}>{$count} url(s) did have unresponsive host(s)</{$colorTag}>");
70
            }
71
        }
72
73
        $this->consoleOutput->writeln('');
74
    }
75
76
    protected function getColorTagForStatusCode(string $code): string
77
    {
78
        if ($this->startsWith($code, '2')) {
79
            return 'info';
80
        }
81
82
        if ($this->startsWith($code, '3')) {
83
            return 'comment';
84
        }
85
86
        return 'error';
87
    }
88
89
    /**
90
     * @param string|null $haystack
91
     * @param string|array $needles
92
     *
93
     * @return bool
94
     */
95
    public function startsWith($haystack, $needles): bool
96
    {
97
        foreach ((array) $needles as $needle) {
98
            if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
99
                return true;
100
            }
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * Set the filename to write the output log.
108
     *
109
     * @param string $filename
110
     */
111
    public function setOutputFile($filename)
112
    {
113
        $this->outputFile = $filename;
114
    }
115
116
    public function crawled(
117
        UriInterface $url,
118
        ResponseInterface $response,
119
        ?UriInterface $foundOnUrl = null
120
    ) {
121
        if ($this->addRedirectedResult($url, $response, $foundOnUrl)) {
122
            return;
123
        }
124
125
        // response wasnt a redirect so lets add it as a standard result
126
        $this->addResult(
127
            (string) $url,
128
            (string) $foundOnUrl,
129
            $response->getStatusCode(),
130
            $response->getReasonPhrase()
131
        );
132
    }
133
134
    public function crawlFailed(
135
        UriInterface $url,
136
        RequestException $requestException,
137
        ?UriInterface $foundOnUrl = null
138
    ) {
139
        if ($response = $requestException->getResponse()) {
140
            $this->crawled($url, $response, $foundOnUrl);
141
        } else {
142
            $this->addResult((string) $url, (string) $foundOnUrl, '---', self::UNRESPONSIVE_HOST);
143
        }
144
    }
145
146
    public function addResult($url, $foundOnUrl, $statusCode, $reason)
147
    {
148
        /*
149
        * don't display duplicate results
150
        * this happens if a redirect is followed to an existing page
151
        */
152
        if (isset($this->crawledUrls[$statusCode]) && in_array($url, $this->crawledUrls[$statusCode])) {
153
            return;
154
        }
155
156
        $colorTag = $this->getColorTagForStatusCode($statusCode);
157
158
        $timestamp = date('Y-m-d H:i:s');
159
160
        $message = "{$statusCode} {$reason} - ".(string) $url;
161
162
        if ($foundOnUrl && $colorTag === 'error') {
163
            $message .= " (found on {$foundOnUrl})";
164
        }
165
166
        if ($this->outputFile && $colorTag === 'error') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->outputFile of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
167
            file_put_contents($this->outputFile, $message.PHP_EOL, FILE_APPEND);
168
        }
169
170
        $this->consoleOutput->writeln("<{$colorTag}>[{$timestamp}] {$message}</{$colorTag}>");
171
172
        $this->crawledUrls[$statusCode][] = $url;
173
    }
174
175
    /*
176
    * https://github.com/guzzle/guzzle/blob/master/docs/faq.rst#how-can-i-track-redirected-requests
177
    */
178
    public function addRedirectedResult(
179
        UriInterface $url,
180
        ResponseInterface $response,
181
        ?UriInterface $foundOnUrl = null
182
    ) {
183
        // if its not a redirect the return false
184
        if (! $response->getHeader('X-Guzzle-Redirect-History')) {
185
            return false;
186
        }
187
188
        // retrieve Redirect URI history
189
        $redirectUriHistory = $response->getHeader('X-Guzzle-Redirect-History');
190
191
        // retrieve Redirect HTTP Status history
192
        $redirectCodeHistory = $response->getHeader('X-Guzzle-Redirect-Status-History');
193
194
        // Add the initial URI requested to the (beginning of) URI history
195
        array_unshift($redirectUriHistory, (string) $url);
196
197
        // Add the final HTTP status code to the end of HTTP response history
198
        array_push($redirectCodeHistory, $response->getStatusCode());
199
200
        // Combine the items of each array into a single result set
201
        $fullRedirectReport = [];
202
        foreach ($redirectUriHistory as $key => $value) {
203
            $fullRedirectReport[$key] = ['location' => $value, 'code' => $redirectCodeHistory[$key]];
204
        }
205
206
        // Add the redirects and final URL as results
207
        foreach ($fullRedirectReport as $k=>$redirect) {
208
            $this->addResult(
209
                (string) $redirect['location'],
210
                (string) $foundOnUrl,
211
                $redirect['code'],
212
                $k + 1 == count($fullRedirectReport) ? $response->getReasonPhrase() : self::REDIRECT
213
            );
214
        }
215
216
        return true;
217
    }
218
}
219