|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flutterwave\Test\Resources\Setup; |
| 6 | + |
| 7 | +use Flutterwave\Contract\ConfigInterface; |
| 8 | +use GuzzleHttp\Client; |
| 9 | +use GuzzleHttp\ClientInterface; |
| 10 | +use function is_null; |
| 11 | +use Monolog\Handler\RotatingFileHandler; |
| 12 | +use Monolog\Logger; |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | + |
| 15 | +class Config implements ConfigInterface |
| 16 | +{ |
| 17 | + public const PUBLIC_KEY = 'PUBLIC_KEY'; |
| 18 | + public const SECRET_KEY = 'SECRET_KEY'; |
| 19 | + public const ENCRYPTION_KEY = 'ENCRYPTION_KEY'; |
| 20 | + public const ENV = 'ENV'; |
| 21 | + public const VERSION = 'v3'; |
| 22 | + public const BASE_URL = 'https://api.flutterwave.com/'.self::VERSION; |
| 23 | + public const DEFAULT_PREFIX = 'FW|PHP'; |
| 24 | + public const LOG_FILE_NAME = 'flutterwave-php.log'; |
| 25 | + protected Logger $logger; |
| 26 | + private string $secret; |
| 27 | + private string $public; |
| 28 | + |
| 29 | + private static ?Config $instance = null; |
| 30 | + private string $env; |
| 31 | + private ClientInterface $http; |
| 32 | + private string $enc; |
| 33 | + |
| 34 | + private function __construct(string $secretKey, string $publicKey, string $encryptKey, string $env) |
| 35 | + { |
| 36 | + $this->secret = $secretKey; |
| 37 | + $this->public = $publicKey; |
| 38 | + $this->enc = $encryptKey; |
| 39 | + $this->env = $env; |
| 40 | + |
| 41 | + $this->http = new Client([ |
| 42 | + 'base_uri' => $this->getBaseUrl(), |
| 43 | + 'timeout' => 60, |
| 44 | + ]); |
| 45 | + |
| 46 | + $log = new Logger('Flutterwave/PHP'); |
| 47 | + $this->logger = $log; |
| 48 | + $log->pushHandler(new RotatingFileHandler(self::LOG_FILE_NAME, 90)); |
| 49 | + } |
| 50 | + |
| 51 | + public static function setUp(string $secretKey, string $publicKey, string $enc, string $env): ConfigInterface |
| 52 | + { |
| 53 | + if (is_null(self::$instance)) { |
| 54 | + return new Config($secretKey, $publicKey, $enc, $env); |
| 55 | + } |
| 56 | + return self::$instance; |
| 57 | + } |
| 58 | + |
| 59 | + public function getHttp(): ClientInterface |
| 60 | + { |
| 61 | + return $this->http ?? new Client(); |
| 62 | + } |
| 63 | + |
| 64 | + public function getLoggerInstance(): LoggerInterface |
| 65 | + { |
| 66 | + return $this->logger; |
| 67 | + } |
| 68 | + |
| 69 | + public function getEncryptkey(): string |
| 70 | + { |
| 71 | + return $this->enc; |
| 72 | + } |
| 73 | + |
| 74 | + public function getPublicKey(): string |
| 75 | + { |
| 76 | + return $this->public; |
| 77 | + } |
| 78 | + |
| 79 | + public static function getBaseUrl(): string |
| 80 | + { |
| 81 | + return self::BASE_URL; |
| 82 | + } |
| 83 | + |
| 84 | + public function getSecretKey(): string |
| 85 | + { |
| 86 | + return $this->secret; |
| 87 | + } |
| 88 | + |
| 89 | + public function getEnv(): string |
| 90 | + { |
| 91 | + return $this->env; |
| 92 | + } |
| 93 | + |
| 94 | + public static function getDefaultTransactionPrefix(): string |
| 95 | + { |
| 96 | + return self::DEFAULT_PREFIX; |
| 97 | + } |
| 98 | +} |
0 commit comments