PingService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 17
dl 0
loc 29
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ping() 0 8 2
A pingSingle() 0 5 1
A pingHandler() 0 10 2
1
<?php
2
3
4
namespace Zwarthoorn\Ping\Service;
5
6
7
use Zwarthoorn\Ping\Entity\Result;
8
use Zwarthoorn\Ping\Entity\Url;
9
10
class PingService
11
{
12
    public function ping(Url $urlEntity): array {
13
        $urls = $urlEntity->getUrl();
14
        $result = [];
15
        foreach ($urls as $iValue) {
16
            $result[] = $this->pingSingle($iValue);
17
        }
18
19
        return $result;
20
    }
21
22
    public function pingSingle(string $url): Result{
23
        $result = new Result();
24
        return $result
25
            ->setResult($this->pingHandler($url))
26
            ->setUrl($url);
27
    }
28
29
    private function pingHandler(string $url): bool {
30
31
        $ch = curl_init($url);
32
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_TIMEOUT, 5);
Loading history...
33
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
34
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
35
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_getinfo() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $httpcode = curl_getinfo(/** @scrutinizer ignore-type */ $ch, CURLINFO_HTTP_CODE);
Loading history...
36
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
37
38
        return $httpcode >= 200 && $httpcode < 300;
39
    }
40
}