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.

BaseReporter::isSuccessOrRedirect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\LinkChecker\Reporters;
4
5
use GuzzleHttp\Exception\RequestException;
6
use Illuminate\Support\Str;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\UriInterface;
9
use Spatie\Crawler\CrawlObserver;
10
11
abstract class BaseReporter extends CrawlObserver
12
{
13
    const UNRESPONSIVE_HOST = 'Host did not respond';
14
15
    /**
16
     * @var array
17
     */
18
    protected $urlsGroupedByStatusCode = [];
19
20
    /**
21
     * Called when the crawler has crawled the given url successfully.
22
     *
23
     * @param \Psr\Http\Message\UriInterface      $url
24
     * @param \Psr\Http\Message\ResponseInterface $response
25
     * @param null|\Psr\Http\Message\UriInterface $foundOnUrl
26
     *
27
     * @return int|string
28
     */
29
    public function crawled(
30
        UriInterface $url,
31
        ResponseInterface $response,
32
        ?UriInterface $foundOnUrl = null
33
    ) {
34
        $statusCode = $response->getStatusCode();
35
36
        if (! $this->isExcludedStatusCode($statusCode)) {
37
            $this->urlsGroupedByStatusCode[$statusCode][] = $url;
38
        }
39
40
        return $statusCode;
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
        $statusCode = $requestException->getCode();
56
57
        if (! $this->isExcludedStatusCode($statusCode)) {
58
            $this->urlsGroupedByStatusCode[$statusCode][] = $url;
59
        }
60
61
        return $statusCode;
62
    }
63
64
    /**
65
     * Determine if the statuscode concerns a successful or
66
     * redirect response.
67
     *
68
     * @param int|string $statusCode
69
     * @return bool
70
     */
71
    protected function isSuccessOrRedirect($statusCode): bool
72
    {
73
        return Str::startsWith($statusCode, ['2', '3']);
74
    }
75
76
    /**
77
     * Determine if the crawler saw some bad urls.
78
     */
79
    protected function crawledBadUrls(): bool
80
    {
81
        return collect($this->urlsGroupedByStatusCode)->keys()->filter(function ($statusCode) {
82
            return ! $this->isSuccessOrRedirect($statusCode);
83
        })->count() > 0;
84
    }
85
86
    /**
87
     * Determine if the statuscode should be excluded'
88
     * from the reporter.
89
     *
90
     * @param int|string $statusCode
91
     *
92
     * @return bool
93
     */
94
    protected function isExcludedStatusCode($statusCode): bool
95
    {
96
        $excludedStatusCodes = config('laravel-link-checker.reporters.exclude_status_codes', []);
97
98
        return in_array($statusCode, $excludedStatusCodes);
99
    }
100
}
101