diff --git a/.styleci.yml b/.styleci.yml index ee176b3..ac8d606 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1,4 +1 @@ -preset: psr2 - -enabled: - - length_ordered_imports +preset: psr12 diff --git a/composer.json b/composer.json index 3e1d59f..0c1b00b 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,12 @@ "authors": [ { "name": "Alex Plekhanov", - "email": "alex@plekhanov.us" + "email": "alex@plekhanov.dev" } ], "require": { - "php": ">=5.4.0" + "php": ">=7.4.0", + "ext-json": "*" }, "autoload": { "psr-4": { diff --git a/isup b/isup index ce6674c..0de0478 100755 --- a/isup +++ b/isup @@ -1,6 +1,11 @@ #! /usr/bin/env php write('Please specify site you want to check!'); exit(1); } -$domains = $argv; -array_shift($domains); +$domains = array_slice($argv, 1); + +$checker = new Check(new IsItUpOrgClient(), $logger); foreach ($domains as $domain) { - (new Alexsoft\Isup\Check)->check($domain); + $checker->check($domain); } -exit(0); \ No newline at end of file +exit(0); diff --git a/src/Check.php b/src/Check.php index f551510..c89a610 100644 --- a/src/Check.php +++ b/src/Check.php @@ -1,47 +1,64 @@ 'It\'s just you. %s is up.', - self::STATUS_WEBSITE_IS_DOWN => 'It\'s not just you! %s looks down from here.', - self::STATUS_NOT_WEBSITE => 'Huh? %s doesn\'t look like a site.', - self::STATUS_API_ERROR => 'Sorry! API is not available right now!' + private const STATUS_WEBSITE_IS_UP = 1; + private const STATUS_WEBSITE_IS_DOWN = 2; + private const STATUS_NOT_WEBSITE = 3; + private const STATUS_API_ERROR = 4; + private const MESSAGES = [ + self::STATUS_WEBSITE_IS_UP => "It's just you. %s is up.", + self::STATUS_WEBSITE_IS_DOWN => "It's not just you! %s looks down from here.", + self::STATUS_NOT_WEBSITE => "Huh? %s doesn't look like a site.", + self::STATUS_API_ERROR => 'Sorry! API is not available right now!', ]; - protected $url = 'http://isitup.org/%s.json'; + private ClientInterface $client; - public function check($domain) + private LoggerInterface $logger; + + public function __construct(ClientInterface $client, LoggerInterface $logger) + { + $this->client = $client; + $this->logger = $logger; + } + + public function check($domain): void { $status = $this->getStatus($domain); - echo sprintf($this->messages[$status], $domain); - echo "\n"; + $this->logger->write(sprintf($this->getMessagePattern($status), $domain)); } - protected function getStatus($domain) + private function getStatus($domain): int { - $opts = [ - 'http' => array( - 'method' => "GET", - 'header' => "User-Agent: github.com/alexsoft/isup.php\r\n" - ) - ]; - - $context = stream_context_create($opts); + try { + $content = $this->client->getContent($domain); - $content = json_decode(file_get_contents(sprintf($this->url, $domain), false, $context), true); + return (int)$content['status_code']; + } catch (ApiException $exception) { + return self::STATUS_API_ERROR; + } + } - if (false === $content) { - return static::STATUS_API_ERROR; + /** + * @throws UnexpectedResponseStatusException + */ + private function getMessagePattern(int $status): string + { + if (!array_key_exists($status, self::MESSAGES)) { + throw UnexpectedResponseStatusException::withStatus($status); } - return $content['status_code']; + return self::MESSAGES[$status]; } } diff --git a/src/Client/ClientInterface.php b/src/Client/ClientInterface.php new file mode 100644 index 0000000..9fd8a31 --- /dev/null +++ b/src/Client/ClientInterface.php @@ -0,0 +1,10 @@ + [ + 'method' => 'GET', + 'header' => "User-Agent: github.com/alexsoft/isup.php\r\n", + ], + ]; + + $contents = file_get_contents( + sprintf(self::URL, $domain), + false, + stream_context_create($opts) + ); + + try { + return json_decode($contents, true, 512, JSON_THROW_ON_ERROR); + } catch (JsonException $exception) { + throw new ApiException($contents, 0, $exception); + } + } +} diff --git a/src/Exceptions/ApiException.php b/src/Exceptions/ApiException.php new file mode 100644 index 0000000..0f85b27 --- /dev/null +++ b/src/Exceptions/ApiException.php @@ -0,0 +1,11 @@ + [ + 'method' => 'GET', + 'header' => "User-Agent: github.com/alexsoft/isup.php\r\n", + ], + ]; + + $contents = file_get_contents( + sprintf(static::URL, $domain), + false, + stream_context_create($opts) + ); + + return json_decode($contents, true); + } +} diff --git a/src/Logger/EchoLogger.php b/src/Logger/EchoLogger.php new file mode 100644 index 0000000..4aa40fe --- /dev/null +++ b/src/Logger/EchoLogger.php @@ -0,0 +1,14 @@ +