Skip to content

Commit

Permalink
Remove unrelated feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusjunges committed Jan 28, 2024
1 parent 2478b21 commit 92a7a47
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
3 changes: 1 addition & 2 deletions src/Facades/InviteCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
* @method static Collection make(int $quantity) Save $quantity invite codes.
* @method static void macro($name, $macro)
* @method static bool hasMacro($name)
* @method static void quietly(callable $callback) Run the given callback without dispatching events
* @method static void createInviteCodeUsing(?callable $callable)
* @method static void createInviteCodeUsing(?callable $callable = null)
* @method static $this canBeUsedOnce()
*/
class InviteCodes extends Facade
Expand Down
21 changes: 5 additions & 16 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,22 @@ class Factory implements InviteCodesFactory
protected int $max_usages;
protected ?string $to = null;
protected ?CarbonInterface $expires_at;
protected static bool $dispatch_events = true;
protected bool $dispatch_events = true;
protected static ?\Closure $createInviteCodeUsing = null;

public static function createInviteCodeUsing(callable $callable): void
public static function createInviteCodeUsing(callable $callable = null): void
{
self::$createInviteCodeUsing = $callable(...);
self::$createInviteCodeUsing = $callable !== null ? $callable(...) : null;
}

/** If used, no events will be dispatched. */
public function withoutEvents(): self
{
self::$dispatch_events = false;
$this->dispatch_events = false;

return $this;
}

public function quietly(callable $callback): void
{
self::$dispatch_events = false;

$closure = $callback(...);

$closure();

self::$dispatch_events = true;
}

/**
* @throws ExpiredInviteCodeException
* @throws InvalidInviteCodeException
Expand Down Expand Up @@ -224,7 +213,7 @@ private function inviteCanBeRedeemed(Invite $invite): bool

private function shouldDispatchEvents(): bool
{
return self::$dispatch_events;
return $this->dispatch_events;
}

private function createInvitationCode(): string
Expand Down
17 changes: 2 additions & 15 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,6 @@ public function test_macro(): void
$this->assertTrue($invite->usageRestrictedToEmail('[email protected]'));
}

public function test_it_can_run_quietly(): void
{
Event::fake();

Invite::query()->create([
'code' => $code = Str::random(),
]);

InviteCodes::quietly(static function () use ($code) {
InviteCodes::redeem($code);
});

Event::assertNotDispatched(InviteRedeemedEvent::class);
}

public function test_it_can_customize_how_invite_code_is_created(): void
{
InviteCodes::createInviteCodeUsing(static function () {
Expand All @@ -69,5 +54,7 @@ public function test_it_can_customize_how_invite_code_is_created(): void
$invite = InviteCodes::create()->save();

$this->assertSame('PREFIX-12345', $invite->code);

InviteCodes::createInviteCodeUsing(null);
}
}

0 comments on commit 92a7a47

Please sign in to comment.