Files
MoneyFlix/crm/Core/Config/ApiConfig.php
T
1sonhador 9d43854277 feat: Initial MoneyFlix project setup
- Created separate MoneyFlix project directory
- Copied Node.js redirector from production server
- Added CRM PHP code (Redirects module, Core classes, Config, Routes, Views)
- Created comprehensive documentation (README_MONEYFLIX.md)
- Added sync script to keep CRM code updated (sync-from-crm.sh)
- Configured .gitignore for Node.js and PHP
- Set up proper project structure for multi-client isolation

This separates MoneyFlix from the main CRM to avoid confusion
between different client systems and provides a dedicated
environment for MoneyFlix-specific development and maintenance.
2025-10-03 12:09:41 +01:00

197 lines
5.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Core\Config;
/**
* Gestão centralizada de configurações de APIs externas
*/
class ApiConfig
{
private static ?self $instance = null;
private array $config = [];
private function __construct()
{
$this->loadConfig();
}
public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Carregar configurações do ambiente
*/
private function loadConfig(): void
{
$this->config = [
'coinmarketcap' => [
'api_key' => $_ENV['COINMARKETCAP_API_KEY'] ?? null,
'base_url' => 'https://pro-api.coinmarketcap.com/v1',
'timeout' => 10,
'monthly_limit' => 10000,
'daily_limit' => 333, // Deixar margem de segurança
],
'google_maps' => [
'api_key' => $_ENV['GOOGLE_MAPS_API_KEY'] ?? null,
'base_url' => 'https://maps.googleapis.com/maps/api',
],
'stripe' => [
'api_key' => $_ENV['STRIPE_API_KEY'] ?? null,
'publishable_key' => $_ENV['STRIPE_PUBLISHABLE_KEY'] ?? null,
'webhook_secret' => $_ENV['STRIPE_WEBHOOK_SECRET'] ?? null,
],
'paypal' => [
'client_id' => $_ENV['PAYPAL_CLIENT_ID'] ?? null,
'client_secret' => $_ENV['PAYPAL_CLIENT_SECRET'] ?? null,
'mode' => $_ENV['PAYPAL_MODE'] ?? 'sandbox', // sandbox ou live
]
];
}
/**
* Obter configuração de uma API específica
*/
public function get(string $api, ?string $key = null)
{
if (!isset($this->config[$api])) {
throw new \InvalidArgumentException("API configuration not found: {$api}");
}
if ($key === null) {
return $this->config[$api];
}
if (!isset($this->config[$api][$key])) {
throw new \InvalidArgumentException("API configuration key not found: {$api}.{$key}");
}
return $this->config[$api][$key];
}
/**
* Verificar se uma API está configurada
*/
public function isConfigured(string $api): bool
{
return isset($this->config[$api]) && !empty($this->config[$api]['api_key']);
}
/**
* Obter todas as APIs disponíveis
*/
public function getAvailableApis(): array
{
return array_keys($this->config);
}
/**
* Obter status de configuração de todas as APIs
*/
public function getConfigurationStatus(): array
{
$status = [];
foreach ($this->config as $api => $config) {
$status[$api] = [
'configured' => $this->isConfigured($api),
'has_api_key' => !empty($config['api_key']),
'config_keys' => array_keys($config)
];
}
return $status;
}
/**
* Validar configuração de uma API
*/
public function validateApiConfig(string $api): array
{
$errors = [];
if (!isset($this->config[$api])) {
$errors[] = "API '{$api}' not found in configuration";
return $errors;
}
$config = $this->config[$api];
switch ($api) {
case 'coinmarketcap':
if (empty($config['api_key'])) {
$errors[] = "CoinMarketCap API key is required. Set COINMARKETCAP_API_KEY in config.env";
}
if (strlen($config['api_key'] ?? '') < 30) {
$errors[] = "CoinMarketCap API key appears to be invalid (too short)";
}
break;
case 'google_maps':
if (empty($config['api_key'])) {
$errors[] = "Google Maps API key is required. Set GOOGLE_MAPS_API_KEY in config.env";
}
break;
case 'stripe':
if (empty($config['api_key'])) {
$errors[] = "Stripe API key is required. Set STRIPE_API_KEY in config.env";
}
if (empty($config['publishable_key'])) {
$errors[] = "Stripe publishable key is required. Set STRIPE_PUBLISHABLE_KEY in config.env";
}
break;
case 'paypal':
if (empty($config['client_id'])) {
$errors[] = "PayPal client ID is required. Set PAYPAL_CLIENT_ID in config.env";
}
if (empty($config['client_secret'])) {
$errors[] = "PayPal client secret is required. Set PAYPAL_CLIENT_SECRET in config.env";
}
if (!in_array($config['mode'], ['sandbox', 'live'])) {
$errors[] = "PayPal mode must be 'sandbox' or 'live'";
}
break;
}
return $errors;
}
/**
* Obter headers padrão para uma API
*/
public function getHeaders(string $api): array
{
switch ($api) {
case 'coinmarketcap':
return [
'X-CMC_PRO_API_KEY: ' . $this->get('coinmarketcap', 'api_key'),
'Accept: application/json'
];
case 'stripe':
return [
'Authorization: Bearer ' . $this->get('stripe', 'api_key'),
'Content-Type: application/x-www-form-urlencoded'
];
default:
return [];
}
}
/**
* Construir URL completa para uma API
*/
public function buildUrl(string $api, string $endpoint): string
{
$baseUrl = $this->get($api, 'base_url');
return rtrim($baseUrl, '/') . '/' . ltrim($endpoint, '/');
}
}