More type hinting

This commit is contained in:
Alex Cabal 2024-01-08 20:21:42 -06:00
parent cb79832092
commit 1c3640fab6
6 changed files with 56 additions and 29 deletions

View file

@ -72,17 +72,27 @@ class Artwork extends PropertiesBase{
return $this->_UrlName; return $this->_UrlName;
} }
protected function GetSubmitter(): User{ protected function GetSubmitter(): ?User{
if($this->_Submitter === null){ if($this->_Submitter === null){
$this->_Submitter = User::Get($this->SubmitterUserId); try{
$this->_Submitter = User::Get($this->SubmitterUserId);
}
catch(Exceptions\InvalidUserException){
// Return null
}
} }
return $this->_Submitter; return $this->_Submitter;
} }
protected function GetReviewer(): User{ protected function GetReviewer(): ?User{
if($this->_Reviewer === null){ if($this->_Reviewer === null){
$this->_Reviewer = User::Get($this->ReviewerUserId); try{
$this->_Reviewer = User::Get($this->ReviewerUserId);
}
catch(Exceptions\InvalidUserException){
// Return null
}
} }
return $this->_Reviewer; return $this->_Reviewer;

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class InvalidLoginException extends AppException{
protected $message = 'We couldnt validate your login information.';
}

View file

@ -2,5 +2,5 @@
namespace Exceptions; namespace Exceptions;
class InvalidUserException extends AppException{ class InvalidUserException extends AppException{
protected $message = 'We couldnt validate your login information.'; protected $message = 'We couldnt locate that user.';
} }

View file

@ -1,4 +1,6 @@
<? <?
use Exceptions\InvalidLoginException;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Safe\DateTime; use Safe\DateTime;
use function Safe\strtotime; use function Safe\strtotime;
@ -33,33 +35,38 @@ class Session extends PropertiesBase{
// ******* // *******
public function Create(?string $email = null, ?string $password = null): void{ public function Create(?string $email = null, ?string $password = null): void{
$this->User = User::GetIfRegistered($email, $password); try{
$this->UserId = $this->User->UserId; $this->User = User::GetIfRegistered($email, $password);
$this->UserId = $this->User->UserId;
$existingSessions = Db::Query(' $existingSessions = Db::Query('
SELECT SessionId, SELECT SessionId,
Created Created
from Sessions from Sessions
where UserId = ? where UserId = ?
', [$this->UserId]); ', [$this->UserId]);
if(sizeof($existingSessions) > 0){ if(sizeof($existingSessions) > 0){
$this->SessionId = $existingSessions[0]->SessionId; $this->SessionId = $existingSessions[0]->SessionId;
$this->Created = $existingSessions[0]->Created; $this->Created = $existingSessions[0]->Created;
}
else{
$uuid = Uuid::uuid4();
$this->SessionId = $uuid->toString();
$this->Created = new DateTime();
Db::Query('
INSERT into Sessions (UserId, SessionId, Created)
values (?,
?,
?)
', [$this->UserId, $this->SessionId, $this->Created]);
}
self::SetSessionCookie($this->SessionId);
} }
else{ catch(Exceptions\InvalidUserException){
$uuid = Uuid::uuid4(); throw new InvalidLoginException();
$this->SessionId = $uuid->toString();
$this->Created = new DateTime();
Db::Query('
INSERT into Sessions (UserId, SessionId, Created)
values (?,
?,
?)
', [$this->UserId, $this->SessionId, $this->Created]);
} }
self::SetSessionCookie($this->SessionId);
} }
public static function GetLoggedInUser(): ?User{ public static function GetLoggedInUser(): ?User{

View file

@ -114,6 +114,10 @@ class User extends PropertiesBase{
// *********** // ***********
public static function Get(?int $userId): User{ public static function Get(?int $userId): User{
if($userId === null){
throw new Exceptions\InvalidUserException();
}
$result = Db::Query(' $result = Db::Query('
SELECT * SELECT *
from Users from Users

View file

@ -82,7 +82,7 @@ catch(Exceptions\ArtworkNotFoundException){
<? if($isAdminView){ ?> <? if($isAdminView){ ?>
<tr> <tr>
<td>Submitted by</td> <td>Submitted by</td>
<td><? if($artwork->SubmitterUserId === null){ ?>Anonymous<? }else{ ?><a href="mailto:<?= Formatter::ToPlainText($artwork->Submitter->Email) ?>"><? if($artwork->Submitter->Name !== null){ ?> <?= Formatter::ToPlainText($artwork->Submitter->Name) ?><? }else{ ?><?= Formatter::ToPlainText($artwork->Submitter->Email) ?><? } ?></a><? } ?></td> <td><? if($artwork->Submitter === null){ ?>Anonymous<? }else{ ?><a href="mailto:<?= Formatter::ToPlainText($artwork->Submitter->Email) ?>"><? if($artwork->Submitter->Name !== null){ ?> <?= Formatter::ToPlainText($artwork->Submitter->Name) ?><? }else{ ?><?= Formatter::ToPlainText($artwork->Submitter->Email) ?><? } ?></a><? } ?></td>
</tr> </tr>
<? if($artwork->Reviewer !== null){ ?> <? if($artwork->Reviewer !== null){ ?>
<tr> <tr>