diff --git a/composer.json b/composer.json index 7b0d11d..56445e1 100644 --- a/composer.json +++ b/composer.json @@ -25,5 +25,12 @@ "prefer-stable": true, "replace": { "core/php-plug-altum": "self.version" + }, + "extra": { + "laravel": { + "providers": [ + "Core\\Plug\\Altum\\AltumServiceProvider" + ] + } } } diff --git a/src/AltumManager.php b/src/AltumManager.php new file mode 100644 index 0000000..0e16042 --- /dev/null +++ b/src/AltumManager.php @@ -0,0 +1,65 @@ +analytics(); + * $biolinks = $manager->biolinks()->withUserKey($workspaceApiKey); + */ +class AltumManager +{ + /** @var array */ + protected array $config; + + public function __construct(array $config) + { + $this->config = $config; + } + + public function analytics(?string $userApiKey = null): AnalyticsClient + { + return $this->make(AnalyticsClient::class, 'analytics', $userApiKey); + } + + public function biolinks(?string $userApiKey = null): BiolinksClient + { + return $this->make(BiolinksClient::class, 'biolinks', $userApiKey); + } + + public function pusher(?string $userApiKey = null): PusherClient + { + return $this->make(PusherClient::class, 'pusher', $userApiKey); + } + + public function socialproof(?string $userApiKey = null): SocialproofClient + { + return $this->make(SocialproofClient::class, 'socialproof', $userApiKey); + } + + /** + * @template T of AltumClient + * @param class-string $class + * @return T + */ + protected function make(string $class, string $product, ?string $userApiKey): AltumClient + { + $cfg = $this->config[$product] ?? throw new \RuntimeException("AltumCode product '{$product}' not configured."); + + return new $class( + baseUrl: $cfg['url'], + adminApiKey: $cfg['admin_key'], + userApiKey: $userApiKey, + ); + } +} diff --git a/src/AltumServiceProvider.php b/src/AltumServiceProvider.php new file mode 100644 index 0000000..9db0e92 --- /dev/null +++ b/src/AltumServiceProvider.php @@ -0,0 +1,17 @@ +app->singleton(AltumManager::class, function ($app) { + return new AltumManager($app['config']['services.altum'] ?? []); + }); + } +}