diff --git a/public/index.php b/public/index.php index 6e0d681..f766e55 100644 --- a/public/index.php +++ b/public/index.php @@ -14,31 +14,28 @@ declare(strict_types=1); use Dotenv\Exception\InvalidPathException; use Komarev\GitHubProfileViewsCounter\BadgeImageRendererService; use Komarev\GitHubProfileViewsCounter\CounterRepositoryFactory; +use Komarev\GitHubProfileViewsCounter\Request; use Komarev\GitHubProfileViewsCounter\Username; $appBasePath = realpath(__DIR__ . '/..'); require $appBasePath . '/vendor/autoload.php'; -array_walk_recursive($_GET, function (&$input) { - $input = htmlspecialchars($input, ENT_NOQUOTES, 'UTF-8', false); -}); +$request = Request::of($_SERVER, $_GET); -$username = $_GET['username'] ?? ''; -$username = trim($username); +$username = trim($request->username()); if ($username === '') { header('Location: https://github.com/antonkomarev/github-profile-views-counter'); exit; } -$httpUserAgent = $_SERVER['HTTP_USER_AGENT'] ?? ''; -$isGitHubUserAgent = strpos($httpUserAgent, 'github-camo') === 0; +$isGitHubUserAgent = strpos($request->userAgent(), 'github-camo') === 0; -$badgeLabel = $_GET['label'] ?? 'Profile views'; -$badgeMessageBackgroundFill = $_GET['color'] ?? 'blue'; -$badgeStyle = $_GET['style'] ?? 'flat'; -if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic', 'for-the-badge', 'pixel'])) { +$badgeLabel = $request->badgeLabel() ?? 'Profile views'; +$badgeMessageBackgroundFill = $request->badgeColor() ?? 'blue'; +$badgeStyle = $request->badgeStyle() ?? 'flat'; +if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic', 'for-the-badge', 'pixel'], true)) { $badgeStyle = 'flat'; } diff --git a/src/Request.php b/src/Request.php new file mode 100644 index 0000000..8cc70d2 --- /dev/null +++ b/src/Request.php @@ -0,0 +1,83 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Komarev\GitHubProfileViewsCounter; + +final class Request +{ + private string $userAgent; + + private string $username; + + private ?string $badgeLabel; + + private ?string $badgeColor; + + private ?string $badgeStyle; + + public function __construct( + string $userAgent, + string $username, + ?string $badgeLabel, + ?string $badgeColor, + ?string $badgeStyle + ) { + $this->userAgent = $userAgent; + $this->username = $username; + $this->badgeLabel = $badgeLabel; + $this->badgeColor = $badgeColor; + $this->badgeStyle = $badgeStyle; + } + + public static function of( + array $server, + array $get + ): self { + array_walk_recursive($get, function (&$input) { + $input = htmlspecialchars($input, ENT_NOQUOTES, 'UTF-8', false); + }); + + return new self( + $server['HTTP_USER_AGENT'] ?? '', + $get['username'] ?? '', + $get['label'] ?? null, + $get['color'] ?? null, + $get['style'] ?? null, + ); + } + + public function userAgent(): string + { + return $this->userAgent; + } + + public function username(): string + { + return $this->username; + } + + public function badgeLabel(): ?string + { + return $this->badgeLabel; + } + + public function badgeColor(): ?string + { + return $this->badgeColor; + } + + public function badgeStyle(): ?string + { + return $this->badgeStyle; + } +}