Skip to content

Commit

Permalink
Added featureValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Nov 9, 2021
1 parent e484ef4 commit b623bbd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class Feature implements Arrayable
*
* @param string $name
* @param string $id
* @param string|null $stripePriceId
* @param mixed $value
* @return void
*/
public function __construct(
public string $name,
public string $id,
public string|null $stripePriceId = null,
public mixed $value = null,
) {
//
}
Expand Down
16 changes: 15 additions & 1 deletion src/MeteredFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,19 @@

class MeteredFeature extends Feature
{
//
/**
* Initialize the MeteredFeature.
*
* @param string $name
* @param string $id
* @param string $stripePriceId
* @return void
*/
public function __construct(
public string $name,
public string $id,
public string $stripePriceId,
) {
parent::__construct($name, $id, null);
}
}
3 changes: 2 additions & 1 deletion src/Thunder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
* @method static \RenokiCo\Thunder\Plan plan(string $name = null, string $id, $features = [])
* @method static \Laravel\Cashier\SubscriptionBuilder subscription($subscriptionBuilder, $plan)
* @method static void hasFeature(string $id, $subscription)
* @method static mixed featureValue(string $id, $subscription)
* @method static void autoReportUsage(string $id, \Closure $callback)
* @method static \Stripe\UsageRecord reportUsageFor(string $featureId, $subscription, $quantity = 1, $timestamp = null)
* @method static void updateUsageReports($subscription)
* @method static void updateUsageReport(string $featureID, $subscription)
* @method static int|float usage(string $featureId, $subscription)
* @method static \RenokiCo\Thunder\Feature feature(string $name, string $id)
* @method static \RenokiCo\Thunder\Feature feature(string $name, string $id, mixed $value = null)
* @method static \RenokiCo\Thunder\MeteredFeature meteredFeature(string $name, string $id, string $stripePriceId)
* @method static void cleanReportUsageCallbacks()
* @method static void clearPlans()
Expand Down
25 changes: 23 additions & 2 deletions src/ThunderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ public function hasFeature(string $id, Subscription $subscription)
return ! is_null($plan->feature($id));
}

/**
* Retrieved the declared feature value.
*
* @param string $id
* @param \Laravel\Cashier\Subscription $subscription
* @return mixed
*/
public function featureValue(string $id, Subscription $subscription)
{
$plan = $this->getPlanFromSubscription($subscription);

if (! $plan) {
return;
}

$feature = $plan->feature($id);

return $feature ? $feature->value : null;
}

/**
* Add a callback to sync the feature usage automatically.
*
Expand Down Expand Up @@ -178,11 +198,12 @@ public function usage(string $featureId, Subscription $subscription)
*
* @param string $name
* @param string $id
* @param mixed $value
* @return \RenokiCo\Thunder\Feature
*/
public function feature(string $name, string $id)
public function feature(string $name, string $id, mixed $value = null)
{
return new Feature($name, $id, null);
return new Feature($name, $id, $value);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/MeteredBillingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function setUp(): void
parent::setUp();

Thunder::plan('Basic Plan', static::$product->id, [
Thunder::feature('VIP Access', 'vip.access'),
Thunder::feature('VIP Access', 'vip.access', true),
Thunder::feature('Extra Gold', 'extra.gold', 100),
Thunder::meteredFeature('Build Minutes', 'build.minutes', static::$buildMinutesPrice->id),
Thunder::meteredFeature('Seats', 'seats', static::$seatsPrice->id),
]);
Expand Down Expand Up @@ -137,6 +138,7 @@ public function test_metering_billing()
$this->assertEquals(200, Thunder::usage('build.minutes', $user->subscription('main')));

$this->assertTrue(Thunder::hasFeature('vip.access', $user->subscription('main')));
$this->assertFalse(Thunder::hasFeature('extra.gold', $user->subscription('main')));
$this->assertFalse(Thunder::hasFeature('extra.silver', $user->subscription('main')));
$this->assertEquals(100, Thunder::featureValue('extra.gold', $user->subscription('main')));
}
}

0 comments on commit b623bbd

Please sign in to comment.