Extract Request class (#72)

* Extract Request class

* Strict in_array
This commit is contained in:
Anton Komarev
2023-09-03 13:50:21 +03:00
committed by GitHub
parent cd5b7842af
commit 1bfb082c58
2 changed files with 91 additions and 11 deletions

View File

@@ -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';
}

83
src/Request.php Normal file
View File

@@ -0,0 +1,83 @@
<?php
/*
* This file is part of GitHub Profile Views Counter.
*
* (c) Anton Komarev <anton@komarev.com>
*
* 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;
}
}