From b33e9d4fcf6499e4f216dd242c94b16614ad15e3 Mon Sep 17 00:00:00 2001 From: antonkomarev Date: Sun, 12 Jul 2020 12:28:18 +0300 Subject: [PATCH] Refactor CounterImageRendererService --- public/index.php | 18 ++++++++++++------ src/CounterFileRepository.php | 5 +---- src/CounterImageRendererService.php | 22 +++++++++------------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/public/index.php b/public/index.php index d78b37c..9c35c49 100644 --- a/public/index.php +++ b/public/index.php @@ -15,8 +15,8 @@ use Komarev\GitHubProfileViewsCounter\CounterImageRendererService; use Komarev\GitHubProfileViewsCounter\CounterFileRepository; $basePath = realpath(__DIR__ . '/..'); -$resourcesPath = $basePath . '/resources'; $storagePath = $basePath . '/storage'; +$counterSourceImagePath = $basePath . '/resources/views-count.svg'; require $basePath . '/vendor/autoload.php'; @@ -29,10 +29,16 @@ if ($username === '') { $counterFileName = $username . '-views-count'; } -$counterRepository = new CounterFileRepository($storagePath, $counterFileName); -$counterImageRenderer = new CounterImageRendererService($resourcesPath, 'views-count.svg', $counterRepository); +try { + $counterRepository = new CounterFileRepository($storagePath, $counterFileName); + $counterImageRenderer = new CounterImageRendererService($counterSourceImagePath); -header('Content-Type: image/svg+xml'); -header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); + header('Content-Type: image/svg+xml'); + header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); -echo $counterImageRenderer->getImage(); + $counterRepository->incrementCount(); + + echo $counterImageRenderer->getImageWithCount($counterRepository->getCount()); +} catch (Exception $exception) { + echo $exception->getMessage(); +} diff --git a/src/CounterFileRepository.php b/src/CounterFileRepository.php index 152dafb..3a959ac 100644 --- a/src/CounterFileRepository.php +++ b/src/CounterFileRepository.php @@ -19,10 +19,7 @@ final class CounterFileRepository implements CounterRepositoryInterface { private string $counterPath; - public function __construct( - string $storagePath, - string $counterFileName - ) + public function __construct(string $storagePath, string $counterFileName) { $this->counterPath = $storagePath . '/' . $counterFileName; } diff --git a/src/CounterImageRendererService.php b/src/CounterImageRendererService.php index 153b5c0..b1d6b0c 100644 --- a/src/CounterImageRendererService.php +++ b/src/CounterImageRendererService.php @@ -13,27 +13,23 @@ declare(strict_types=1); namespace Komarev\GitHubProfileViewsCounter; +use InvalidArgumentException; + final class CounterImageRendererService { private string $sourceImagePath; - private CounterFileRepository $counterRepository; - - public function __construct( - string $resourcesPath, - string $sourceImageFileName, - CounterFileRepository $counterRepository - ) + public function __construct(string $sourceImagePath) { - $this->sourceImagePath = $resourcesPath . '/' . $sourceImageFileName; - $this->counterRepository = $counterRepository; + if (!file_exists($sourceImagePath)) { + throw new InvalidArgumentException('Counter source image not found'); + } + + $this->sourceImagePath = $sourceImagePath; } - public function getImage(): string + public function getImageWithCount(int $count): string { - $this->counterRepository->incrementCount(); - $count = $this->counterRepository->getCount(); - $counterImage = file_get_contents($this->sourceImagePath); return $this->replaceImagePlaceholders($counterImage, $count);