From 88e53b778a77254dd42aed68d8136b04be465a89 Mon Sep 17 00:00:00 2001 From: Alex Plekhanov Date: Wed, 2 Oct 2019 16:23:06 +0200 Subject: [PATCH 1/5] Refactored the whole code --- .styleci.yml | 3 - composer.json | 5 +- isup | 16 +++-- src/Check.php | 59 ++++++++++++------- .../UnexpectedResponseStatusException.php | 13 ++++ src/IsItUpOrgClient.php | 26 ++++++++ src/Logger.php | 14 +++++ 7 files changed, 105 insertions(+), 31 deletions(-) create mode 100644 src/Exceptions/UnexpectedResponseStatusException.php create mode 100644 src/IsItUpOrgClient.php create mode 100644 src/Logger.php diff --git a/.styleci.yml b/.styleci.yml index ee176b3..247a09c 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1,4 +1 @@ preset: psr2 - -enabled: - - length_ordered_imports diff --git a/composer.json b/composer.json index 3e1d59f..8210c4d 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": ">=5.4.0", + "ext-json": "*" }, "autoload": { "psr-4": { diff --git a/isup b/isup index ce6674c..35f6227 100755 --- a/isup +++ b/isup @@ -1,6 +1,9 @@ #! /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..f391e20 100644 --- a/src/Check.php +++ b/src/Check.php @@ -1,42 +1,45 @@ '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!' + 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'; + /** @var IsItUpOrgClient */ + private $client; + + /** @var Logger */ + private $logger; + + public function __construct(IsItUpOrgClient $client, Logger $logger) + { + $this->client = $client; + $this->logger = $logger; + } public function check($domain) { $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) { - $opts = [ - 'http' => array( - 'method' => "GET", - 'header' => "User-Agent: github.com/alexsoft/isup.php\r\n" - ) - ]; - - $context = stream_context_create($opts); - - $content = json_decode(file_get_contents(sprintf($this->url, $domain), false, $context), true); + $content = $this->client->getContent($domain); if (false === $content) { return static::STATUS_API_ERROR; @@ -44,4 +47,18 @@ protected function getStatus($domain) return $content['status_code']; } + + /** + * @param int $status + * @return string + * @throws UnexpectedResponseStatusException + */ + private function getMessagePattern($status) + { + if (!array_key_exists($status, $this->messages)) { + throw UnexpectedResponseStatusException::withStatus($status); + } + + return $this->messages[$status]; + } } diff --git a/src/Exceptions/UnexpectedResponseStatusException.php b/src/Exceptions/UnexpectedResponseStatusException.php new file mode 100644 index 0000000..8f47780 --- /dev/null +++ b/src/Exceptions/UnexpectedResponseStatusException.php @@ -0,0 +1,13 @@ + [ + '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.php b/src/Logger.php new file mode 100644 index 0000000..f020b21 --- /dev/null +++ b/src/Logger.php @@ -0,0 +1,14 @@ + Date: Wed, 2 Oct 2019 14:24:34 +0000 Subject: [PATCH 2/5] Apply fixes from StyleCI --- src/IsItUpOrgClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IsItUpOrgClient.php b/src/IsItUpOrgClient.php index b61b65b..4176a17 100644 --- a/src/IsItUpOrgClient.php +++ b/src/IsItUpOrgClient.php @@ -4,7 +4,7 @@ final class IsItUpOrgClient { - CONST URL = 'https://isitup.org/%s.json'; + const URL = 'https://isitup.org/%s.json'; public function getContent($domain) { From 811f8c5001a543c339ad5b94db6267c15dc4015c Mon Sep 17 00:00:00 2001 From: Alex Plekhanov Date: Mon, 12 Apr 2021 00:46:26 +0200 Subject: [PATCH 3/5] Refactored the whole code --- .styleci.yml | 2 +- composer.json | 7 ++++- isup | 8 +++--- src/Check.php | 48 ++++++++++++++++----------------- src/Client/ClientInterface.php | 10 +++++++ src/Client/IsItUpOrgClient.php | 35 ++++++++++++++++++++++++ src/Exceptions/ApiException.php | 12 +++++++++ src/IsItUpOrgClient.php | 26 ------------------ src/Logger.php | 14 ---------- src/Logger/EchoLogger.php | 14 ++++++++++ src/Logger/LoggerInterface.php | 10 +++++++ 11 files changed, 117 insertions(+), 69 deletions(-) create mode 100644 src/Client/ClientInterface.php create mode 100644 src/Client/IsItUpOrgClient.php create mode 100644 src/Exceptions/ApiException.php delete mode 100644 src/IsItUpOrgClient.php delete mode 100644 src/Logger.php create mode 100644 src/Logger/EchoLogger.php create mode 100644 src/Logger/LoggerInterface.php diff --git a/.styleci.yml b/.styleci.yml index 247a09c..ac8d606 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1 +1 @@ -preset: psr2 +preset: psr12 diff --git a/composer.json b/composer.json index 8210c4d..31b4e5a 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=5.4.0", + "php": ">=7.4.0", "ext-json": "*" }, "autoload": { @@ -22,6 +22,11 @@ "Alexsoft\\Isup\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Alexsoft\\Isup\\Tests\\": "tests/" + } + }, "bin": [ "isup" ], diff --git a/isup b/isup index 35f6227..0de0478 100755 --- a/isup +++ b/isup @@ -1,9 +1,11 @@ #! /usr/bin/env php write('Please specify site you want to check!'); diff --git a/src/Check.php b/src/Check.php index f391e20..19ba2d1 100644 --- a/src/Check.php +++ b/src/Check.php @@ -1,64 +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!" + self::STATUS_API_ERROR => "Sorry! API is not available right now!", ]; - /** @var IsItUpOrgClient */ - private $client; + private ClientInterface $client; - /** @var Logger */ - private $logger; + private LoggerInterface $logger; - public function __construct(IsItUpOrgClient $client, Logger $logger) + public function __construct(ClientInterface $client, LoggerInterface $logger) { $this->client = $client; $this->logger = $logger; } - public function check($domain) + public function check($domain): void { $status = $this->getStatus($domain); $this->logger->write(sprintf($this->getMessagePattern($status), $domain)); } - private function getStatus($domain) + private function getStatus($domain): int { - $content = $this->client->getContent($domain); + try { + $content = $this->client->getContent($domain); - if (false === $content) { - return static::STATUS_API_ERROR; + return (int)$content['status_code']; + } catch (ApiException $exception) { + return self::STATUS_API_ERROR; } - - return $content['status_code']; } /** - * @param int $status - * @return string * @throws UnexpectedResponseStatusException */ - private function getMessagePattern($status) + private function getMessagePattern(int $status): string { - if (!array_key_exists($status, $this->messages)) { + if (!array_key_exists($status, self::MESSAGES)) { throw UnexpectedResponseStatusException::withStatus($status); } - return $this->messages[$status]; + 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..3a6a133 --- /dev/null +++ b/src/Exceptions/ApiException.php @@ -0,0 +1,12 @@ + [ - '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.php b/src/Logger.php deleted file mode 100644 index f020b21..0000000 --- a/src/Logger.php +++ /dev/null @@ -1,14 +0,0 @@ - Date: Sun, 11 Apr 2021 22:47:43 +0000 Subject: [PATCH 4/5] Apply fixes from StyleCI --- src/Exceptions/ApiException.php | 1 - src/IsItUpOrgClient.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Exceptions/ApiException.php b/src/Exceptions/ApiException.php index 3a6a133..0f85b27 100644 --- a/src/Exceptions/ApiException.php +++ b/src/Exceptions/ApiException.php @@ -8,5 +8,4 @@ class ApiException extends RuntimeException { - } diff --git a/src/IsItUpOrgClient.php b/src/IsItUpOrgClient.php index 4176a17..e0d46d6 100644 --- a/src/IsItUpOrgClient.php +++ b/src/IsItUpOrgClient.php @@ -4,7 +4,7 @@ final class IsItUpOrgClient { - const URL = 'https://isitup.org/%s.json'; + public const URL = 'https://isitup.org/%s.json'; public function getContent($domain) { From 070bd5cbb56539689ad54cbd649af95ae8d84d9c Mon Sep 17 00:00:00 2001 From: Alex Plekhanov Date: Mon, 12 Apr 2021 00:47:57 +0200 Subject: [PATCH 5/5] Changed quotes --- composer.json | 5 ----- src/Check.php | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 31b4e5a..0c1b00b 100644 --- a/composer.json +++ b/composer.json @@ -22,11 +22,6 @@ "Alexsoft\\Isup\\": "src/" } }, - "autoload-dev": { - "psr-4": { - "Alexsoft\\Isup\\Tests\\": "tests/" - } - }, "bin": [ "isup" ], diff --git a/src/Check.php b/src/Check.php index 19ba2d1..c89a610 100644 --- a/src/Check.php +++ b/src/Check.php @@ -19,7 +19,7 @@ class Check 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!", + self::STATUS_API_ERROR => 'Sorry! API is not available right now!', ]; private ClientInterface $client;