mirror of
https://github.com/antonkomarev/github-profile-views-counter.git
synced 2026-05-31 12:11:57 -04:00
Refactor CounterRepository
This commit is contained in:
@@ -15,7 +15,7 @@ namespace Contracts\Komarev\GitHubProfileViewsCounter;
|
||||
|
||||
interface CounterRepositoryInterface
|
||||
{
|
||||
public function getCount(): int;
|
||||
public function getCountByUsername(string $username): int;
|
||||
|
||||
public function incrementCount(): void;
|
||||
public function incrementCountByUsername(string $username): void;
|
||||
}
|
||||
|
||||
@@ -23,22 +23,17 @@ require $basePath . '/vendor/autoload.php';
|
||||
$username = $_GET['username'] ?? '';
|
||||
$username = trim($username);
|
||||
|
||||
if ($username === '') {
|
||||
$counterFileName = 'views-count';
|
||||
} else {
|
||||
$counterFileName = $username . '-views-count';
|
||||
}
|
||||
|
||||
try {
|
||||
$counterRepository = new CounterFileRepository($storagePath, $counterFileName);
|
||||
$counterRepository = new CounterFileRepository($storagePath);
|
||||
$counterImageRenderer = new CounterImageRendererService($counterSourceImagePath);
|
||||
|
||||
$counterRepository->incrementCountByUsername($username);
|
||||
$count = $counterRepository->getCountByUsername($username);
|
||||
|
||||
header('Content-Type: image/svg+xml');
|
||||
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
|
||||
|
||||
$counterRepository->incrementCount();
|
||||
|
||||
echo $counterImageRenderer->getImageWithCount($counterRepository->getCount());
|
||||
echo $counterImageRenderer->getImageWithCount($count);
|
||||
} catch (Exception $exception) {
|
||||
echo $exception->getMessage();
|
||||
}
|
||||
|
||||
@@ -17,20 +17,35 @@ use Contracts\Komarev\GitHubProfileViewsCounter\CounterRepositoryInterface;
|
||||
|
||||
final class CounterFileRepository implements CounterRepositoryInterface
|
||||
{
|
||||
private string $counterPath;
|
||||
private string $storagePath;
|
||||
|
||||
public function __construct(string $storagePath, string $counterFileName)
|
||||
public function __construct(string $storagePath)
|
||||
{
|
||||
$this->counterPath = $storagePath . '/' . $counterFileName;
|
||||
$this->storagePath = $storagePath;
|
||||
}
|
||||
|
||||
public function getCount(): int
|
||||
public function getCountByUsername(string $username): int
|
||||
{
|
||||
return file_exists($this->counterPath) ? (int) file_get_contents($this->counterPath) : 0;
|
||||
$counterFilePath = $this->getCounterFilePath($username);
|
||||
|
||||
return file_exists($counterFilePath) ? (int) file_get_contents($counterFilePath) : 0;
|
||||
}
|
||||
|
||||
public function incrementCount(): void
|
||||
public function incrementCountByUsername(string $username): void
|
||||
{
|
||||
file_put_contents($this->counterPath, $this->getCount() + 1);
|
||||
$counterFilePath = $this->getCounterFilePath($username);
|
||||
|
||||
file_put_contents($counterFilePath, $this->getCountByUsername($username) + 1);
|
||||
}
|
||||
|
||||
private function getCounterFilePath(string $username): string
|
||||
{
|
||||
if ($username === '') {
|
||||
$counterFileName = 'views-count';
|
||||
} else {
|
||||
$counterFileName = $username . '-views-count';
|
||||
}
|
||||
|
||||
return $this->storagePath . '/' . $counterFileName;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user