Skip to content

Commit

Permalink
Fix interface return types. Fix parameter types
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusjunges committed Jan 28, 2024
1 parent 07726f5 commit a3a8b80
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
14 changes: 8 additions & 6 deletions src/Contracts/InviteCodesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Junges\InviteCodes\Contracts;

use Carbon\CarbonInterface;
use Illuminate\Support\Collection;
use Junges\InviteCodes\Exceptions\DuplicateInviteCodeException;
use Junges\InviteCodes\Exceptions\ExpiredInviteCodeException;
Expand All @@ -10,13 +11,12 @@
use Junges\InviteCodes\Exceptions\InviteWithRestrictedUsageException;
use Junges\InviteCodes\Exceptions\SoldOutException;
use Junges\InviteCodes\Exceptions\UserLoggedOutException;
use Junges\InviteCodes\Factory;
use Junges\InviteCodes\Models\Invite;

interface InviteCodesFactory
{
/** If used, no events will be dispatched. */
public function withoutEvents(): Factory;
public function withoutEvents(): InviteCodesFactory;

/**
* @throws ExpiredInviteCodeException
Expand All @@ -28,20 +28,22 @@ public function withoutEvents(): Factory;
public function redeem(string $code): Invite;

/** Create a new invite */
public function create(): Factory;
public function create(): InviteCodesFactory;

/**
* Set the number of allowed redemptions.
*
* @throws InviteMustBeAbleToBeRedeemedException
*/
public function maxUsages(int $usages = 1): Factory;
public function maxUsages(int $usages = 1): InviteCodesFactory;

/** Set the max usages amount to one. */
public function canBeUsedOnce(): Factory;
public function canBeUsedOnce(): InviteCodesFactory;

/** Set the user who can use this invite. */
public function restrictUsageTo(string $email): Factory;
public function restrictUsageTo(string $email): InviteCodesFactory;

public function expiresAt(CarbonInterface|string $date): InviteCodesFactory;

/** Save the created invite.*/
public function save(): Invite;
Expand Down
19 changes: 9 additions & 10 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Factory implements InviteCodesFactory
use Conditionable;

protected int $max_usages;
protected ?string $to = null;
protected ?string $restrictedTo = null;
protected ?CarbonInterface $expires_at;
protected bool $dispatch_events = true;
protected static ?\Closure $createInviteCodeUsing = null;
Expand Down Expand Up @@ -112,19 +112,18 @@ public function canBeUsedOnce(): self
/** @inheritdoc . */
public function restrictUsageTo(string $email): self
{
$this->to = $email;
$this->restrictedTo = $email;

return $this;
}

/** Set the invite expiration date. */
public function expiresAt($date): self
public function expiresAt(CarbonInterface|string $date): self
{
if (is_string($date)) {
$this->expires_at = Carbon::parse($date);
} elseif ($date instanceof Carbon) {
$this->expires_at = $date;
}
$this->expires_at = match(true) {
is_string($date) => Carbon::parse($date),
$date instanceof CarbonInterface => $date,
};

return $this;
}
Expand All @@ -150,7 +149,7 @@ public function save(): Invite

return $model->create([
'code' => $code,
'to' => $this->to,
'to' => $this->restrictedTo,
'uses' => 0,
'expires_at' => $this->expires_at ?? null,
'max_usages' => $this->max_usages ?? null,
Expand All @@ -162,7 +161,7 @@ public function make(int $quantity): Collection
{
$invites = collect();

if (! empty($this->to) && $quantity > 1) {
if (! empty($this->restrictedTo) && $quantity > 1) {
throw DuplicateInviteCodeException::forEmail();
}

Expand Down

0 comments on commit a3a8b80

Please sign in to comment.