5e63faf229
Standalone Composer package (mokoconsulting-tech/enterprise). Source: api/, bin/, lib/ directories from MokoStandards main repo. Autoload paths updated for standalone layout.
132 lines
3.9 KiB
PHP
132 lines
3.9 KiB
PHP
<?php
|
|
/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
|
|
*
|
|
* This file is part of a Moko Consulting project.
|
|
*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
* FILE INFORMATION
|
|
* DEFGROUP: MokoStandards.Enterprise.Platform
|
|
* INGROUP: MokoStandards.Enterprise
|
|
* REPO: https://github.com/mokoconsulting-tech/MokoStandards
|
|
* PATH: /api/lib/Enterprise/PlatformAdapterFactory.php
|
|
* VERSION: 04.06.10
|
|
* BRIEF: Factory for creating platform-specific GitPlatformAdapter instances
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MokoEnterprise;
|
|
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* Factory for creating GitPlatformAdapter instances.
|
|
*
|
|
* Reads GIT_PLATFORM env var (default: 'github') and constructs
|
|
* the appropriate adapter with correct base URL, auth scheme, and token.
|
|
*
|
|
* Usage:
|
|
* ```php
|
|
* $config = Config::load();
|
|
* $adapter = PlatformAdapterFactory::create($config);
|
|
* $repos = $adapter->listOrgRepos('mokoconsulting-tech');
|
|
* ```
|
|
*
|
|
* @package MokoStandards\Enterprise
|
|
* @version 04.06.10
|
|
*/
|
|
class PlatformAdapterFactory
|
|
{
|
|
/**
|
|
* Create a GitPlatformAdapter based on configuration.
|
|
*
|
|
* @param Config $config Configuration instance
|
|
* @param string|null $platformOverride Force a specific platform ('github' or 'gitea')
|
|
* @return GitPlatformAdapter The constructed adapter
|
|
* @throws RuntimeException If the platform is not supported or token is missing
|
|
*/
|
|
public static function create(Config $config, ?string $platformOverride = null): GitPlatformAdapter
|
|
{
|
|
$platform = $platformOverride ?? $config->getString('platform', 'github');
|
|
|
|
return match ($platform) {
|
|
'github' => self::createGitHubAdapter($config),
|
|
'gitea' => self::createGiteaAdapter($config),
|
|
default => throw new RuntimeException("Unsupported git platform: {$platform}. Use 'github' or 'gitea'."),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create a GitHubAdapter with configured ApiClient.
|
|
*
|
|
* @param Config $config Configuration instance
|
|
* @return GitHubAdapter Configured GitHub adapter
|
|
* @throws RuntimeException If GitHub token is not available
|
|
*/
|
|
private static function createGitHubAdapter(Config $config): GitHubAdapter
|
|
{
|
|
$token = $config->getString('github.token', '');
|
|
if (empty($token)) {
|
|
throw new RuntimeException(
|
|
'GitHub token not found. Set GH_TOKEN, GITHUB_TOKEN, or authenticate with `gh auth login`.'
|
|
);
|
|
}
|
|
|
|
$apiClient = new ApiClient(
|
|
baseUrl: 'https://api.github.com',
|
|
authToken: $token,
|
|
maxRequestsPerHour: $config->getInt('github.rate_limit', 5000),
|
|
maxRetries: $config->getInt('github.max_retries', 3),
|
|
authScheme: 'Bearer'
|
|
);
|
|
|
|
return new GitHubAdapter($apiClient);
|
|
}
|
|
|
|
/**
|
|
* Create a GiteaAdapter with configured ApiClient.
|
|
*
|
|
* @param Config $config Configuration instance
|
|
* @return GiteaAdapter Configured Gitea adapter
|
|
* @throws RuntimeException If Gitea token is not available
|
|
*/
|
|
private static function createGiteaAdapter(Config $config): GiteaAdapter
|
|
{
|
|
$token = $config->getString('gitea.token', '');
|
|
if (empty($token)) {
|
|
throw new RuntimeException(
|
|
'Gitea token not found. Set GITEA_TOKEN environment variable.'
|
|
);
|
|
}
|
|
|
|
$giteaUrl = $config->getString('gitea.url', 'https://git.mokoconsulting.tech');
|
|
$apiBaseUrl = rtrim($giteaUrl, '/') . '/api/v1';
|
|
|
|
$apiClient = new ApiClient(
|
|
baseUrl: $apiBaseUrl,
|
|
authToken: $token,
|
|
maxRequestsPerHour: $config->getInt('gitea.rate_limit', 5000),
|
|
maxRetries: $config->getInt('gitea.max_retries', 3),
|
|
authScheme: 'token'
|
|
);
|
|
|
|
return new GiteaAdapter($apiClient, $apiBaseUrl);
|
|
}
|
|
|
|
/**
|
|
* Create adapters for both platforms (useful during migration).
|
|
*
|
|
* @param Config $config Configuration instance
|
|
* @return array{github: GitHubAdapter, gitea: GiteaAdapter} Both adapters
|
|
* @throws RuntimeException If either token is missing
|
|
*/
|
|
public static function createBoth(Config $config): array
|
|
{
|
|
return [
|
|
'github' => self::createGitHubAdapter($config),
|
|
'gitea' => self::createGiteaAdapter($config),
|
|
];
|
|
}
|
|
}
|