pennant icon indicating copy to clipboard operation
pennant copied to clipboard

[1.x] Add `activeOrFail` method

Open martinbean opened this issue 4 months ago • 2 comments

Adds a new activeOrFail (taking the naming convention from Eloquent models) method to check if a feature is active, throwing an exception if it isn’t.

The motivation for this method is to reduce the need of conditionals when feature-flagging code paths. For example, a queued job that can only be queued if a specific feature is active. Before, I’d have something like this:

public function __construct(User $user)
{
    if (Feature::inactive('feature-v2')) {
        throw new RuntimeException('Feature [feature-v2] is not active.');
    }

    $this->user = $user;
}

With the new method, this can now be simplified to:

  public function __construct(User $user)
  {
-     if (Feature::inactive('feature-v2')) {
-         throw new RuntimeException('Feature [feature-v2] is not active.');
-     }
+     Feature::activeOrFail('feature-v2');

      $this->user = $user;
  }

martinbean avatar Oct 04 '24 11:10 martinbean