Code style fix

This commit is contained in:
Anton Komarev
2023-09-03 13:24:00 +03:00
parent bd9bad0f0b
commit cd5b7842af
5 changed files with 84 additions and 37 deletions

View File

@@ -17,7 +17,11 @@ use Komarev\GitHubProfileViewsCounter\Username;
interface CounterRepositoryInterface
{
public function getViewsCountByUsername(Username $username): int;
public function getViewsCountByUsername(
Username $username
): int;
public function addViewByUsername(Username $username): void;
public function addViewByUsername(
Username $username
): void;
}

View File

@@ -34,18 +34,35 @@ final class BadgeImageRendererService
]);
}
public function renderBadgeWithCount(string $label, int $count, string $messageBackgroundFill, string $badgeStyle): string
{
public function renderBadgeWithCount(
string $label,
int $count,
string $messageBackgroundFill,
string $badgeStyle
): string {
$message = number_format($count);
return $this->renderBadge($label, $message, $messageBackgroundFill, $badgeStyle);
return $this->renderBadge(
$label,
$message,
$messageBackgroundFill,
$badgeStyle,
);
}
public function renderBadgeWithError(string $label, string $message, string $badgeStyle): string
{
public function renderBadgeWithError(
string $label,
string $message,
string $badgeStyle
): string {
$messageBackgroundFill = 'red';
return $this->renderBadge($label, $message, $messageBackgroundFill, $badgeStyle);
return $this->renderBadge(
$label,
$message,
$messageBackgroundFill,
$badgeStyle,
);
}
public function renderPixel(): string
@@ -53,8 +70,18 @@ final class BadgeImageRendererService
return '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>';
}
private function renderBadge(string $label, string $message, string $messageBackgroundFill, string $badgeStyle): string
{
return (string) $this->poser->generate($label, $message, $messageBackgroundFill, $badgeStyle, Badge::DEFAULT_FORMAT);
private function renderBadge(
string $label,
string $message,
string $messageBackgroundFill,
string $badgeStyle
): string {
return (string)$this->poser->generate(
$label,
$message,
$messageBackgroundFill,
$badgeStyle,
Badge::DEFAULT_FORMAT,
);
}
}

View File

@@ -15,8 +15,6 @@ namespace Komarev\GitHubProfileViewsCounter;
use Contracts\Komarev\GitHubProfileViewsCounter\CounterRepositoryInterface;
use Contracts\Komarev\GitHubProfileViewsCounter\Exceptions\InvalidPathException;
use DateTimeImmutable;
use DateTimeZone;
final class CounterFileRepository implements
CounterRepositoryInterface
@@ -25,8 +23,9 @@ final class CounterFileRepository implements
private string $storagePath;
public function __construct(string $storagePath)
{
public function __construct(
string $storagePath
) {
if (!is_dir($storagePath)) {
throw new InvalidPathException('Counter storage is not a directory');
}
@@ -38,15 +37,19 @@ final class CounterFileRepository implements
$this->storagePath = $storagePath;
}
public function getViewsCountByUsername(Username $username): int
{
public function getViewsCountByUsername(
Username $username
): int {
$counterFilePath = $this->getCounterFilePath($username);
return file_exists($counterFilePath) ? (int) file_get_contents($counterFilePath) : 0;
return file_exists($counterFilePath)
? (int)file_get_contents($counterFilePath)
: 0;
}
public function addViewByUsername(Username $username): void
{
public function addViewByUsername(
Username $username
): void {
file_put_contents(
$this->getViewsFilePath($username),
$this->getCurrentFormattedDateTime() . PHP_EOL,
@@ -60,8 +63,9 @@ final class CounterFileRepository implements
}
}
private function incrementViewsCount(Username $username): void
{
private function incrementViewsCount(
Username $username
): void {
$counterFilePath = $this->getCounterFilePath($username);
/**
@@ -111,18 +115,21 @@ final class CounterFileRepository implements
}
}
private function getViewsFilePath(Username $username): string
{
private function getViewsFilePath(
Username $username
): string {
return $this->storagePath . '/' . $username . '-views';
}
private function getCounterFilePath(Username $username): string
{
private function getCounterFilePath(
Username $username
): string {
return $this->storagePath . '/' . $username . '-views-count';
}
private function getCurrentFormattedDateTime(): string
{
return (new DateTimeImmutable('now', new DateTimeZone('UTC')))->format(DATE_RFC3339_EXTENDED);
return (new \DateTimeImmutable('now', new \DateTimeZone('UTC')))
->format(DATE_RFC3339_EXTENDED);
}
}

View File

@@ -23,14 +23,17 @@ final class CounterPdoRepository implements
private string $tableName;
public function __construct(PDO $connection, string $tableName = null)
{
public function __construct(
PDO $connection,
string $tableName = null
) {
$this->connection = $connection;
$this->tableName = $tableName !== null ? $tableName : 'github_profile_views';
}
public function getViewsCountByUsername(Username $username): int
{
public function getViewsCountByUsername(
Username $username
): int {
$statement = $this->connection->prepare(
'SELECT COUNT(*)
FROM ' . $this->tableName . '
@@ -39,11 +42,12 @@ final class CounterPdoRepository implements
$statement->bindParam('username', $username);
$statement->execute();
return (int) $statement->fetchColumn(0);
return (int)$statement->fetchColumn(0);
}
public function addViewByUsername(Username $username): void
{
public function addViewByUsername(
Username $username
): void {
$statement = $this->connection->prepare(
'INSERT INTO ' . $this->tableName . '
(username, created_at)

View File

@@ -27,11 +27,16 @@ final class Username
private string $username;
public function __construct(string $username)
{
public function __construct(
string $username
) {
Assert::minLength($username, self::LENGTH_MIN);
Assert::maxLength($username, self::LENGTH_MAX);
Assert::regex($username, self::REGEX, 'Username may only contain alphanumeric characters or single hyphens, and cannot begin or end with a hyphen.');
Assert::regex(
$username,
self::REGEX,
'Username may only contain alphanumeric characters or single hyphens, and cannot begin or end with a hyphen.'
);
$this->username = strtolower($username);
}