Add error badge rendering (#3)

This commit is contained in:
Anton Komarev
2020-07-12 18:48:32 +03:00
committed by GitHub
parent e4f24b04f7
commit c2a6fd9eea
6 changed files with 108 additions and 21 deletions

View File

@@ -0,0 +1,60 @@
<?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 = 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;
}
}