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.
Completed
Push — master ( 6a638f...c871b7 )
by Freek
02:02
created

CrawlLogger::setOutputFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\HttpStatusCheck;
4
5
use Spatie\Crawler\Url;
6
use Spatie\Crawler\CrawlObserver;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class CrawlLogger implements CrawlObserver
10
{
11
    const UNRESPONSIVE_HOST = 'Host did not respond';
12
13
    /**
14
     * @var \Symfony\Component\Console\Output\OutputInterface
15
     */
16
    protected $output;
17
18
    /**
19
     * @var array
20
     */
21
    protected $crawledUrls = [];
22
23
    /**
24
     * @var string|null
25
     */
26
    protected $outputFile = null;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $overwriteOutputFile = false;
32
33
    /**
34
     * @param \Symfony\Component\Console\Output\OutputInterface $output
35
     */
36
    public function __construct(OutputInterface $output)
37
    {
38
        $this->output = $output;
39
    }
40
41
    /**
42
     * Called when the crawl will crawl the url.
43
     *
44
     * @param \Spatie\Crawler\Url $url
45
     */
46
    public function willCrawl(Url $url)
47
    {
48
    }
49
50
    /**
51
     * Called when the crawler has crawled the given url.
52
     *
53
     * @param \Spatie\Crawler\Url $url
54
     * @param \Psr\Http\Message\ResponseInterface|null $response
55
     * @param \Spatie\Crawler\Url $foundOn
56
     */
57
    public function hasBeenCrawled(Url $url, $response, Url $foundOn = null)
58
    {
59
        $statusCode = $response ? $response->getStatusCode() : self::UNRESPONSIVE_HOST;
60
61
        $reason = $response ? $response->getReasonPhrase() : '';
62
63
        $colorTag = $this->getColorTagForStatusCode($statusCode);
64
65
        $timestamp = date('Y-m-d H:i:s');
66
67
        $message = (string) $url;
68
69
        if ($foundOn && $colorTag === 'error') {
70
            $message .= " (found on {$foundOn})";
71
        }
72
73
        $this->output->writeln("<{$colorTag}>[{$timestamp}] {$statusCode} {$reason} - {$message}</{$colorTag}>");
74
75
        $this->crawledUrls[$statusCode][] = $url;
76
    }
77
78
    /**
79
     * Called when the crawl has ended.
80
     */
81
    public function finishedCrawling()
82
    {
83
        $outputFileData = [];
84
        $outputFileData[] = '';
85
        $outputFileData[] = 'Crawling summary';
86
        $outputFileData[] = '----------------';
87
88
        $this->output->writeln('');
89
        $this->output->writeln('Crawling summary');
90
        $this->output->writeln('----------------');
91
92
        ksort($this->crawledUrls);
93
94
        foreach ($this->crawledUrls as $statusCode => $urls) {
95
            $colorTag = $this->getColorTagForStatusCode($statusCode);
96
97
            $count = count($urls);
98
99
            if (is_numeric($statusCode)) {
100
                $this->output->writeln("<{$colorTag}>Crawled {$count} url(s) with statuscode {$statusCode}</{$colorTag}>");
101
            }
102
103
            if ($statusCode == static::UNRESPONSIVE_HOST) {
104
                $this->output->writeln("<{$colorTag}>{$count} url(s) did have unresponsive host(s)</{$colorTag}>");
105
            }
106
107
            if ($this->outputFile !== null && $colorTag !== 'info') {
108
                $outputFileData[] = "Status: {$statusCode}";
109
                $outputFileData = array_merge($outputFileData, $urls);
110
            }
111
        }
112
113
        $outputFileData[] = '';
114
        $this->output->writeln('');
115
116
        if ($this->outputFile !== null) {
117
            if ($this->overwriteOutputFile === false) {
118
                file_put_contents($this->outputFile, implode(\PHP_EOL, $outputFileData), \FILE_APPEND);
119
            } else {
120
                file_put_contents($this->outputFile, implode(\PHP_EOL, $outputFileData));
121
            }
122
        }
123
    }
124
125
    protected function getColorTagForStatusCode(string $code): string
126
    {
127
        if ($this->startsWith($code, '2')) {
128
            return 'info';
129
        }
130
131
        if ($this->startsWith($code, '3')) {
132
            return 'comment';
133
        }
134
135
        return 'error';
136
    }
137
138
    /**
139
     * @param string|null $haystack
140
     * @param string|array $needles
141
     *
142
     * @return bool
143
     */
144
    public function startsWith($haystack, $needles): bool
145
    {
146
        foreach ((array) $needles as $needle) {
147
            if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
148
                return true;
149
            }
150
        }
151
152
        return false;
153
    }
154
155
    /**
156
     * Set the filename to write the output log.
157
     *
158
     * @param string $filename
159
     */
160
    public function setOutputFile($filename)
161
    {
162
        $this->outputFile = $filename;
163
    }
164
165
    /**
166
     * Set the state indicating if the output file should be overwritten.
167
     *
168
     * @param bool $flag
169
     */
170
    public function setOverwriteOutputFile($flag)
171
    {
172
        $this->overwriteOutputFile = (bool) $flag;
173
    }
174
}
175