Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiAuthConfigLoader
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
4 / 4
12
100.00% covered (success)
100.00%
1 / 1
 load
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 getSystemApiToken
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 setConfig
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 reset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Shared\Infrastructure\Config;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Centralized API Authentication Configuration Loader.
13 *
14 * Provides static cached access to system Bearer token configuration.
15 *
16 * @package App\Shared\Infrastructure\Config
17 */
18final class ApiAuthConfigLoader
19{
20    /** @var array<string, mixed>|null */
21    private static ?array $cachedFileConfig = null;
22
23    /** @var array<string, mixed>|null */
24    private static ?array $config = null;
25
26    /**
27     * Loads and caches API auth configuration.
28     *
29     * @return array<string, mixed> Configuration dictionary.
30     */
31    public static function load(): array
32    {
33        if (self::$config !== null) {
34            return self::$config;
35        }
36
37        $configFile = dirname(__DIR__, 4) . '/config/common/api_auth.php';
38        if (!file_exists($configFile)) {
39            self::$config = [];
40        } elseif (self::$cachedFileConfig !== null) {
41            self::$config = self::$cachedFileConfig;
42        } else {
43            $loaded = (static fn(string $f) => require_once $f)($configFile);
44            self::$cachedFileConfig = is_array($loaded) ? $loaded : [];
45            self::$config = self::$cachedFileConfig;
46        }
47
48        return self::$config;
49    }
50
51    /**
52     * Resolves the system API Bearer token.
53     *
54     * @return string System Bearer token string.
55     */
56    public static function getSystemApiToken(): string
57    {
58        if (self::$config !== null && array_key_exists('system_api_token', self::$config)) {
59            return (string) self::$config['system_api_token'];
60        }
61
62        $envToken = (string) ($_ENV['SYSTEM_API_TOKEN'] ?? getenv('SYSTEM_API_TOKEN') ?: '');
63        if ($envToken !== '') {
64            return $envToken;
65        }
66
67        $config = self::load();
68        return (string) ($config['system_api_token'] ?? '');
69    }
70
71    /**
72     * Injects custom configuration (for testing).
73     *
74     * @param array<string, mixed>|null $config Configuration array or null.
75     */
76    public static function setConfig(?array $config): void
77    {
78        self::$config = $config;
79    }
80
81    /**
82     * Resets cached state (for testing).
83     */
84    public static function reset(): void
85    {
86        self::$config = null;
87    }
88}