Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.65% covered (warning)
67.65%
23 / 34
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ModuleLoggerInitializer
67.65% covered (warning)
67.65%
23 / 34
60.00% covered (warning)
60.00%
3 / 5
35.93
0.00% covered (danger)
0.00%
0 / 1
 configure
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
11
 initializeCache
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 tryInitializeCache
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getRequestLimits
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 registerHttpMiddleware
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Slim\Helper;
6
7use BO\Slim\LoggerService;
8use BO\Slim\Middleware\RequestLoggingMiddleware;
9use BO\Slim\Middleware\RequestSanitizerMiddleware;
10use BO\Slim\Middleware\SecurityHeadersMiddleware;
11use Psr\SimpleCache\CacheInterface;
12
13final class ModuleLoggerInitializer
14{
15    /**
16     * @SuppressWarnings(PHPMD.NPathComplexity)
17     */
18    public static function configure(string $envPrefix): void
19    {
20        LoggerService::configure([
21            'maxRequests' => (int) (getenv("{$envPrefix}_LOGGER_MAX_REQUESTS") ?: 1000),
22            'maxErrorRequests' => (int) (getenv("{$envPrefix}_LOGGER_MAX_ERROR_REQUESTS") ?: 0),
23            'responseLength' => (int) (getenv("{$envPrefix}_LOGGER_RESPONSE_LENGTH") ?: 1048576),
24            'stackLines' => (int) (getenv("{$envPrefix}_LOGGER_STACK_LINES") ?: 20),
25            'messageSize' => (int) (getenv("{$envPrefix}_LOGGER_MESSAGE_SIZE") ?: 8192),
26            'cacheTtl' => (int) (getenv("{$envPrefix}_LOGGER_CACHE_TTL") ?: 60),
27            'maxRetries' => (int) (getenv("{$envPrefix}_LOGGER_MAX_RETRIES") ?: 3),
28            'backoffMin' => (int) (getenv("{$envPrefix}_LOGGER_BACKOFF_MIN") ?: 100),
29            'backoffMax' => (int) (getenv("{$envPrefix}_LOGGER_BACKOFF_MAX") ?: 1000),
30            'lockTimeout' => (int) (getenv("{$envPrefix}_LOGGER_LOCK_TIMEOUT") ?: 5),
31        ]);
32    }
33
34    public static function initializeCache(?string $cacheDir = null): CacheInterface
35    {
36        if ($cacheDir !== null) {
37            $ttl = (int) (getenv('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' => (int) (getenv('MAX_STRING_LENGTH') ?: 32768),
63            'maxRecursionDepth' => (int) (getenv('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}