Ping   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 23
dl 0
loc 64
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A removeUrl() 0 9 2
A addUrl() 0 8 2
A chekcIfValidUrl() 0 2 1
A __construct() 0 7 3
A ping() 0 6 2
A addMultipleUrls() 0 6 2
A createUrlEntity() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zwarthoorn\Ping;
6
7
use http\Exception\RuntimeException;
8
use Zwarthoorn\Ping\Entity\Url;
9
use Zwarthoorn\Ping\Service\PingService;
10
11
class Ping
12
{
13
    /**
14
     * @var Url $urlEntity
15
     */
16
    private $urlEntity;
17
18
    /**
19
     * @var PingService
20
     */
21
    private $pingService;
22
23
    public function __construct($url = '')
24
    {
25
        $this->createUrlEntity();
26
        if ($url !== '' && filter_var($url, FILTER_VALIDATE_URL)) {
27
            $this->urlEntity->addUrl($url);
28
        }
29
        $this->pingService = new PingService();
30
    }
31
32
    private function createUrlEntity(): Url {
33
        $this->urlEntity = new Url();
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Zwarthoorn\Ping\Entity\Url. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
34
    }
35
36
    public function addUrl(string $url): self {
37
38
       if ($this->chekcIfValidUrl($url) === false) {
39
           throw new \RuntimeException('Not a good url.');
40
       }
41
42
        $this->urlEntity->addUrl($url);
43
        return $this;
44
    }
45
46
    public function addMultipleUrls(array $urlArray): self {
47
        foreach ($urlArray as $url){
48
            $this->addUrl($url);
49
        }
50
51
        return $this;
52
    }
53
54
    public function removeUrl($url): self {
55
        if ($url === null)
56
        {
57
            throw new RuntimeException('There is no url supplied');
58
        }
59
60
        $this->urlEntity->removeUrl($url);
61
62
        return $this;
63
    }
64
65
    private function chekcIfValidUrl(string $url) :bool {
66
        return filter_var($url, FILTER_VALIDATE_URL);
67
    }
68
69
    public function ping(string $url = null) {
70
71
        if ($url !== null){
72
            return $this->pingService->pingSingle($url);
73
        }
74
        return $this->pingService->ping($this->urlEntity);
75
    }
76
77
}
78