Implemented API State endpoint

This commit is contained in:
ErickSkrauch
2019-04-07 02:14:52 +02:00
parent 6cb975d2d3
commit ddf3a07d1f
5 changed files with 156 additions and 0 deletions

View File

@@ -24,6 +24,26 @@ class Api {
$this->client = $client;
}
/**
* @return \Ely\Mojang\Response\ApiStatus[]
*
* @throws GuzzleException
*
* @url https://wiki.vg/Mojang_API#API_Status
*/
public function apiStatus(): array {
$response = $this->getClient()->request('GET', 'https://status.mojang.com/check');
$body = $this->decode($response->getBody()->getContents());
$result = [];
foreach ($body as $serviceDeclaration) {
$serviceName = array_keys($serviceDeclaration)[0];
$result[$serviceName] = new Response\ApiStatus($serviceName, $serviceDeclaration[$serviceName]);
}
return $result;
}
/**
* @param string $username
* @param int $atTime

View File

@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Ely\Mojang\Response;
class ApiStatus {
/**
* @var string
*/
private $serviceName;
/**
* @var string
*/
private $status;
public function __construct(string $serviceName, string $status) {
$this->serviceName = $serviceName;
$this->status = $status;
}
public function getServiceName(): string {
return $this->serviceName;
}
public function getStatus(): string {
return $this->status;
}
/**
* Asserts that current service has no issues.
*
* @return bool
*/
public function isGreen(): bool {
return $this->assertStatusIs('green');
}
/**
* Asserts that current service has some issues.
*
* @return bool
*/
public function isYellow(): bool {
return $this->assertStatusIs('yellow');
}
/**
* Asserts that current service is unavailable.
*
* @return bool
*/
public function isRed(): bool {
return $this->assertStatusIs('red');
}
private function assertStatusIs(string $expectedStatus): bool {
return $this->getStatus() === $expectedStatus;
}
}