Skip to content

Commit

Permalink
feat: add support for backed enum plans
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenpaauw committed Sep 30, 2024
1 parent 8703b34 commit e533e25
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/tests export-ignore
/.editorconfig export-ignore
/.php-cs-fixer.dist.php export-ignore
/workbench export-ignore
/art export-ignore
/docs export-ignore
/UPGRADING.md export-ignore
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
},
"autoload-dev": {
"psr-4": {
"Maartenpaauw\\Filament\\Cashier\\Tests\\": "tests"
"Maartenpaauw\\Filament\\Cashier\\Tests\\": "tests",
"Workbench\\App\\": "workbench/app/"
}
},
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/Stripe/BillingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Maartenpaauw\Filament\Cashier\Stripe;

use BackedEnum;
use Closure;
use Filament\Billing\Providers\Contracts\Provider;
use Filament\Pages\Dashboard;
Expand All @@ -13,7 +14,7 @@
final class BillingProvider implements Provider
{
public function __construct(
private readonly string $plan = 'default',
private readonly string|BackedEnum $plan = 'default',
) {}

public function getRouteAction(): string|Closure|array
Expand Down
8 changes: 6 additions & 2 deletions src/Stripe/RedirectIfUserNotSubscribed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Maartenpaauw\Filament\Cashier\Stripe;

use BackedEnum;
use Closure;
use Exception;
use Filament\Pages\Dashboard;
Expand Down Expand Up @@ -59,8 +60,11 @@ public function handle(Request $request, Closure $next, string $plan = 'default'
->redirect();
}

public static function plan(string $plan = 'default'): string
public static function plan(string|BackedEnum $plan = 'default'): string
{
return sprintf('%s:%s', self::class, $plan);
return sprintf('%s:%s', self::class, match (true) {
$plan instanceof BackedEnum => strval($plan->value),
default => $plan,
});
}
}
15 changes: 13 additions & 2 deletions tests/BillingProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
declare(strict_types=1);

use Maartenpaauw\Filament\Cashier\Stripe\BillingProvider;
use Workbench\App\enums\Plan;

it('should use the defined plan', function () {
it('should use the defined plan', function (): void {
expect(new BillingProvider('basic'))
->getSubscribedMiddleware()
->toEqual('Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:basic');
});

it('should use the default plan when no plan provided', function () {
it('should use the default plan when no plan provided', function (): void {
expect(new BillingProvider)
->getSubscribedMiddleware()
->toEqual('Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:default');
});

it('should use the value from string backed enums as plan name', function (BackedEnum $plan, string $expectedMiddleware): void {
expect(new BillingProvider($plan))
->getSubscribedMiddleware()
->toEqual($expectedMiddleware);
})->with([
[Plan::Basic, 'Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:basic'],
[Plan::Advanced, 'Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:advanced'],
[Plan::Premium, 'Maartenpaauw\Filament\Cashier\Stripe\RedirectIfUserNotSubscribed:premium'],
]);
10 changes: 10 additions & 0 deletions workbench/app/enums/Plan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Workbench\App\enums;

enum Plan: string
{
case Basic = 'basic';
case Advanced = 'advanced';
case Premium = 'premium';
}

0 comments on commit e533e25

Please sign in to comment.