Skip to content

Commit

Permalink
One class to rule them all (#34)
Browse files Browse the repository at this point in the history
* Common service status response

* New line at the end of the file

* Service => Services
  • Loading branch information
Dyktus authored Feb 27, 2018
1 parent d075600 commit ea15f65
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions src/Jsend/ServiceStatusResponse.php
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']);
}

}

0 comments on commit ea15f65

Please sign in to comment.