Skip to content

Commit

Permalink
Merge duplicated code (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Sep 2, 2023
1 parent 78b6e54 commit e7bc045
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 180 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
PROJECT_NAME=ghpvc

# file | pdo
REPOSITORY=file

DB_DRIVER=pgsql
DB_HOST=localhost
DB_PORT=5432
Expand Down
81 changes: 0 additions & 81 deletions public/file-repository.php

This file was deleted.

130 changes: 128 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,131 @@

declare(strict_types=1);

require 'file-repository.php';
//require 'pdo-repository.php';
use Dotenv\Dotenv;
use Dotenv\Exception\InvalidPathException;
use Komarev\GitHubProfileViewsCounter\BadgeImageRendererService;
use Komarev\GitHubProfileViewsCounter\CounterFileRepository;
use Komarev\GitHubProfileViewsCounter\CounterPdoRepository;
use Komarev\GitHubProfileViewsCounter\Username;

$basePath = realpath(__DIR__ . '/..');

// Register The Auto Loader
require $basePath . '/vendor/autoload.php';

const REPOSITORY_TYPE_FILE = 'file';
const REPOSITORY_TYPE_PDO = 'pdo';

header('Content-Type: image/svg+xml');
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');

$badgeImageRenderer = new BadgeImageRendererService();

array_walk_recursive($_GET, function (&$input) {
$input = htmlspecialchars($input, ENT_NOQUOTES, 'UTF-8', false);
});

$httpUserAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$badgeLabel = $_GET['label'] ?? 'Profile views';
$badgeMessageBackgroundFill = $_GET['color'] ?? 'blue';
$badgeStyle = $_GET['style'] ?? 'flat';
if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic', 'for-the-badge'])) {
$badgeStyle = 'flat';
}
$username = $_GET['username'] ?? '';
$username = trim($username);

if ($username === '') {
header('Location: https://github.com/antonkomarev/github-profile-views-counter');
exit;
}

try {
$dotEnv = Dotenv::createImmutable($basePath);
$dotEnv->load();

$dotEnv->required([
'REPOSITORY',
]);

$repositoryType = $_ENV['REPOSITORY'];

switch ($repositoryType) {
case REPOSITORY_TYPE_PDO:
$dotEnv->required([
'DB_DRIVER',
'DB_HOST',
'DB_PORT',
'DB_USER',
'DB_PASSWORD',
'DB_NAME',
]);

$dsn = sprintf(
'%s:host=%s;port=%d;dbname=%s',
$_ENV['DB_DRIVER'],
$_ENV['DB_HOST'],
$_ENV['DB_PORT'],
$_ENV['DB_NAME']
);
$dbConnection = new PDO(
$dsn,
$_ENV['DB_USER'],
$_ENV['DB_PASSWORD'],
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
],
);

$counterRepository = new CounterPdoRepository($dbConnection);
break;
case REPOSITORY_TYPE_FILE:
if (!isset($_ENV['FILE_STORAGE_PATH']) || $_ENV['FILE_STORAGE_PATH'] === '') {
$storagePath = $basePath . '/storage';
} else {
$storagePath = $_ENV['FILE_STORAGE_PATH'];
}

$counterRepository = new CounterFileRepository($storagePath);
break;
default:
throw new \Exception(
"Unsupported repository `$repositoryType`",
);
}

$username = new Username($username);

if (strpos($httpUserAgent, 'github-camo') === 0) {
$counterRepository->addViewByUsername($username);
}

if ($badgeStyle === 'pixel') {
echo $badgeImageRenderer->renderPixel();
exit;
}

$count = $counterRepository->getViewsCountByUsername($username);

echo $badgeImageRenderer->renderBadgeWithCount(
$badgeLabel,
$count,
$badgeMessageBackgroundFill,
$badgeStyle,
);
exit;
} catch (InvalidPathException $exception) {
echo $badgeImageRenderer->renderBadgeWithError(
$badgeLabel,
'Application environment file is missing',
$badgeStyle,
);
exit;
} catch (Exception $exception) {
echo $badgeImageRenderer->renderBadgeWithError(
$badgeLabel,
$exception->getMessage(),
$badgeStyle,
);
exit;
}
97 changes: 0 additions & 97 deletions public/pdo-repository.php

This file was deleted.

0 comments on commit e7bc045

Please sign in to comment.