From 20b3dfc76df23bcee98e46cd6153fad9a18e8a75 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 14 Sep 2014 17:11:21 +0200 Subject: [PATCH] Added additional type safety --- src/Controllers/ViewProxies/UserViewProxy.php | 2 +- src/Entities/Entity.php | 2 +- src/Entities/Token.php | 8 +- src/Entities/User.php | 10 +-- src/Helpers/EnumHelper.php | 77 ++++++++++--------- src/Services/AuthService.php | 1 + tests/Services/PrivilegeServiceTest.php | 4 +- 7 files changed, 54 insertions(+), 50 deletions(-) diff --git a/src/Controllers/ViewProxies/UserViewProxy.php b/src/Controllers/ViewProxies/UserViewProxy.php index d4c992c2..ce3445a3 100644 --- a/src/Controllers/ViewProxies/UserViewProxy.php +++ b/src/Controllers/ViewProxies/UserViewProxy.php @@ -20,7 +20,7 @@ class UserViewProxy extends AbstractViewProxy $result->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankToString($user->getAccessRank()); $result->registrationTime = $user->getRegistrationTime(); $result->lastLoginTime = $user->getLastLoginTime(); - $result->avatarStyle = $user->getAvatarStyle(); + $result->avatarStyle = \Szurubooru\Helpers\EnumHelper::avatarStyleToString($user->getAvatarStyle()); if ($this->privilegeService->isLoggedIn($user)) { diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 0bc2dbf5..7ed1cd00 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -7,7 +7,7 @@ abstract class Entity public function __construct($id = null) { - $this->id = $id; + $this->id = $id === null ? null : intval($id); } public function getId() diff --git a/src/Entities/Token.php b/src/Entities/Token.php index 3074839d..e0859cf5 100644 --- a/src/Entities/Token.php +++ b/src/Entities/Token.php @@ -3,9 +3,9 @@ namespace Szurubooru\Entities; final class Token extends Entity { - const PURPOSE_LOGIN = 0; - const PURPOSE_ACTIVATE = 1; - const PURPOSE_PASSWORD_RESET = 2; + const PURPOSE_LOGIN = 1; + const PURPOSE_ACTIVATE = 2; + const PURPOSE_PASSWORD_RESET = 3; protected $name; protected $purpose; @@ -28,7 +28,7 @@ final class Token extends Entity public function setPurpose($purpose) { - $this->purpose = $purpose; + $this->purpose = intval($purpose); } public function getAdditionalData() diff --git a/src/Entities/User.php b/src/Entities/User.php index f486d9c6..e7b6e08b 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -10,9 +10,9 @@ final class User extends Entity const ACCESS_RANK_MODERATOR = 4; const ACCESS_RANK_ADMINISTRATOR = 5; - const AVATAR_STYLE_GRAVATAR = 'gravatar'; - const AVATAR_STYLE_MANUAL = 'manual'; - const AVATAR_STYLE_BLANK = 'blank'; + const AVATAR_STYLE_GRAVATAR = 1; + const AVATAR_STYLE_MANUAL = 2; + const AVATAR_STYLE_BLANK = 3; protected $name; protected $email; @@ -82,7 +82,7 @@ final class User extends Entity public function setAccessRank($accessRank) { - $this->accessRank = $accessRank; + $this->accessRank = intval($accessRank); } public function getRegistrationTime() @@ -112,7 +112,7 @@ final class User extends Entity public function setAvatarStyle($avatarStyle) { - $this->avatarStyle = $avatarStyle; + $this->avatarStyle = intval($avatarStyle); } public function getBrowsingSettings() diff --git a/src/Helpers/EnumHelper.php b/src/Helpers/EnumHelper.php index bf7c583d..d749f2a2 100644 --- a/src/Helpers/EnumHelper.php +++ b/src/Helpers/EnumHelper.php @@ -3,55 +3,58 @@ namespace Szurubooru\Helpers; class EnumHelper { + private static $accessRankMap = + [ + 'anonymous' => \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS, + 'regularUser' => \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, + 'powerUser' => \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER, + 'moderator' => \Szurubooru\Entities\User::ACCESS_RANK_MODERATOR, + 'administrator' => \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR, + ]; + + private static $avatarStyleMap = + [ + 'gravatar' => \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR, + 'manual' => \Szurubooru\Entities\User::AVATAR_STYLE_MANUAL, + 'none' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK, + 'blank' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK, + ]; + public static function accessRankToString($accessRank) { - $map = - [ - \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS => 'anonymous', - \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER => 'regularUser', - \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER => 'powerUser', - \Szurubooru\Entities\User::ACCESS_RANK_MODERATOR => 'moderator', - \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR => 'administrator', - ]; - - if (!isset($map[$accessRank])) - throw new \DomainException('Invalid access rank!'); - - return $map[$accessRank]; + return self::enumToString(self::$accessRankMap, $accessRank); } public static function accessRankFromString($accessRankString) { - $map = - [ - 'anonymous' => \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS, - 'regularUser' => \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER, - 'powerUser' => \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER, - 'moderator' => \Szurubooru\Entities\User::ACCESS_RANK_MODERATOR, - 'administrator' => \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR, - ]; + return self::stringToEnum(self::$accessRankMap, $accessRankString); + } - $key = trim(strtolower($accessRankString)); - if (!isset($map[$key])) - throw new \DomainException('Unrecognized access rank: ' . $accessRankString); - - return $map[$key]; + public static function avatarStyleToString($avatarStyle) + { + return self::enumToString(self::$avatarStyleMap, $avatarStyle); } public static function avatarStyleFromString($avatarStyleString) { - $map = - [ - 'gravatar' => \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR, - 'manual' => \Szurubooru\Entities\User::AVATAR_STYLE_MANUAL, - 'none' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK, - 'blank' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK, - ]; + return self::stringToEnum(self::$avatarStyleMap, $avatarStyleString); + } - $key = trim(strtolower($avatarStyleString)); - if (!isset($map[$key])) - throw new \DomainException('Unrecognized avatar style: ' . $avatarStyleString); + private static function enumToString($enumMap, $enumValue) + { + $reverseMap = array_flip($enumMap); + if (!isset($reverseMap[$enumValue])) + throw new \RuntimeException('Invalid value!'); - return $map[$key]; + return $reverseMap[$enumValue]; + } + + private static function stringToEnum($enumMap, $enumString) + { + $key = trim(strtolower($enumString)); + if (!isset($enumMap[$key])) + throw new \DomainException('Unrecognized avatar style: ' . $enumString); + + return $enumMap[$key]; } } diff --git a/src/Services/AuthService.php b/src/Services/AuthService.php index 2d21ffd6..21aa1a71 100644 --- a/src/Services/AuthService.php +++ b/src/Services/AuthService.php @@ -75,6 +75,7 @@ class AuthService $user = new \Szurubooru\Entities\User(); $user->setName('Anonymous user'); $user->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS); + $user->setAvatarStyle(\Szurubooru\Entities\User::AVATAR_STYLE_BLANK); return $user; } diff --git a/tests/Services/PrivilegeServiceTest.php b/tests/Services/PrivilegeServiceTest.php index 8b0429da..39ef5482 100644 --- a/tests/Services/PrivilegeServiceTest.php +++ b/tests/Services/PrivilegeServiceTest.php @@ -58,8 +58,8 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testIsLoggedInByUserId() { - $testUser1 = new \Szurubooru\Entities\User('dummy'); - $testUser2 = new \Szurubooru\Entities\User('godzilla'); + $testUser1 = new \Szurubooru\Entities\User(5); + $testUser2 = new \Szurubooru\Entities\User(13); $this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1); $privilegeService = $this->getPrivilegeService();