Skip to content

Commit b623bbd

Browse files
committed
Added featureValue()
1 parent e484ef4 commit b623bbd

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

src/Feature.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class Feature implements Arrayable
1111
*
1212
* @param string $name
1313
* @param string $id
14-
* @param string|null $stripePriceId
14+
* @param mixed $value
1515
* @return void
1616
*/
1717
public function __construct(
1818
public string $name,
1919
public string $id,
20-
public string|null $stripePriceId = null,
20+
public mixed $value = null,
2121
) {
2222
//
2323
}

src/MeteredFeature.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,19 @@
44

55
class MeteredFeature extends Feature
66
{
7-
//
7+
/**
8+
* Initialize the MeteredFeature.
9+
*
10+
* @param string $name
11+
* @param string $id
12+
* @param string $stripePriceId
13+
* @return void
14+
*/
15+
public function __construct(
16+
public string $name,
17+
public string $id,
18+
public string $stripePriceId,
19+
) {
20+
parent::__construct($name, $id, null);
21+
}
822
}

src/Thunder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
* @method static \RenokiCo\Thunder\Plan plan(string $name = null, string $id, $features = [])
99
* @method static \Laravel\Cashier\SubscriptionBuilder subscription($subscriptionBuilder, $plan)
1010
* @method static void hasFeature(string $id, $subscription)
11+
* @method static mixed featureValue(string $id, $subscription)
1112
* @method static void autoReportUsage(string $id, \Closure $callback)
1213
* @method static \Stripe\UsageRecord reportUsageFor(string $featureId, $subscription, $quantity = 1, $timestamp = null)
1314
* @method static void updateUsageReports($subscription)
1415
* @method static void updateUsageReport(string $featureID, $subscription)
1516
* @method static int|float usage(string $featureId, $subscription)
16-
* @method static \RenokiCo\Thunder\Feature feature(string $name, string $id)
17+
* @method static \RenokiCo\Thunder\Feature feature(string $name, string $id, mixed $value = null)
1718
* @method static \RenokiCo\Thunder\MeteredFeature meteredFeature(string $name, string $id, string $stripePriceId)
1819
* @method static void cleanReportUsageCallbacks()
1920
* @method static void clearPlans()

src/ThunderManager.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ public function hasFeature(string $id, Subscription $subscription)
6969
return ! is_null($plan->feature($id));
7070
}
7171

72+
/**
73+
* Retrieved the declared feature value.
74+
*
75+
* @param string $id
76+
* @param \Laravel\Cashier\Subscription $subscription
77+
* @return mixed
78+
*/
79+
public function featureValue(string $id, Subscription $subscription)
80+
{
81+
$plan = $this->getPlanFromSubscription($subscription);
82+
83+
if (! $plan) {
84+
return;
85+
}
86+
87+
$feature = $plan->feature($id);
88+
89+
return $feature ? $feature->value : null;
90+
}
91+
7292
/**
7393
* Add a callback to sync the feature usage automatically.
7494
*
@@ -178,11 +198,12 @@ public function usage(string $featureId, Subscription $subscription)
178198
*
179199
* @param string $name
180200
* @param string $id
201+
* @param mixed $value
181202
* @return \RenokiCo\Thunder\Feature
182203
*/
183-
public function feature(string $name, string $id)
204+
public function feature(string $name, string $id, mixed $value = null)
184205
{
185-
return new Feature($name, $id, null);
206+
return new Feature($name, $id, $value);
186207
}
187208

188209
/**

tests/MeteredBillingTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public function setUp(): void
7878
parent::setUp();
7979

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

139140
$this->assertTrue(Thunder::hasFeature('vip.access', $user->subscription('main')));
140-
$this->assertFalse(Thunder::hasFeature('extra.gold', $user->subscription('main')));
141+
$this->assertFalse(Thunder::hasFeature('extra.silver', $user->subscription('main')));
142+
$this->assertEquals(100, Thunder::featureValue('extra.gold', $user->subscription('main')));
141143
}
142144
}

0 commit comments

Comments
 (0)