Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the whole code #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
preset: psr2

enabled:
- length_ordered_imports
preset: psr12
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
18 changes: 13 additions & 5 deletions isup
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#! /usr/bin/env php
<?php

declare(strict_types=1);

use Alexsoft\Isup\Check;
use Alexsoft\Isup\Client\IsItUpOrgClient;
use Alexsoft\Isup\Logger\EchoLogger;

if (file_exists(__DIR__.'/../../autoload.php')) {
require __DIR__.'/../../autoload.php';
} else {
require __DIR__.'/vendor/autoload.php';
}

$logger = new EchoLogger();

if ($argc < 2) {
echo "Please specify site you want to check!\n";
$logger->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);
exit(0);
71 changes: 44 additions & 27 deletions src/Check.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,64 @@
<?php

declare(strict_types=1);

namespace Alexsoft\Isup;

use Alexsoft\Isup\Client\ClientInterface;
use Alexsoft\Isup\Exceptions\ApiException;
use Alexsoft\Isup\Exceptions\UnexpectedResponseStatusException;
use Alexsoft\Isup\Logger\LoggerInterface;

class Check
{
const STATUS_WEBSITE_IS_UP = 1;
const STATUS_WEBSITE_IS_DOWN = 2;
const STATUS_NOT_WEBSITE = 3;
const STATUS_API_ERROR = 4;

private $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!'
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];
}
}
10 changes: 10 additions & 0 deletions src/Client/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Alexsoft\Isup\Client;

interface ClientInterface
{
public function getContent(string $domain): array;
}
35 changes: 35 additions & 0 deletions src/Client/IsItUpOrgClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Alexsoft\Isup\Client;

use Alexsoft\Isup\Exceptions\ApiException;
use JsonException;

final class IsItUpOrgClient implements ClientInterface
{
private const URL = 'https://isitup.org/%s.json';

public function getContent(string $domain): array
{
$opts = [
'http' => [
'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);
}
}
}
11 changes: 11 additions & 0 deletions src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Alexsoft\Isup\Exceptions;

use RuntimeException;

class ApiException extends RuntimeException
{
}
13 changes: 13 additions & 0 deletions src/Exceptions/UnexpectedResponseStatusException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Alexsoft\Isup\Exceptions;

use RuntimeException;

class UnexpectedResponseStatusException extends RuntimeException
{
public static function withStatus($status)
{
return new self("Unexpected status [{$status}]");
}
}
26 changes: 26 additions & 0 deletions src/IsItUpOrgClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Alexsoft\Isup;

final class IsItUpOrgClient
{
public const URL = 'https://isitup.org/%s.json';

public function getContent($domain)
{
$opts = [
'http' => [
'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);
}
}
14 changes: 14 additions & 0 deletions src/Logger/EchoLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Alexsoft\Isup\Logger;

final class EchoLogger implements LoggerInterface
{
public function write(string $string, bool $finishWithNewline = true)
{
echo $string;
if ($finishWithNewline) {
echo "\n";
}
}
}
10 changes: 10 additions & 0 deletions src/Logger/LoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Alexsoft\Isup\Logger;

interface LoggerInterface
{
public function write(string $string, bool $finishWithNewline = true);
}