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 ( 8a0b57...581156 )
by Freek
9s
created

SyncFile::createOrUpdateMonitor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Spatie\UptimeMonitor\Commands;
4
5
use Spatie\UptimeMonitor\Models\Monitor;
6
use Spatie\UptimeMonitor\Exceptions\CannotSaveMonitor;
7
8
class SyncFile extends BaseCommand
9
{
10
    protected $signature = 'monitor:sync-file
11
                            {path : Path to JSON file with monitors}
12
                            {--delete-missing : Delete monitors from the database if they\'re not found in the monitors file}';
13
14
    protected $description = 'One way sync monitors from JSON file to database';
15
16
    public function handle()
17
    {
18
        $json = file_get_contents($this->argument('path'));
19
20
        $monitorsInFile = collect(json_decode($json, true));
21
22
        $this->validateMonitors($monitorsInFile);
23
24
        $this->createOrUpdateMonitorsFromFile($monitorsInFile);
25
26
        $this->deleteMissingMonitors($monitorsInFile);
27
    }
28
29
    protected function validateMonitors($monitorsInFile)
30
    {
31
        $monitorsInFile->each(function ($monitorAttributes) {
32
            if (! starts_with($monitorAttributes['url'], ['https://', 'http://'])) {
33
                throw new CannotSaveMonitor("URL `{$monitorAttributes['url']}` is invalid (is the URL scheme included?)");
34
            }
35
        });
36
    }
37
38
    protected function createOrUpdateMonitorsFromFile($monitorsInFile)
39
    {
40
        $monitorsInFile
41
            ->each(function ($monitorAttributes) {
42
                $this->createOrUpdateMonitor($monitorAttributes);
43
            });
44
45
        $this->info("Synced {$monitorsInFile->count()} monitor(s) to database");
46
    }
47
48
    protected function createOrUpdateMonitor(array $monitorAttributes)
49
    {
50
        Monitor::firstOrNew([
51
            'url' => $monitorAttributes['url'],
52
        ])
53
            ->fill($monitorAttributes)
54
            ->save();
55
    }
56
57
    protected function deleteMissingMonitors($monitorsInFile)
58
    {
59
        if (! $this->option('delete-missing')) {
60
            return;
61
        }
62
63
        Monitor::all()
64
            ->reject(function (Monitor $monitor) use ($monitorsInFile) {
65
                return $monitorsInFile->contains('url', $monitor->url);
66
            })
67
            ->each(function (Monitor $monitor) {
68
                $path = $this->argument('path');
69
                $this->comment("Deleted monitor for `{$monitor->url}` from database because it was not found in `{$path}`");
70
                $monitor->delete();
71
            });
72
    }
73
}
74