Skip to content

Commit eb4ecc3

Browse files
Merge pull request #24 from HajMo/patch-1
Custom exception for "Time Bombs"
2 parents 154b06d + a095fa0 commit eb4ecc3

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace JustSteveKing\Laravel\FeatureFlags\Exceptions;
4+
5+
use Exception;
6+
7+
class ExpiredFeatureException extends Exception
8+
{
9+
public static function create(string $feature)
10+
{
11+
return new static("The Feature {$feature} has expired.");
12+
}
13+
}

src/Models/Feature.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace JustSteveKing\Laravel\FeatureFlags\Models;
66

77
use Carbon\Carbon;
8-
use Exception;
98
use Illuminate\Database\Eloquent\Model;
109
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1110
use Illuminate\Support\Facades\App;
12-
use JustSteveKing\Laravel\FeatureFlags\Models\Concerns\NormaliseName;
11+
use JustSteveKing\Laravel\FeatureFlags\Exceptions\ExpiredFeatureException;
1312
use JustSteveKing\Laravel\FeatureFlags\Models\Builders\FeatureBuilder;
13+
use JustSteveKing\Laravel\FeatureFlags\Models\Concerns\NormaliseName;
1414

1515
class Feature extends Model
1616
{
@@ -30,15 +30,15 @@ class Feature extends Model
3030

3131
public static function booted(): void
3232
{
33-
static::retrieved(function(Feature $feature) {
33+
static::retrieved(function (Feature $feature) {
3434
$timeBombsAreEnabled = config('feature-flags.enable_time_bombs');
35-
$environmentAllowsTimeBombs = ! App::environment(config('feature-flags.time_bomb_environments'));
35+
$environmentAllowsTimeBombs = !App::environment(config('feature-flags.time_bomb_environments'));
3636

37-
if($timeBombsAreEnabled && $environmentAllowsTimeBombs) {
37+
if ($timeBombsAreEnabled && $environmentAllowsTimeBombs) {
3838
$featureHasExpired = Carbon::now()->isAfter($feature->expires_at);
3939

4040
if ($featureHasExpired) {
41-
throw new Exception(sprintf('The Feature has expired - %s', $feature->name));
41+
throw ExpiredFeatureException::create($feature->name);
4242
}
4343
return true;
4444
}

tests/Unit/FeatureTimeBombTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Carbon\Carbon;
6+
use JustSteveKing\Laravel\FeatureFlags\Exceptions\ExpiredFeatureException;
67
use JustSteveKing\Laravel\FeatureFlags\Models\Feature;
78
use Illuminate\Support\Facades\Config;
89

@@ -54,7 +55,7 @@ function tryException() {
5455
]);
5556

5657
Feature::all();
57-
})->throws(Exception::class, 'The Feature has expired - expired feature');
58+
})->throws(ExpiredFeatureException::class, 'The Feature expired feature has expired.');
5859

5960
it('casts the Expiry date to Carbon', function(): void {
6061
Feature::create([
@@ -72,7 +73,7 @@ function tryException() {
7273
]);
7374

7475
Feature::all();
75-
})->throws(Exception::class, 'The Feature has expired - expired feature');
76+
})->throws(ExpiredFeatureException::class, 'The Feature expired feature has expired.');
7677

7778
it('Does not throw an Exception when an expiry date is 1 second in the future', function(): void {
7879
Feature::create([
@@ -101,4 +102,4 @@ function tryException() {
101102
]);
102103

103104
Feature::all();
104-
})->throws(Exception::class, 'The Feature has expired - expired feature');
105+
})->throws(ExpiredFeatureException::class, 'The Feature expired feature has expired.');

0 commit comments

Comments
 (0)