Skip to content

Commit

Permalink
Merge pull request #10 from valeryan/patch-handle-token-exceptions
Browse files Browse the repository at this point in the history
handle invalid token exception internally
  • Loading branch information
junaidnasir authored Aug 15, 2017
2 parents 1ee6f25 + 6fd0a1d commit edc44c1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/InvitationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public function consume();
*/
public function cancel();

/**
* check if a code exist
*
* @return boolean true if code found | false if not
*/
public function isExisting();

/**
* check if invitation is valid
* @return boolean
Expand Down
43 changes: 37 additions & 6 deletions src/LaraInvite.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* Laravel Invitation class
*/
class LaraInvite implements InvitationInterface
{
{

/**
* Email address to invite
* @var string
Expand All @@ -23,6 +24,12 @@ class LaraInvite implements InvitationInterface
*/
private $code = null;

/**
* Status of code existing in DB
* @var bool
*/
private $exist = false;

/**
* integer ID of referral
* @var [type]
Expand Down Expand Up @@ -58,7 +65,12 @@ public function invite($email, $referral, $expires, $beforeSave = null)
public function setCode($code)
{
$this->code = $code;
$this->getModelInstance(false);
try {
$this->getModelInstance(false);
} catch (InvalidTokenException $exception) {
// handle invalid codes
$this->exist = false;
}
return $this;
}

Expand All @@ -70,12 +82,15 @@ public function get()
return $this->instance;
}

/**
/**
* {@inheritdoc}
*/
public function status()
{
return $this->instance->status;
if ($this->isValid()) {
return $this->instance->status;
}
return 'Invalid';
}

/**
Expand Down Expand Up @@ -105,6 +120,14 @@ public function cancel()
}
return false;
}

/**
* {@inheritdoc}
*/
public function isExisting()
{
return $this->exist;
}

/**
* {@inheritdoc}
Expand All @@ -119,6 +142,9 @@ public function isValid()
*/
public function isExpired()
{
if (!$this->isExisting()) {
return true;
}
if (strtotime($this->instance->valid_till) >= time()) {
return false;
}
Expand All @@ -133,6 +159,9 @@ public function isExpired()
*/
public function isPending()
{
if (!$this->isExisting()) {
return false;
}
return ($this->instance->status == 'pending') ? true : false;
}

Expand All @@ -141,7 +170,7 @@ public function isPending()
*/
public function isAllowed($email)
{
return (($this->instance->email == $email) && $this->isValid());
return ($this->isValid() && ($this->instance->email == $email));
}

/**
Expand Down Expand Up @@ -180,13 +209,14 @@ private function save($code, $beforeSave = null)
if (!is_null($beforeSave)) {
if ($beforeSave instanceof Closure) {
$beforeSave->call($this->instance);
} else if (is_callable($beforeSave)) {
} elseif (is_callable($beforeSave)) {
call_user_func($beforeSave, $this->instance);
}
}
$this->instance->save();

$this->code = $code;
$this->exist = true;
return $this;
}

Expand All @@ -205,6 +235,7 @@ private function getModelInstance($allowNew = true)
}
try {
$this->instance = (new $model)->where('code', $this->code)->firstOrFail();
$this->exist = true;
return $this;
} catch (ModelNotFoundException $e) {
throw new InvalidTokenException("Invalid Token {$this->code}", 401);
Expand Down

0 comments on commit edc44c1

Please sign in to comment.