mirror of
https://github.com/antonkomarev/github-profile-views-counter.git
synced 2026-07-29 08:42:38 -04:00
Remove duplicate badge renderers
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%IMAGE_WIDTH%" height="20">
|
||||
<g shape-rendering="crispEdges">
|
||||
<rect width="79" height="20" fill="#555"/>
|
||||
<rect x="79" width="%MESSAGE_BACKGROUND_WIDTH%" height="20" fill="#e05d44"/>
|
||||
<rect x="79" width="%MESSAGE_BACKGROUND_WIDTH%" height="20" fill="%MESSAGE_BACKGROUND_FILL%"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif"
|
||||
text-rendering="geometricPrecision" font-size="110">
|
||||
|
Before Width: | Height: | Size: 679 B After Width: | Height: | Size: 697 B |
@@ -1,11 +0,0 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%IMAGE_WIDTH%" height="20">
|
||||
<g shape-rendering="crispEdges">
|
||||
<rect width="79" height="20" fill="#555"/>
|
||||
<rect x="79" width="%COUNTER_BACKGROUND_WIDTH%" height="20" fill="#007ec6"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif"
|
||||
text-rendering="geometricPrecision" font-size="110">
|
||||
<text x="405" y="140" transform="scale(.1)" textLength="690">%LABEL%</text>
|
||||
<text x="%COUNTER_TEXT_MARGIN_LEFT%" y="140" transform="scale(.1)" textLength="%COUNTER_TEXT_LENGTH%">%COUNT%</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 677 B |
73
src/BadgeImageRendererService.php
Normal file
73
src/BadgeImageRendererService.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of GitHub Profile Views Counter.
|
||||
*
|
||||
* (c) Anton Komarev <anton@komarev.com>
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of GitHub Profile Views Counter.
|
||||
*
|
||||
* (c) Anton Komarev <anton@komarev.com>
|
||||
*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of GitHub Profile Views Counter.
|
||||
*
|
||||
* (c) Anton Komarev <anton@komarev.com>
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user