From e0147d3ca12977b82521fd6d7254f1912f17973f Mon Sep 17 00:00:00 2001 From: antonkomarev Date: Sun, 12 Jul 2020 23:55:43 +0300 Subject: [PATCH] Store views timestamps --- contracts/CounterRepositoryInterface.php | 4 ++-- public/database-repository.php | 4 ++-- public/file-repository.php | 4 ++-- src/CounterDatabaseRepository.php | 4 ++-- src/CounterFileRepository.php | 24 +++++++++++++++++++++--- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/contracts/CounterRepositoryInterface.php b/contracts/CounterRepositoryInterface.php index 2cecf06..46d4d65 100644 --- a/contracts/CounterRepositoryInterface.php +++ b/contracts/CounterRepositoryInterface.php @@ -15,7 +15,7 @@ namespace Contracts\Komarev\GitHubProfileViewsCounter; interface CounterRepositoryInterface { - public function getCountByUsername(string $username): int; + public function getViewsCountByUsername(string $username): int; - public function incrementCountByUsername(string $username): void; + public function addViewByUsername(string $username): void; } diff --git a/public/database-repository.php b/public/database-repository.php index b829db8..3fd764a 100644 --- a/public/database-repository.php +++ b/public/database-repository.php @@ -61,8 +61,8 @@ try { $dbConnection = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $dbConnectionOptions); $counterRepository = new CounterDatabaseRepository($dbConnection); - $counterRepository->incrementCountByUsername($username); - $count = $counterRepository->getCountByUsername($username); + $counterRepository->addViewByUsername($username); + $count = $counterRepository->getViewsCountByUsername($username); $counterImageRenderer = new CounterImageRendererService($counterBadgePath); $counterImage = $counterImageRenderer->getImageWithCount($count); diff --git a/public/file-repository.php b/public/file-repository.php index 91c06e0..0eee957 100644 --- a/public/file-repository.php +++ b/public/file-repository.php @@ -48,8 +48,8 @@ try { } $counterRepository = new CounterFileRepository($storagePath); - $counterRepository->incrementCountByUsername($username); - $count = $counterRepository->getCountByUsername($username); + $counterRepository->addViewByUsername($username); + $count = $counterRepository->getViewsCountByUsername($username); $counterImageRenderer = new CounterImageRendererService($counterBadgePath); $counterImage = $counterImageRenderer->getImageWithCount($count); diff --git a/src/CounterDatabaseRepository.php b/src/CounterDatabaseRepository.php index d8895c0..c9b68e7 100644 --- a/src/CounterDatabaseRepository.php +++ b/src/CounterDatabaseRepository.php @@ -25,7 +25,7 @@ final class CounterDatabaseRepository implements CounterRepositoryInterface $this->connection = $connection; } - public function getCountByUsername(string $username): int + public function getViewsCountByUsername(string $username): int { $statement = $this->connection->prepare( 'SELECT COUNT(*) @@ -38,7 +38,7 @@ final class CounterDatabaseRepository implements CounterRepositoryInterface return (int) $statement->fetchColumn(0); } - public function incrementCountByUsername(string $username): void + public function addViewByUsername(string $username): void { $statement = $this->connection->prepare( 'INSERT INTO github_profile_views diff --git a/src/CounterFileRepository.php b/src/CounterFileRepository.php index 8e24e3b..3769c84 100644 --- a/src/CounterFileRepository.php +++ b/src/CounterFileRepository.php @@ -15,6 +15,8 @@ namespace Komarev\GitHubProfileViewsCounter; use Contracts\Komarev\GitHubProfileViewsCounter\CounterRepositoryInterface; use Contracts\Komarev\GitHubProfileViewsCounter\InvalidPathException; +use DateTimeImmutable; +use DateTimeZone; final class CounterFileRepository implements CounterRepositoryInterface { @@ -33,18 +35,34 @@ final class CounterFileRepository implements CounterRepositoryInterface $this->storagePath = $storagePath; } - public function getCountByUsername(string $username): int + public function getViewsCountByUsername(string $username): int { $counterFilePath = $this->getCounterFilePath($username); return file_exists($counterFilePath) ? (int) file_get_contents($counterFilePath) : 0; } - public function incrementCountByUsername(string $username): void + public function addViewByUsername(string $username): void + { + file_put_contents( + $this->getViewsFilePath($username), + (new DateTimeImmutable('now', new DateTimeZone('UTC')))->format(DATE_RFC3339_EXTENDED) . PHP_EOL, + FILE_APPEND + ); + + $this->incrementViewsCount($username); + } + + private function incrementViewsCount(string $username): void { $counterFilePath = $this->getCounterFilePath($username); - file_put_contents($counterFilePath, $this->getCountByUsername($username) + 1); + file_put_contents($counterFilePath, $this->getViewsCountByUsername($username) + 1); + } + + private function getViewsFilePath(string $username): string + { + return $this->storagePath . '/' . $username . '-views'; } private function getCounterFilePath(string $username): string