From cd5b7842afe1e60126514155d9f51a70a6b60afa Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Sun, 3 Sep 2023 13:24:00 +0300 Subject: [PATCH] Code style fix --- contracts/CounterRepositoryInterface.php | 8 +++-- src/BadgeImageRendererService.php | 45 +++++++++++++++++++----- src/CounterFileRepository.php | 39 +++++++++++--------- src/CounterPdoRepository.php | 18 ++++++---- src/Username.php | 11 ++++-- 5 files changed, 84 insertions(+), 37 deletions(-) diff --git a/contracts/CounterRepositoryInterface.php b/contracts/CounterRepositoryInterface.php index a54c610..dd0841c 100644 --- a/contracts/CounterRepositoryInterface.php +++ b/contracts/CounterRepositoryInterface.php @@ -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; } diff --git a/src/BadgeImageRendererService.php b/src/BadgeImageRendererService.php index 171af39..04a9127 100644 --- a/src/BadgeImageRendererService.php +++ b/src/BadgeImageRendererService.php @@ -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 ''; } - 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, + ); } } diff --git a/src/CounterFileRepository.php b/src/CounterFileRepository.php index f38dc4c..74a6a02 100644 --- a/src/CounterFileRepository.php +++ b/src/CounterFileRepository.php @@ -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); } } diff --git a/src/CounterPdoRepository.php b/src/CounterPdoRepository.php index f77fb92..e4c1a64 100644 --- a/src/CounterPdoRepository.php +++ b/src/CounterPdoRepository.php @@ -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) diff --git a/src/Username.php b/src/Username.php index 0b1637e..4d56bf3 100644 --- a/src/Username.php +++ b/src/Username.php @@ -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); }