Completed
Push — master ( ed81e8...106907 )
by Paul
01:14
created

AddEndPoint::validateEndpoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Infinitypaul\LaravelUptime\Commands;
4
5
use Illuminate\Console\Command;
6
use Infinitypaul\LaravelUptime\Endpoint;
7
use Infinitypaul\LaravelUptime\Tasks\PingEndPoint;
8
9
class AddEndPoint extends Command
10
{
11
12
    protected $uri;
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'endpoint:add {endpoint :  The Endpoint To Monitor} {--f|frequency=5 : The Frequency To Check This Endpoint, In Minutes} ';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Add An Endpoint To Monitor';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return mixed
41
     */
42
    public function handle()
43
    {
44
        $this->validateEndpoint();
45
46
        $frequency = is_numeric($this->option('frequency')) ? $this->option('frequency') : 5;
47
48
        Endpoint::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on Infinitypaul\LaravelUptime\Endpoint. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
            'uri' => $this->uri,
50
            'frequency' => $frequency,
51
        ]);
52
53
        $this->info("Endpoint {$this->uri} is now being monitored.");
54
55
        $this->call('uptime:status');
56
    }
57
58
    protected function validateEndpoint(){
59
        $this->checkSelf();
60
61
        if (! filter_var($this->uri, FILTER_VALIDATE_URL)) {
62
            $this->error("Endpoint {$this->uri} is not a valid uri.");
63
            die();
64
        }
65
    }
66
67
    protected function checkSelf(){
68
        if($this->argument('endpoint') === 'own') {
69
            $this->uri = config('app.url');
70
            return;
71
        }
72
            $this->uri = $this->argument('endpoint');
73
    }
74
}
75