From c2a6fd9eeac09571d378d977999f89983f8f51f5 Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Sun, 12 Jul 2020 18:48:32 +0300 Subject: [PATCH] Add error badge rendering (#3) --- public/file-repository.php | 21 ++++--- public/pdo-repository.php | 25 +++++--- resources/error-badge.svg | 11 ++++ ...{views-count.svg => views-count-badge.svg} | 0 src/CounterImageRendererService.php | 12 ++-- src/ErrorImageRendererService.php | 60 +++++++++++++++++++ 6 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 resources/error-badge.svg rename resources/{views-count.svg => views-count-badge.svg} (100%) create mode 100644 src/ErrorImageRendererService.php diff --git a/public/file-repository.php b/public/file-repository.php index 4029d91..91c06e0 100644 --- a/public/file-repository.php +++ b/public/file-repository.php @@ -14,17 +14,22 @@ declare(strict_types=1); use Dotenv\Dotenv; use Komarev\GitHubProfileViewsCounter\CounterImageRendererService; use Komarev\GitHubProfileViewsCounter\CounterFileRepository; +use Komarev\GitHubProfileViewsCounter\ErrorImageRendererService; $basePath = realpath(__DIR__ . '/..'); // Register The Auto Loader require $basePath . '/vendor/autoload.php'; +header('Content-Type: image/svg+xml'); +header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); + try { $dotEnv = Dotenv::createImmutable($basePath); $dotEnv->safeLoad(); - $counterSourceImagePath = $basePath . '/resources/views-count.svg'; + $counterBadgePath = $basePath . '/resources/views-count-badge.svg'; + $errorBadgePath = $basePath . '/resources/error-badge.svg'; if ($_ENV['FILE_STORAGE_PATH'] === null) { $storagePath = $basePath . '/storage'; @@ -36,20 +41,22 @@ try { $username = trim($username); if ($username === '') { - throw new InvalidArgumentException('Query property `username` is missing'); + $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); + + echo $errorImageRenderer->getImageWithMessage('Invalid query parameter: username'); + exit; } $counterRepository = new CounterFileRepository($storagePath); $counterRepository->incrementCountByUsername($username); $count = $counterRepository->getCountByUsername($username); - $counterImageRenderer = new CounterImageRendererService($counterSourceImagePath); + $counterImageRenderer = new CounterImageRendererService($counterBadgePath); $counterImage = $counterImageRenderer->getImageWithCount($count); - header('Content-Type: image/svg+xml'); - header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); - echo $counterImage; } catch (Exception $exception) { - echo $exception->getMessage(); + $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); + + echo $errorImageRenderer->getImageWithMessage($exception->getMessage()); } diff --git a/public/pdo-repository.php b/public/pdo-repository.php index 50486a3..a683003 100644 --- a/public/pdo-repository.php +++ b/public/pdo-repository.php @@ -15,12 +15,16 @@ use Dotenv\Dotenv; use Dotenv\Exception\InvalidPathException; use Komarev\GitHubProfileViewsCounter\CounterImageRendererService; use Komarev\GitHubProfileViewsCounter\CounterPdoRepository; +use Komarev\GitHubProfileViewsCounter\ErrorImageRendererService; $basePath = realpath(__DIR__ . '/..'); // Register The Auto Loader require $basePath . '/vendor/autoload.php'; +header('Content-Type: image/svg+xml'); +header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); + try { $dotEnv = Dotenv::createImmutable($basePath); $dotEnv->load(); @@ -34,13 +38,17 @@ try { 'DB_NAME', ]); - $counterSourceImagePath = $basePath . '/resources/views-count.svg'; + $counterBadgePath = $basePath . '/resources/views-count-badge.svg'; + $errorBadgePath = $basePath . '/resources/error-badge.svg'; $username = $_GET['username'] ?? ''; $username = trim($username); if ($username === '') { - throw new InvalidArgumentException('Query property `username` is missing'); + $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); + + echo $errorImageRenderer->getImageWithMessage('Invalid query parameter: username'); + exit; } $dsn = sprintf( @@ -56,15 +64,16 @@ try { $counterRepository->incrementCountByUsername($username); $count = $counterRepository->getCountByUsername($username); - $counterImageRenderer = new CounterImageRendererService($counterSourceImagePath); + $counterImageRenderer = new CounterImageRendererService($counterBadgePath); $counterImage = $counterImageRenderer->getImageWithCount($count); - header('Content-Type: image/svg+xml'); - header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); - echo $counterImage; } catch (InvalidPathException $exception) { - echo 'Application environment file is missing'; + $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); + + echo $errorImageRenderer->getImageWithMessage('Application environment file is missing'); } catch (Exception $exception) { - echo $exception->getMessage(); + $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); + + echo $errorImageRenderer->getImageWithMessage($exception->getMessage()); } diff --git a/resources/error-badge.svg b/resources/error-badge.svg new file mode 100644 index 0000000..5bf3f79 --- /dev/null +++ b/resources/error-badge.svg @@ -0,0 +1,11 @@ + + + + + + + static + %MESSAGE% + + diff --git a/resources/views-count.svg b/resources/views-count-badge.svg similarity index 100% rename from resources/views-count.svg rename to resources/views-count-badge.svg diff --git a/src/CounterImageRendererService.php b/src/CounterImageRendererService.php index bbf5ff5..77c9191 100644 --- a/src/CounterImageRendererService.php +++ b/src/CounterImageRendererService.php @@ -17,20 +17,20 @@ use Contracts\Komarev\GitHubProfileViewsCounter\InvalidPathException; final class CounterImageRendererService { - private string $sourceImagePath; + private string $counterBadgePath; - public function __construct(string $sourceImagePath) + public function __construct(string $counterBadgePath) { - if (!file_exists($sourceImagePath)) { - throw new InvalidPathException('Counter source image not found'); + if (!file_exists($counterBadgePath)) { + throw new InvalidPathException('Counter badge image not found'); } - $this->sourceImagePath = $sourceImagePath; + $this->counterBadgePath = $counterBadgePath; } public function getImageWithCount(int $count): string { - $counterImage = file_get_contents($this->sourceImagePath); + $counterImage = file_get_contents($this->counterBadgePath); return $this->replaceImagePlaceholders($counterImage, $count); } diff --git a/src/ErrorImageRendererService.php b/src/ErrorImageRendererService.php new file mode 100644 index 0000000..6dd19c1 --- /dev/null +++ b/src/ErrorImageRendererService.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Komarev\GitHubProfileViewsCounter; + +use Contracts\Komarev\GitHubProfileViewsCounter\InvalidPathException; + +final class ErrorImageRendererService +{ + private string $errorBadgePath; + + public function __construct(string $errorBadgePath) + { + if (!file_exists($errorBadgePath)) { + throw new InvalidPathException('Error badge image not found'); + } + + $this->errorBadgePath = $errorBadgePath; + } + + public function getImageWithMessage(string $message): string + { + $errorImage = file_get_contents($this->errorBadgePath); + + return $this->replaceImagePlaceholders($errorImage, $message); + } + + private function replaceImagePlaceholders(string $errorImage, string $message): string + { + $messageLength = strlen($message); + + $minImageWidth = 98; + $minCounterBackgroundWidth = 17; + $minCounterTextLength = 70; + $minCounterTextMarginLeft = 480; + + $imageWidth = $minImageWidth + (8 * $messageLength); + $counterBackgroundWidth = $minCounterBackgroundWidth + (8 * $messageLength); + $counterTextLength = $minCounterTextLength + (80 * $messageLength); + $counterTextMarginLeft = $minCounterTextMarginLeft + (40 * $messageLength); + + $errorImage = str_replace('%MESSAGE%', $message, $errorImage); + $errorImage = str_replace('%IMAGE_WIDTH%', $imageWidth, $errorImage); + $errorImage = str_replace('%COUNTER_BACKGROUND_WIDTH%', $counterBackgroundWidth, $errorImage); + $errorImage = str_replace('%COUNTER_TEXT_LENGTH%', $counterTextLength, $errorImage); + $errorImage = str_replace('%COUNTER_TEXT_MARGIN_LEFT%', $counterTextMarginLeft, $errorImage); + + return $errorImage; + } +}