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
Pull Request — master (#28)
by Freek
02:04
created

MonitorCollection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkUptime() 0 23 1
A getPromises() 0 19 2
A resetItemKeys() 0 4 1
A getMonitorAtIndex() 0 4 1
1
<?php
2
3
namespace Spatie\UptimeMonitor;
4
5
use Generator;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\RequestException;
8
use GuzzleHttp\Promise\EachPromise;
9
use Illuminate\Support\Collection;
10
use Psr\Http\Message\ResponseInterface;
11
use Spatie\UptimeMonitor\Helpers\ConsoleOutput;
12
use Spatie\UptimeMonitor\Models\Monitor;
13
14
class MonitorCollection extends Collection
15
{
16
    public function checkUptime()
17
    {
18
        $this->resetItemKeys();
19
20
        (new EachPromise($this->getPromises(), [
21
            'concurrency' => config('laravel-uptime-monitor.uptime_check.concurrent_checks'),
22
            'fulfilled' => function (ResponseInterface $response, $index) {
23
                $monitor = $this->getMonitorAtIndex($index);
24
25
                ConsoleOutput::info("Could reach {$monitor->url}");
26
27
                $monitor->uptimeRequestSucceeded($response->getBody());
28
            },
29
30
            'rejected' => function (RequestException $exception, $index) {
31
                $monitor = $this->getMonitorAtIndex($index);
32
33
                ConsoleOutput::error("Could not reach {$monitor->url} error: `{$exception->getMessage()}`");
34
35
                $monitor->uptimeRequestFailed($exception->getMessage());
36
            },
37
        ]))->promise()->wait();
38
    }
39
40
    protected function getPromises() : Generator
41
    {
42
        $client = new Client([
43
            'headers' => [
44
                'User-Agent' => config('laravel-uptime-monitor.uptime_check.user_agent'),
45
            ],
46
        ]);
47
48
        foreach ($this->items as $monitor) {
49
            ConsoleOutput::info("checking {$monitor->url}");
50
            $promise = $client->requestAsync(
51
                $monitor->uptime_check_method,
52
                $monitor->url,
53
                ['connect_timeout' => config('laravel-uptime-monitor.uptime_check.timeout_per_site')]
54
            );
55
56
            yield $promise;
57
        }
58
    }
59
60
    /**
61
     * In order to make use of Guzzle promises we have to make sure the
62
     * keys of the collection are in consecutive order without gaps.
63
     */
64
    protected function resetItemKeys()
65
    {
66
        $this->items = $this->values()->all();
67
    }
68
69
    protected function getMonitorAtIndex(int $index): Monitor
70
    {
71
        return $this->items[$index];
72
    }
73
}
74