Skip to content

Commit

Permalink
Using Xtend's Registry for promotion rule types
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jul 15, 2024
1 parent 546eadc commit 9d2f4a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 48 deletions.
59 changes: 12 additions & 47 deletions src/Promotion/PromotionRuleTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,34 @@

namespace Vanilo\Promotion;

use Konekt\Extend\Concerns\HasRegistry;
use Konekt\Extend\Concerns\RequiresClassOrInterface;
use Konekt\Extend\Contracts\Registry;
use Vanilo\Promotion\Contracts\PromotionRuleType;
use Vanilo\Promotion\Exceptions\InexistentPromotionRuleException;

final class PromotionRuleTypes
final class PromotionRuleTypes implements Registry
{
private static array $registry = [];
use HasRegistry;
use RequiresClassOrInterface;

private static string $requiredInterface = PromotionRuleType::class;

public static function register(string $id, string $class)
{
if (array_key_exists($id, self::$registry)) {
return;
}

if (!in_array(PromotionRuleType::class, class_implements($class))) {
throw new \InvalidArgumentException(
sprintf(
'The class you are trying to register (%s) as promotion rule, ' .
'must implement the %s interface.',
$class,
PromotionRuleType::class
)
);
}

self::$registry[$id] = $class;
return self::add($id, $class);
}

public static function make(string $id): PromotionRuleType
public static function make(string $id, array $parameters = []): PromotionRuleType
{
$gwClass = self::getClass($id);
$gwClass = self::getClassOf($id);

if (null === $gwClass) {
throw new InexistentPromotionRuleException(
"No rule is registered with the id `$id`."
);
}

return app()->make($gwClass);
}

public static function reset(): void
{
self::$registry = [];
}

public static function getClass(string $id): ?string
{
return self::$registry[$id] ?? null;
}

public static function ids(): array
{
return array_keys(self::$registry);
}

public static function choices(): array
{
$result = [];

foreach (self::$registry as $type => $class) {
$result[$type] = $class::getName();
}

return $result;
return app()->make($gwClass, $parameters);
}
}
2 changes: 1 addition & 1 deletion src/Promotion/Tests/PromotionRuleTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function registered_gateway_instances_can_be_returned()
/** @test */
public function attempting_to_retrieve_an_unregistered_gateway_returns_null()
{
$this->assertNull(PromotionRuleTypes::getClass('randomness'));
$this->assertNull(PromotionRuleTypes::getClassOf('randomness'));
}

/** @test */
Expand Down

0 comments on commit 9d2f4a6

Please sign in to comment.