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 ( f16087...8664b9 )
by Freek
03:41
created

CrawlLogger::willCrawl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
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
     * @param \Symfony\Component\Console\Output\OutputInterface $output
25
     */
26
    public function __construct(OutputInterface $output)
27
    {
28
        $this->output = $output;
29
    }
30
31
    /**
32
     * Called when the crawl will crawl the url.
33
     *
34
     * @param \Spatie\Crawler\Url $url
35
     */
36
    public function willCrawl(Url $url)
37
    {
38
    }
39
40
    /**
41
     * Called when the crawler has crawled the given url.
42
     *
43
     * @param \Spatie\Crawler\Url $url
44
     * @param \Psr\Http\Message\ResponseInterface|null $response
45
     * @param \Spatie\Crawler\Url $foundOn
46
     */
47
    public function hasBeenCrawled(Url $url, $response, Url $foundOn = null)
48
    {
49
        $statusCode = $response ? $response->getStatusCode() : self::UNRESPONSIVE_HOST;
50
51
        $reason = $response ? $response->getReasonPhrase() : '';
52
53
        $colorTag = $this->getColorTagForStatusCode($statusCode);
54
55
        $timestamp = date('Y-m-d H:i:s');
56
57
        $message = (string) $url;
58
59
        if ($foundOn && $colorTag === 'error') {
60
            $message .= " (found on {$foundOn})";
61
        }
62
63
        $this->output->writeln("<{$colorTag}>[{$timestamp}] {$statusCode} {$reason} - {$message}</{$colorTag}>");
64
65
        $this->crawledUrls[$statusCode][] = $url;
66
    }
67
68
    /**
69
     * Called when the crawl has ended.
70
     */
71
    public function finishedCrawling()
72
    {
73
        $this->output->writeln('');
74
        $this->output->writeln('Crawling summary');
75
        $this->output->writeln('----------------');
76
77
        ksort($this->crawledUrls);
78
79
        foreach ($this->crawledUrls as $statusCode => $urls) {
80
            $colorTag = $this->getColorTagForStatusCode($statusCode);
81
82
            $count = count($urls);
83
84
            if (is_numeric($statusCode)) {
85
                $this->output->writeln("<{$colorTag}>Crawled {$count} url(s) with statuscode {$statusCode}</{$colorTag}>");
86
            }
87
88
            if ($statusCode == static::UNRESPONSIVE_HOST) {
89
                $this->output->writeln("<{$colorTag}>{$count} url(s) did have unresponsive host(s)</{$colorTag}>");
90
            }
91
        }
92
93
        $this->output->writeln('');
94
    }
95
96
    protected function getColorTagForStatusCode(string $code): string
97
    {
98
        if ($this->startsWith($code, '2')) {
99
            return 'info';
100
        }
101
102
        if ($this->startsWith($code, '3')) {
103
            return 'comment';
104
        }
105
106
        return 'error';
107
    }
108
109
    /**
110
     * @param string|null $haystack
111
     * @param string|array $needles
112
     *
113
     * @return bool
114
     */
115
    public function startsWith($haystack, $needles): bool
116
    {
117
        foreach ((array) $needles as $needle) {
118
            if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
119
                return true;
120
            }
121
        }
122
        return false;
123
    }
124
}
125