-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 [Healthcheck] Introduce a Destination value object
Thus don't repeating ourself in destination validation
- Loading branch information
Showing
8 changed files
with
115 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ubirak package. | ||
* | ||
* (c) Ubirak team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ubirak\Component\Healthcheck; | ||
|
||
final class Destination | ||
{ | ||
private $uri; | ||
|
||
/** | ||
* Informs if a destination is reachable | ||
* | ||
* @param string $destination A destination to join for the health check | ||
* | ||
* @throws InvalidDestination when the destination is not supported. | ||
*/ | ||
public function __construct(string $uri) | ||
{ | ||
$this->uri = filter_var($uri, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED); | ||
|
||
if ($this->uri === false) { | ||
throw InvalidDestination::forUri($uri); | ||
} | ||
} | ||
|
||
public function parse(): array | ||
{ | ||
return parse_url($this->uri); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->uri; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ubirak package. | ||
* | ||
* (c) Ubirak team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ubirak\Component\Healthcheck\Tests\Units; | ||
|
||
use atoum; | ||
|
||
class Destination extends atoum | ||
{ | ||
/** | ||
* @dataProvider invalidUri | ||
*/ | ||
public function test it fails with invalid uri($uri) | ||
{ | ||
$this | ||
->exception(function () use ($uri) { | ||
$this->newTestedInstance($uri); | ||
}) | ||
->hasMessage("Invalid destination: $uri."); | ||
; | ||
} | ||
|
||
protected function invalidUri(): array | ||
{ | ||
return [ | ||
'whithout scheme' => ['localhost'], | ||
'whithout host #1' => ['http://'], | ||
'whithout host #2' => ['https://'], | ||
'whithout host #3' => ['tcp://'], | ||
'empty' => [''], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters