Lines
71.05%
27 / 38
Methods
66.66%
4 / 6
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| configure | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 1 | ||
| initializeCache | 75.00% | 3 / 4 | 0.00% | 0 / 1 | 2.06 | ||
| tryInitializeCache | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| getRequestLimits | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| registerHttpMiddleware | 0.00% | 0 / 10 | 0.00% | 0 / 1 | 6 | ||
| getenvInt | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 4 | ||
| 13 | final class ModuleLoggerInitializer | |
| 14 | { | |
| 15 | /** | |
| 16 | * @SuppressWarnings(PHPMD.NPathComplexity) | |
| 17 | */ | |
| 18 | public static function configure(string $envPrefix): void | |
| 19 | { | |
| 20 | LoggerService::configure([ | |
| 21 | 'maxRequests' => self::getenvInt("{$envPrefix}_LOGGER_MAX_REQUESTS", 1000), | |
| 22 | 'maxErrorRequests' => self::getenvInt("{$envPrefix}_LOGGER_MAX_ERROR_REQUESTS", 0), | |
| 23 | 'responseLength' => self::getenvInt("{$envPrefix}_LOGGER_RESPONSE_LENGTH", 1048576), | |
| 24 | 'stackLines' => self::getenvInt("{$envPrefix}_LOGGER_STACK_LINES", 20), | |
| 25 | 'messageSize' => self::getenvInt("{$envPrefix}_LOGGER_MESSAGE_SIZE", 8192), | |
| 26 | 'cacheTtl' => self::getenvInt("{$envPrefix}_LOGGER_CACHE_TTL", 60), | |
| 27 | 'maxRetries' => self::getenvInt("{$envPrefix}_LOGGER_MAX_RETRIES", 3), | |
| 28 | 'backoffMin' => self::getenvInt("{$envPrefix}_LOGGER_BACKOFF_MIN", 100), | |
| 29 | 'backoffMax' => self::getenvInt("{$envPrefix}_LOGGER_BACKOFF_MAX", 1000), | |
| 30 | 'lockTimeout' => self::getenvInt("{$envPrefix}_LOGGER_LOCK_TIMEOUT", 5), | |
| 31 | ]); | |
| 32 | } | |
| 33 | ||
| 34 | public static function initializeCache(?string $cacheDir = null): CacheInterface | |
| 35 | { | |
| 36 | if ($cacheDir !== null) { | |
| 37 | $ttl = self::getenvInt('SOURCE_CACHE_TTL', 3600); | |
| 38 | ||
| 39 | return CacheBootstrap::create($cacheDir, $ttl); | |
| 40 | } | |
| 41 | ||
| 42 | return CacheBootstrap::createFromEnv(sys_get_temp_dir()); | |
| 43 | } | |
| 44 | ||
| 45 | public static function tryInitializeCache(?string $cacheDir = null): ?CacheInterface | |
| 46 | { | |
| 47 | try { | |
| 48 | return self::initializeCache($cacheDir); | |
| 49 | } catch (\RuntimeException) { | |
| 50 | LoggerService::$cache = null; | |
| 51 | ||
| 52 | return null; | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * @return array{maxStringLength: int, maxRecursionDepth: int} | |
| 58 | */ | |
| 59 | public static function getRequestLimits(): array | |
| 60 | { | |
| 61 | return [ | |
| 62 | 'maxStringLength' => self::getenvInt('MAX_STRING_LENGTH', 32768), | |
| 63 | 'maxRecursionDepth' => self::getenvInt('MAX_RECURSION_DEPTH', 10), | |
| 64 | ]; | |
| 65 | } | |
| 66 | ||
| 67 | public static function registerHttpMiddleware(bool $includeSecurityHeaders = true): void | |
| 68 | { | |
| 69 | $logger = new LoggerService(); | |
| 70 | $requestLimits = self::getRequestLimits(); | |
| 71 | ||
| 72 | \App::$slim->add(new RequestLoggingMiddleware($logger)); | |
| 73 | if ($includeSecurityHeaders) { | |
| 74 | \App::$slim->add(new SecurityHeadersMiddleware($logger)); | |
| 75 | } | |
| 76 | \App::$slim->add(new RequestSanitizerMiddleware( | |
| 77 | $logger, | |
| 78 | $requestLimits['maxRecursionDepth'], | |
| 79 | $requestLimits['maxStringLength'] | |
| 80 | )); | |
| 81 | } | |
| 82 | ||
| 83 | private static function getenvInt(string $name, int $default): int | |
| 84 | { | |
| 85 | $value = getenv($name); | |
| 86 | if ($value === false || $value === '' || $value === '0') { | |
| 87 | return $default; | |
| 88 | } | |
| 89 | ||
| 90 | return (int) $value; | |
| 91 | } | |
| 92 | } |