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.

MonitorRepository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 15 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 18
loc 120
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnabled() 0 6 1
A getDisabled() 0 10 1
A getForUptimeCheck() 0 6 1
A getForCertificateCheck() 0 8 1
A getHealthy() 0 6 1
A getWithFailingUptimeCheck() 9 9 1
A getWithFailingCertificateCheck() 9 9 1
A getUnhealthy() 0 6 1
A getUnchecked() 0 19 1
A findByUrl() 0 6 1
A query() 0 6 1
A determineMonitorModel() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spatie\UptimeMonitor;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Collection;
7
use Spatie\UptimeMonitor\Exceptions\InvalidConfiguration;
8
use Spatie\UptimeMonitor\Models\Enums\CertificateStatus;
9
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus;
10
use Spatie\UptimeMonitor\Models\Monitor;
11
12
class MonitorRepository
13
{
14
    public static function getEnabled(): Collection
15
    {
16
        $monitors = self::query()->get();
17
18
        return MonitorCollection::make($monitors)->sortByHost();
19
    }
20
21
    public static function getDisabled(): Collection
22
    {
23
        $modelClass = static::determineMonitorModel();
24
25
        $monitors = $modelClass::where('uptime_check_enabled', false)
26
            ->where('certificate_check_enabled', false)
27
            ->get();
28
29
        return MonitorCollection::make($monitors)->sortByHost();
30
    }
31
32
    public static function getForUptimeCheck(): MonitorCollection
33
    {
34
        $monitors = self::query()->get()->filter->shouldCheckUptime();
35
36
        return MonitorCollection::make($monitors)->sortByHost();
37
    }
38
39
    public static function getForCertificateCheck(): Collection
40
    {
41
        $monitors = self::query()
42
            ->where('certificate_check_enabled', true)
43
            ->get();
44
45
        return MonitorCollection::make($monitors)->sortByHost();
46
    }
47
48
    public static function getHealthy(): Collection
49
    {
50
        $monitors = self::query()->get()->filter->isHealthy();
51
52
        return MonitorCollection::make($monitors)->sortByHost();
53
    }
54
55 View Code Duplication
    public static function getWithFailingUptimeCheck(): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $monitors = self::query()
58
            ->where('uptime_check_enabled', true)
59
            ->where('uptime_status', UptimeStatus::DOWN)
60
            ->get();
61
62
        return MonitorCollection::make($monitors)->sortByHost();
63
    }
64
65 View Code Duplication
    public static function getWithFailingCertificateCheck(): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $monitors = self::query()
68
            ->where('certificate_check_enabled', true)
69
            ->where('certificate_status', CertificateStatus::INVALID)
70
            ->get();
71
72
        return MonitorCollection::make($monitors)->sortByHost();
73
    }
74
75
    public static function getUnhealthy(): Collection
76
    {
77
        $monitors = self::query()->get()->reject->isHealthy();
78
79
        return MonitorCollection::make($monitors)->sortByHost();
80
    }
81
82
    public static function getUnchecked(): Collection
83
    {
84
        $modelClass = static::determineMonitorModel();
85
86
        $monitors = $modelClass
87
            ::where(function (Builder $query) {
88
                $query
89
                    ->where('uptime_check_enabled', true)
90
                    ->where('uptime_status', UptimeStatus::NOT_YET_CHECKED);
91
            })
92
            ->orWhere(function (Builder $query) {
93
                $query
94
                    ->where('certificate_check_enabled', true)
95
                    ->where('certificate_status', CertificateStatus::NOT_YET_CHECKED);
96
            })
97
            ->get();
98
99
        return MonitorCollection::make($monitors)->sortByHost();
100
    }
101
102
    /**
103
     * @param string|\Spatie\Url\Url $url
104
     *
105
     * @return \Spatie\UptimeMonitor\Models\Monitor
106
     */
107
    public static function findByUrl($url)
108
    {
109
        $model = static::determineMonitorModel();
110
111
        return $model::where('url', (string) $url)->first();
112
    }
113
114
    protected static function query()
115
    {
116
        $modelClass = static::determineMonitorModel();
117
118
        return $modelClass::enabled();
119
    }
120
121
    protected static function determineMonitorModel(): string
122
    {
123
        $monitorModel = config('uptime-monitor.monitor_model') ?? Monitor::class;
124
125
        if (! is_a($monitorModel, Monitor::class, true)) {
126
            throw InvalidConfiguration::modelIsNotValid($monitorModel);
127
        }
128
129
        return $monitorModel;
130
    }
131
}
132