Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineOmega committed Dec 7, 2018
0 parents commit d6a7d35
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
.idea
11 changes: 11 additions & 0 deletions composer.json
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/"
}
}
}
11 changes: 11 additions & 0 deletions example.php
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);
59 changes: 59 additions & 0 deletions src/Client.php
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;
}
}
11 changes: 11 additions & 0 deletions src/Enums/GitHubStatus.php
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';
}

0 comments on commit d6a7d35

Please sign in to comment.