From 69a108058cd9f6e65f4ad08309ee373e637acb4d Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Sat, 2 Sep 2023 17:56:03 +0300 Subject: [PATCH] Merge duplicated code --- .env.example | 3 + public/file-repository.php | 81 ----------------------- public/index.php | 130 ++++++++++++++++++++++++++++++++++++- public/pdo-repository.php | 97 --------------------------- 4 files changed, 131 insertions(+), 180 deletions(-) delete mode 100644 public/file-repository.php delete mode 100644 public/pdo-repository.php diff --git a/.env.example b/.env.example index 8018a20..1bd96ee 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,8 @@ PROJECT_NAME=ghpvc +# file | pdo +REPOSITORY=file + DB_DRIVER=pgsql DB_HOST=localhost DB_PORT=5432 diff --git a/public/file-repository.php b/public/file-repository.php deleted file mode 100644 index a07ef63..0000000 --- a/public/file-repository.php +++ /dev/null @@ -1,81 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -use Dotenv\Dotenv; -use Komarev\GitHubProfileViewsCounter\BadgeImageRendererService; -use Komarev\GitHubProfileViewsCounter\CounterFileRepository; -use Komarev\GitHubProfileViewsCounter\Username; - -$basePath = realpath(__DIR__ . '/..'); - -// Register The Auto Loader -require $basePath . '/vendor/autoload.php'; - -header('Content-Type: image/svg+xml'); -header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); - -$badgeImageRenderer = new BadgeImageRendererService(); - -try { - $dotEnv = Dotenv::createImmutable($basePath); - $dotEnv->load(); - - $httpUserAgent = $_SERVER['HTTP_USER_AGENT'] ?? ''; - - if (!isset($_ENV['FILE_STORAGE_PATH']) || $_ENV['FILE_STORAGE_PATH'] === '') { - $storagePath = $basePath . '/storage'; - } else { - $storagePath = $_ENV['FILE_STORAGE_PATH']; - } - - array_walk_recursive($_GET, function (&$input) { - $input = htmlspecialchars($input, ENT_NOQUOTES, 'UTF-8', false); - }); - - $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; -// echo $badgeImageRenderer->renderBadgeWithError($badgeLabel, 'Invalid query parameter: username', $badgeStyle); -// exit; - } - - $username = new Username($username); - - $counterRepository = new CounterFileRepository($storagePath); - - 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 (Exception $exception) { - echo $badgeImageRenderer->renderBadgeWithError($badgeLabel, $exception->getMessage(), $badgeStyle); - exit; -} diff --git a/public/index.php b/public/index.php index 8dd4468..9aa262b 100644 --- a/public/index.php +++ b/public/index.php @@ -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; +} diff --git a/public/pdo-repository.php b/public/pdo-repository.php deleted file mode 100644 index 16151c8..0000000 --- a/public/pdo-repository.php +++ /dev/null @@ -1,97 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -use Dotenv\Dotenv; -use Dotenv\Exception\InvalidPathException; -use Komarev\GitHubProfileViewsCounter\BadgeImageRendererService; -use Komarev\GitHubProfileViewsCounter\CounterPdoRepository; -use Komarev\GitHubProfileViewsCounter\Username; - -$basePath = realpath(__DIR__ . '/..'); - -// Register The Auto Loader -require $basePath . '/vendor/autoload.php'; - -header('Content-Type: image/svg+xml'); -header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); - -$badgeImageRenderer = new BadgeImageRendererService(); - -try { - $dotEnv = Dotenv::createImmutable($basePath); - $dotEnv->load(); - - $dotEnv->required([ - 'DB_DRIVER', - 'DB_HOST', - 'DB_PORT', - 'DB_USER', - 'DB_PASSWORD', - 'DB_NAME', - ]); - - $httpUserAgent = $_SERVER['HTTP_USER_AGENT'] ?? ''; - - array_walk_recursive($_GET, function (&$input) { - $input = htmlspecialchars($input, ENT_NOQUOTES, 'UTF-8', false); - }); - - $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; -// echo $badgeImageRenderer->renderBadgeWithError($badgeLabel, 'Invalid query parameter: username', $badgeStyle); -// exit; - } - - $username = new Username($username); - - $dsn = sprintf( - '%s:host=%s;port=%d;dbname=%s', - $_ENV['DB_DRIVER'], $_ENV['DB_HOST'], $_ENV['DB_PORT'], $_ENV['DB_NAME'] - ); - $dbConnectionOptions = [ - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - ]; - $dbConnection = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $dbConnectionOptions); - - $counterRepository = new CounterPdoRepository($dbConnection); - - 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; -}