diff --git a/src/Facades/InviteCodes.php b/src/Facades/InviteCodes.php index eb5a8d7..465e232 100644 --- a/src/Facades/InviteCodes.php +++ b/src/Facades/InviteCodes.php @@ -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 diff --git a/src/Factory.php b/src/Factory.php index bbd58c3..ee4d6db 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -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 @@ -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 diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 9b8ae77..d7e7b35 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -45,21 +45,6 @@ public function test_macro(): void $this->assertTrue($invite->usageRestrictedToEmail('test@example.com')); } - 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 () { @@ -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); } }