Skip to content

Commit

Permalink
feat: add foggables (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
azuradara authored Jul 12, 2024
1 parent 1110ecc commit 4ccbf5f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Contracts/Foggable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace YouCanShop\Foggle\Contracts;

/**
* This is a helper for FogGen generations allowing you to pass the entire context
* e.g. foggle()->for($store) instead of foggle()->for($store->getId())
*/
interface Foggable
{
public function foggleId(): string;
}
11 changes: 10 additions & 1 deletion src/FogGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace YouCanShop\Foggle;

use InvalidArgumentException;
use YouCanShop\Foggle\Contracts\Foggable;

class FogGen
{
/**
Expand All @@ -16,7 +19,13 @@ public static function inconfig(string $path): callable
if (in_array('*', $config)) {
return true;
}


$context = $context instanceof Foggable ? $context->foggleId() : $context;

if (!is_string($context)) {
throw new InvalidArgumentException('Context must be an instance of Foggable or a string');
}

return in_array($context, config($path, []));
};
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Feature/FoggleTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Workbench\App\Models\User;
use YouCanShop\Foggle\FogGen;

use function Orchestra\Testbench\workbench_path;
Expand Down Expand Up @@ -46,3 +47,10 @@
expect(foggle()->for('1')->active('billing'))->toBeTrue()
->and(foggle()->for('0')->active('billing'))->toBeFalse();
});

it('resolves a foggable using its id', function () {
foggle()->define('billing', FogGen::inconfig('features.billing.sellers'));

expect(foggle()->for(new User('1'))->active('billing'))->toBeTrue()
->and(foggle()->for(new User('0'))->active('billing'))->toBeFalse();
});
20 changes: 20 additions & 0 deletions workbench/app/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Workbench\App\Models;

use YouCanShop\Foggle\Contracts\Foggable;

class User implements Foggable
{
private string $id;

public function __construct(string $id)
{
$this->id = $id;
}

public function foggleId(): string
{
return $this->id;
}
}

0 comments on commit 4ccbf5f

Please sign in to comment.