Completed
Push — master ( 858c23...0b09f6 )
by Freek
01:40
created

HostRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 34
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 6 6 1
A query() 6 6 1
A determineHostModel() 10 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\ServerMonitor;
4
5
use Spatie\ServerMonitor\Models\Host;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Collection;
8
use Spatie\ServerMonitor\Exceptions\InvalidConfiguration;
9
10 View Code Duplication
class HostRepository
0 ignored issues
show
Duplication introduced by
This class 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...
11
{
12
    public static function all(): Collection
13
    {
14
        $hosts = self::query()->get();
15
16
        return $hosts;
17
    }
18
19
    protected static function query(): Builder
20
    {
21
        $modelClass = static::determineHostModel();
22
23
        return $modelClass::query();
24
    }
25
26
    /**
27
     * Determine the host model class name.
28
     *
29
     * @return string
30
     *
31
     * @throws \Spatie\ServerMonitor\Exceptions\InvalidConfiguration
32
     */
33
    public static function determineHostModel(): string
34
    {
35
        $hostModel = config('server-monitor.host_model') ?? Host::class;
36
37
        if (! is_a($hostModel, Host::class, true)) {
38
            throw InvalidConfiguration::hostModelIsNotValid($hostModel);
39
        }
40
41
        return $hostModel;
42
    }
43
}
44