-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d6a7d35
Showing
5 changed files
with
95 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,3 @@ | ||
vendor/ | ||
composer.lock | ||
.idea |
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,11 @@ | ||
{ | ||
"require": { | ||
"ivopetkov/html5-dom-document-php": "^1.1", | ||
"nesbot/carbon": "^2.7" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"DivineOmega\\GitHubStatusApi\\": "src/" | ||
} | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
use Carbon\Carbon; | ||
use DivineOmega\GitHubStatusApi\Client; | ||
|
||
require_once 'vendor/autoload.php'; | ||
|
||
$client = new Client(); | ||
$status = $client->status(Carbon::parse('2018-12-06 17:00')); | ||
|
||
var_dump($status); |
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,59 @@ | ||
<?php | ||
|
||
namespace DivineOmega\GitHubStatusApi; | ||
|
||
use Carbon\Carbon; | ||
use DivineOmega\GitHubStatusApi\Enums\GitHubStatus; | ||
use IvoPetkov\HTML5DOMDocument; | ||
|
||
class Client | ||
{ | ||
const GITHUB_STATUS_URL = 'https://status.github.com/messages'; | ||
|
||
const CLASS_TO_STATUS_MAP = [ | ||
'good' => GitHubStatus::GOOD, | ||
'major' => GitHubStatus::MAJOR, | ||
'minor' => GitHubStatus::MINOR, | ||
]; | ||
|
||
public function status(Carbon $datetime) | ||
{ | ||
$dom = $this->getDomDocument($datetime); | ||
|
||
$timeElements = $dom->querySelectorAll('time'); | ||
|
||
/** @var HTML5DOMDocument $timeElement*/ | ||
foreach($timeElements as $timeElement) { | ||
|
||
$timeElementDatetime = Carbon::parse($timeElement->getAttribute('datetime')); | ||
|
||
if ($datetime >= $timeElementDatetime) { | ||
|
||
$class = $timeElement->parentNode->attributes->getNamedItem('class')->textContent; | ||
$classParts = explode(' ', $class); | ||
|
||
foreach($classParts as $classPart) { | ||
|
||
if (array_key_exists($classPart, self::CLASS_TO_STATUS_MAP)) { | ||
return self::CLASS_TO_STATUS_MAP[$classPart]; | ||
} | ||
|
||
} | ||
|
||
return GitHubStatus::UNKNOWN; | ||
} | ||
|
||
} | ||
} | ||
|
||
private function getDomDocument(Carbon $datetime) | ||
{ | ||
$url = self::GITHUB_STATUS_URL.'/'.$datetime->format('Y-m-d'); | ||
$html = file_get_contents($url); | ||
|
||
$dom = new HTML5DOMDocument(); | ||
$dom->loadHTML($html); | ||
|
||
return $dom; | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace DivineOmega\GitHubStatusApi\Enums; | ||
|
||
class GitHubStatus | ||
{ | ||
const GOOD = 'good'; | ||
const MINOR = 'minor'; | ||
const MAJOR = 'major'; | ||
const UNKNOWN = 'unknown'; | ||
} |