diff --git a/public/database-repository.php b/public/database-repository.php index 347468a..0c7422b 100644 --- a/public/database-repository.php +++ b/public/database-repository.php @@ -13,7 +13,7 @@ declare(strict_types=1); use Dotenv\Dotenv; use Dotenv\Exception\InvalidPathException; -use Komarev\GitHubProfileViewsCounter\CounterImageRendererService; +use Komarev\GitHubProfileViewsCounter\BadgeImageRendererService; use Komarev\GitHubProfileViewsCounter\CounterDatabaseRepository; use Komarev\GitHubProfileViewsCounter\ErrorImageRendererService; @@ -25,6 +25,8 @@ require $basePath . '/vendor/autoload.php'; header('Content-Type: image/svg+xml'); header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); +$badgeImageRenderer = new BadgeImageRendererService(); + try { $dotEnv = Dotenv::createImmutable($basePath); $dotEnv->load(); @@ -38,17 +40,14 @@ try { 'DB_NAME', ]); - $counterBadgePath = $basePath . '/resources/views-count-badge.svg'; - $errorBadgePath = $basePath . '/resources/error-badge.svg'; + $badgeImagePath = $basePath . '/resources/badge.svg'; $style = $_GET['style'] ?? null; $username = $_GET['username'] ?? ''; $username = trim($username); if ($username === '') { - $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); - - echo $errorImageRenderer->getImageWithMessage('Invalid query parameter: username'); + echo $badgeImageRenderer->renderBadgeWithError($badgeImagePath, 'Invalid query parameter: username'); exit; } @@ -71,19 +70,12 @@ try { $count = $counterRepository->getViewsCountByUsername($username); - $counterImageRenderer = new CounterImageRendererService($counterBadgePath); - $counterImage = $counterImageRenderer->getImageWithCount($count); - - echo $counterImage; + echo $badgeImageRenderer->renderBadgeWithCount($badgeImagePath, $count); exit; } catch (InvalidPathException $exception) { - $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); - - echo $errorImageRenderer->getImageWithMessage('Application environment file is missing'); + echo $badgeImageRenderer->renderBadgeWithError($badgeImagePath, 'Application environment file is missing'); exit; } catch (Exception $exception) { - $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); - - echo $errorImageRenderer->getImageWithMessage($exception->getMessage()); + echo $badgeImageRenderer->renderBadgeWithError($badgeImagePath, $exception->getMessage()); exit; } diff --git a/public/file-repository.php b/public/file-repository.php index 9be19ce..ba62a40 100644 --- a/public/file-repository.php +++ b/public/file-repository.php @@ -12,9 +12,8 @@ declare(strict_types=1); use Dotenv\Dotenv; -use Komarev\GitHubProfileViewsCounter\CounterImageRendererService; +use Komarev\GitHubProfileViewsCounter\BadgeImageRendererService; use Komarev\GitHubProfileViewsCounter\CounterFileRepository; -use Komarev\GitHubProfileViewsCounter\ErrorImageRendererService; $basePath = realpath(__DIR__ . '/..'); @@ -24,12 +23,13 @@ require $basePath . '/vendor/autoload.php'; header('Content-Type: image/svg+xml'); header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); +$badgeImageRenderer = new BadgeImageRendererService(); + try { $dotEnv = Dotenv::createImmutable($basePath); $dotEnv->safeLoad(); - $counterBadgePath = $basePath . '/resources/views-count-badge.svg'; - $errorBadgePath = $basePath . '/resources/error-badge.svg'; + $badgeImagePath = $basePath . '/resources/badge.svg'; if (!isset($_ENV['FILE_STORAGE_PATH']) || $_ENV['FILE_STORAGE_PATH'] === null) { $storagePath = $basePath . '/storage'; @@ -42,9 +42,7 @@ try { $username = trim($username); if ($username === '') { - $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); - - echo $errorImageRenderer->getImageWithMessage('Invalid query parameter: username'); + echo $badgeImageRenderer->renderBadgeWithError($badgeImagePath, 'Invalid query parameter: username'); exit; } @@ -58,14 +56,9 @@ try { $count = $counterRepository->getViewsCountByUsername($username); - $counterImageRenderer = new CounterImageRendererService($counterBadgePath); - $counterImage = $counterImageRenderer->getImageWithCount($count); - - echo $counterImage; + echo $badgeImageRenderer->renderBadgeWithCount($badgeImagePath, $count); exit; } catch (Exception $exception) { - $errorImageRenderer = new ErrorImageRendererService($errorBadgePath); - - echo $errorImageRenderer->getImageWithMessage($exception->getMessage()); + echo $badgeImageRenderer->renderBadgeWithError($badgeImagePath, $exception->getMessage()); exit; } diff --git a/resources/error-badge.svg b/resources/badge.svg similarity index 94% rename from resources/error-badge.svg rename to resources/badge.svg index 9127ae9..c154072 100644 --- a/resources/error-badge.svg +++ b/resources/badge.svg @@ -1,7 +1,7 @@ - + diff --git a/resources/views-count-badge.svg b/resources/views-count-badge.svg deleted file mode 100644 index 51c36db..0000000 --- a/resources/views-count-badge.svg +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - %LABEL% - %COUNT% - - diff --git a/src/BadgeImageRendererService.php b/src/BadgeImageRendererService.php new file mode 100644 index 0000000..f74a26a --- /dev/null +++ b/src/BadgeImageRendererService.php @@ -0,0 +1,73 @@ + + * + * 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 BadgeImageRendererService +{ + public function renderBadgeWithCount(string $imagePath, int $count): string + { + $message = (string) $count; + + $messageBackgroundFill = '#007ec6'; + + return $this->renderBadge($imagePath, $message, $messageBackgroundFill); + } + + public function renderBadgeWithError(string $imagePath, string $message): string + { + $messageBackgroundFill = '#e05d44'; + + return $this->renderBadge($imagePath, $message, $messageBackgroundFill); + } + + private function renderBadge(string $imagePath, string $message, string $messageBackgroundFill): string + { + if (!file_exists($imagePath)) { + throw new InvalidPathException('Badge image not found'); + } + + $image = file_get_contents($imagePath); + + + return $this->replaceImagePlaceholders($image, $message, $messageBackgroundFill); + } + + private function replaceImagePlaceholders(string $image, string $message, string $messageBackgroundFill): string + { + $messageLength = strlen($message); + + $minImageWidth = 98; + $minMessageBackgroundWidth = 17; + $minMessageTextLength = 70; + $minMessageTextMarginLeft = 885; + + $label = 'Profile views'; + $imageWidth = $minImageWidth + (8 * $messageLength); + $messageBackgroundWidth = $minMessageBackgroundWidth + (8 * $messageLength); + $messageTextLength = $minMessageTextLength + (80 * $messageLength); + $messageTextMarginLeft = $minMessageTextMarginLeft + (40 * $messageLength); + + $image = str_replace('%IMAGE_WIDTH%', $imageWidth, $image); + $image = str_replace('%LABEL%', $label, $image); + $image = str_replace('%MESSAGE%', $message, $image); + $image = str_replace('%MESSAGE_BACKGROUND_WIDTH%', $messageBackgroundWidth, $image); + $image = str_replace('%MESSAGE_BACKGROUND_FILL%', $messageBackgroundFill, $image); + $image = str_replace('%MESSAGE_TEXT_LENGTH%', $messageTextLength, $image); + $image = str_replace('%MESSAGE_TEXT_MARGIN_LEFT%', $messageTextMarginLeft, $image); + + return $image; + } +} diff --git a/src/CounterImageRendererService.php b/src/CounterImageRendererService.php deleted file mode 100644 index 9c67ac4..0000000 --- a/src/CounterImageRendererService.php +++ /dev/null @@ -1,67 +0,0 @@ - - * - * 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 CounterImageRendererService -{ - private string $counterBadgePath; - - public function __construct(string $counterBadgePath) - { - if (!file_exists($counterBadgePath)) { - throw new InvalidPathException('Counter badge image not found'); - } - - $this->counterBadgePath = $counterBadgePath; - } - - public function getImageWithCount(int $count): string - { - $counterImage = file_get_contents($this->counterBadgePath); - - return $this->replaceImagePlaceholders($counterImage, $count); - } - - private function replaceImagePlaceholders(string $counterImage, int $count): string - { - $countLength = $this->countIntegerLength($count); - - $minImageWidth = 98; - $minCounterBackgroundWidth = 17; - $minCounterTextLength = 70; - $minCounterTextMarginLeft = 885; - - $label = 'Profile views'; - $imageWidth = $minImageWidth + (8 * $countLength); - $counterBackgroundWidth = $minCounterBackgroundWidth + (8 * $countLength); - $counterTextLength = $minCounterTextLength + (80 * $countLength); - $counterTextMarginLeft = $minCounterTextMarginLeft + (40 * $countLength); - - $counterImage = str_replace('%IMAGE_WIDTH%', $imageWidth, $counterImage); - $counterImage = str_replace('%LABEL%', $label, $counterImage); - $counterImage = str_replace('%COUNT%', $count, $counterImage); - $counterImage = str_replace('%COUNTER_BACKGROUND_WIDTH%', $counterBackgroundWidth, $counterImage); - $counterImage = str_replace('%COUNTER_TEXT_LENGTH%', $counterTextLength, $counterImage); - $counterImage = str_replace('%COUNTER_TEXT_MARGIN_LEFT%', $counterTextMarginLeft, $counterImage); - - return $counterImage; - } - - private function countIntegerLength(int $integer): int - { - return strlen((string) $integer); - } -} diff --git a/src/ErrorImageRendererService.php b/src/ErrorImageRendererService.php deleted file mode 100644 index 6a26cf3..0000000 --- a/src/ErrorImageRendererService.php +++ /dev/null @@ -1,62 +0,0 @@ - - * - * 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 = 885; - - $label = 'Profile views'; - $imageWidth = $minImageWidth + (8 * $messageLength); - $counterBackgroundWidth = $minCounterBackgroundWidth + (8 * $messageLength); - $counterTextLength = $minCounterTextLength + (80 * $messageLength); - $counterTextMarginLeft = $minCounterTextMarginLeft + (40 * $messageLength); - - $errorImage = str_replace('%IMAGE_WIDTH%', $imageWidth, $errorImage); - $errorImage = str_replace('%LABEL%', $label, $errorImage); - $errorImage = str_replace('%MESSAGE%', $message, $errorImage); - $errorImage = str_replace('%MESSAGE_BACKGROUND_WIDTH%', $counterBackgroundWidth, $errorImage); - $errorImage = str_replace('%MESSAGE_TEXT_LENGTH%', $counterTextLength, $errorImage); - $errorImage = str_replace('%MESSAGE_TEXT_MARGIN_LEFT%', $counterTextMarginLeft, $errorImage); - - return $errorImage; - } -}