-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Common service status response * New line at the end of the file * Service => Services
- Loading branch information
Showing
1 changed file
with
121 additions
and
0 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,121 @@ | ||
<?php | ||
|
||
namespace Assertis\Util\Jsend; | ||
|
||
/* | ||
* @author Lukasz Nowak <[email protected]> | ||
*/ | ||
use Assertis\Http\Client\ClientInterface; | ||
use Assertis\Http\Request\Request; | ||
use Symfony\Component\HttpFoundation\Response as Status; | ||
|
||
class ServiceStatusResponse | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $status; | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
/** | ||
* @var int | ||
*/ | ||
private $apiVersion; | ||
/** | ||
* @var array | ||
*/ | ||
private $mysql; | ||
/** | ||
* @var array | ||
*/ | ||
private $services; | ||
/** | ||
* @var array | ||
*/ | ||
private $settings; | ||
/** | ||
* @var array | ||
*/ | ||
private $config; | ||
/** | ||
* @var string | ||
*/ | ||
private $environment; | ||
|
||
/** | ||
* ServiceStatusResponse constructor. | ||
* @param string $status | ||
* @param string $environment | ||
* @param string $name | ||
* @param int $apiVersion | ||
* @param array $mysql | ||
* @param array $services | ||
* @param array $settings | ||
* @param array $config | ||
*/ | ||
public function __construct( | ||
string $status, | ||
string $environment, | ||
string $name, | ||
int $apiVersion, | ||
array $mysql = [], | ||
array $services = [], | ||
array $settings = [], | ||
array $config = [] | ||
) { | ||
$this->status = $status; | ||
$this->environment = $environment; | ||
$this->name = $name; | ||
$this->apiVersion = $apiVersion; | ||
$this->mysql = $mysql; | ||
$this->services = $services; | ||
$this->settings = $settings; | ||
$this->config = $config; | ||
} | ||
|
||
public function getResponseStatus(): int | ||
{ | ||
if (strcmp($this->status, StatusEnum::SUCCESS) === 0) { | ||
return 200; | ||
} | ||
return 503; | ||
} | ||
|
||
public function getResponseBody(): array | ||
{ | ||
return [ | ||
'status' => $this->status, | ||
'data' => [ | ||
'name' => $this->name, | ||
'environment' => $this->environment, | ||
'apiVersion' => $this->apiVersion, | ||
'mysql' => $this->mysql, | ||
'settings' => $this->settings, | ||
'services' => $this->services, | ||
'config' => $this->config | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param ClientInterface $client | ||
* @param string $url | ||
* @return bool | ||
*/ | ||
public static function isServiceOk(ClientInterface $client, string $url = 'status'): bool | ||
{ | ||
$response = $client->send(new Request($url, '', [], Request::GET)); | ||
|
||
if ($response->getStatusCode() !== Status::HTTP_OK) { | ||
return false; | ||
} | ||
|
||
$data = $response->json(); | ||
|
||
return is_array($data) && in_array($data['status'], ['ok', 'success']); | ||
} | ||
|
||
} |