zwarthoorn /
ping
| 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
|
|||
| 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 |
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: