Added additional type safety

This commit is contained in:
Marcin Kurczewski 2014-09-14 17:11:21 +02:00
parent e6073ba7c7
commit 20b3dfc76d
7 changed files with 54 additions and 50 deletions

View file

@ -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))
{

View file

@ -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()

View file

@ -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()

View file

@ -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()

View file

@ -3,26 +3,7 @@ namespace Szurubooru\Helpers;
class EnumHelper
{
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];
}
public static function accessRankFromString($accessRankString)
{
$map =
private static $accessRankMap =
[
'anonymous' => \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS,
'regularUser' => \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER,
@ -31,16 +12,7 @@ class EnumHelper
'administrator' => \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR,
];
$key = trim(strtolower($accessRankString));
if (!isset($map[$key]))
throw new \DomainException('Unrecognized access rank: ' . $accessRankString);
return $map[$key];
}
public static function avatarStyleFromString($avatarStyleString)
{
$map =
private static $avatarStyleMap =
[
'gravatar' => \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR,
'manual' => \Szurubooru\Entities\User::AVATAR_STYLE_MANUAL,
@ -48,10 +20,41 @@ class EnumHelper
'blank' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK,
];
$key = trim(strtolower($avatarStyleString));
if (!isset($map[$key]))
throw new \DomainException('Unrecognized avatar style: ' . $avatarStyleString);
public static function accessRankToString($accessRank)
{
return self::enumToString(self::$accessRankMap, $accessRank);
}
return $map[$key];
public static function accessRankFromString($accessRankString)
{
return self::stringToEnum(self::$accessRankMap, $accessRankString);
}
public static function avatarStyleToString($avatarStyle)
{
return self::enumToString(self::$avatarStyleMap, $avatarStyle);
}
public static function avatarStyleFromString($avatarStyleString)
{
return self::stringToEnum(self::$avatarStyleMap, $avatarStyleString);
}
private static function enumToString($enumMap, $enumValue)
{
$reverseMap = array_flip($enumMap);
if (!isset($reverseMap[$enumValue]))
throw new \RuntimeException('Invalid value!');
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];
}
}

View file

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

View file

@ -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();