Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
21.43% |
3 / 14 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Application | |
21.43% |
3 / 14 |
|
20.00% |
1 / 5 |
69.69 | |
0.00% |
0 / 1 |
| getNow | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| initializeCache | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| validateCacheDirectory | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| setupCache | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| initialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package 115Mandant |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsapi; |
| 9 | |
| 10 | use Psr\SimpleCache\CacheInterface; |
| 11 | use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
| 12 | use Symfony\Component\Cache\Psr16Cache; |
| 13 | |
| 14 | if (($token = getenv('ZMS_CONFIG_SECURE_TOKEN')) === false || $token === '') { |
| 15 | throw new \RuntimeException('ZMS_CONFIG_SECURE_TOKEN environment variable must be set'); |
| 16 | } |
| 17 | |
| 18 | define('ZMS_CONFIG_SECURE_TOKEN', getenv('ZMS_CONFIG_SECURE_TOKEN')); |
| 19 | |
| 20 | if (!defined('ZMS_API_TWIG_CACHE')) { |
| 21 | $value = getenv('ZMS_API_TWIG_CACHE'); |
| 22 | define('ZMS_API_TWIG_CACHE', ($value === 'false') ? false : ($value ?: '/cache/')); |
| 23 | } |
| 24 | |
| 25 | class Application extends \BO\Slim\Application |
| 26 | { |
| 27 | /** |
| 28 | * Name of the application |
| 29 | */ |
| 30 | const IDENTIFIER = 'zms'; |
| 31 | |
| 32 | const MODULE_NAME = 'zmsapi'; |
| 33 | |
| 34 | public static ?CacheInterface $cache = null; |
| 35 | // Cache config |
| 36 | public static string $CACHE_DIR; |
| 37 | public static int $SOURCE_CACHE_TTL; |
| 38 | |
| 39 | /** |
| 40 | * @var Bool DEBUG |
| 41 | */ |
| 42 | const DEBUG = false; |
| 43 | const TWIG_CACHE = ZMS_API_TWIG_CACHE; |
| 44 | |
| 45 | /** |
| 46 | * @var Bool DB_ENABLE_WSREPSYNCWAIT |
| 47 | */ |
| 48 | const DB_ENABLE_WSREPSYNCWAIT = false; |
| 49 | |
| 50 | /** |
| 51 | * @var Bool RIGHTSCHECK_ENABLED |
| 52 | */ |
| 53 | const RIGHTSCHECK_ENABLED = true; |
| 54 | |
| 55 | /** |
| 56 | * @var String DB_DSN_READONLY |
| 57 | */ |
| 58 | const DB_DSN_READONLY = 'mysql:dbname=zmsbo;host=127.0.0.1'; |
| 59 | |
| 60 | /** |
| 61 | * @var String DB_DSN_READWRITE |
| 62 | */ |
| 63 | const DB_DSN_READWRITE = 'mysql:dbname=zmsbo;host=127.0.0.1'; |
| 64 | |
| 65 | /** |
| 66 | * temporary db name for using dldb data |
| 67 | * @var String DB_STARTINFO |
| 68 | */ |
| 69 | const DB_STARTINFO = 'startinfo'; |
| 70 | |
| 71 | /** |
| 72 | * @var String DB_USERNAME |
| 73 | */ |
| 74 | const DB_USERNAME = 'server'; |
| 75 | |
| 76 | /** |
| 77 | * @var String DB_PASSWORD |
| 78 | */ |
| 79 | const DB_PASSWORD = 'internet'; |
| 80 | |
| 81 | /** |
| 82 | * @var String DB_IS_GALERA |
| 83 | */ |
| 84 | const DB_IS_GALERA = true; |
| 85 | |
| 86 | /** |
| 87 | * @var String Security Token for Api Access -> get config for example |
| 88 | */ |
| 89 | const SECURE_TOKEN = ZMS_CONFIG_SECURE_TOKEN; |
| 90 | |
| 91 | /** |
| 92 | * language preferences |
| 93 | */ |
| 94 | |
| 95 | public static $locale = 'de'; |
| 96 | |
| 97 | public static $supportedLanguages = array( |
| 98 | // Default language |
| 99 | 'de' => array( |
| 100 | 'name' => 'Deutsch', |
| 101 | 'locale' => 'de_DE.utf-8', |
| 102 | 'default' => true, |
| 103 | ) |
| 104 | ); |
| 105 | |
| 106 | /** |
| 107 | * dldb data path |
| 108 | */ |
| 109 | public static $data = '/data'; |
| 110 | |
| 111 | /** |
| 112 | * @var \DateTimeInterface $now time to use for today (testing) |
| 113 | */ |
| 114 | public static $now = null; |
| 115 | |
| 116 | public static function getNow() |
| 117 | { |
| 118 | if (self::$now instanceof \DateTimeInterface) { |
| 119 | return self::$now; |
| 120 | } |
| 121 | return new \DateTimeImmutable(); |
| 122 | } |
| 123 | |
| 124 | private static function initializeCache(): void |
| 125 | { |
| 126 | self::$CACHE_DIR = getenv('CACHE_DIR') ?: __DIR__ . '/cache'; |
| 127 | self::$SOURCE_CACHE_TTL = (int) (getenv('SOURCE_CACHE_TTL') ?: 3600); |
| 128 | self::validateCacheDirectory(); |
| 129 | self::setupCache(); |
| 130 | } |
| 131 | |
| 132 | private static function validateCacheDirectory(): void |
| 133 | { |
| 134 | if (!is_dir(self::$CACHE_DIR) && !mkdir(self::$CACHE_DIR, 0750, true)) { |
| 135 | throw new \RuntimeException(sprintf('Cache directory "%s" could not be created', self::$CACHE_DIR)); |
| 136 | } |
| 137 | |
| 138 | if (!is_writable(self::$CACHE_DIR)) { |
| 139 | throw new \RuntimeException(sprintf('Cache directory "%s" is not writable', self::$CACHE_DIR)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private static function setupCache(): void |
| 144 | { |
| 145 | $psr6 = new FilesystemAdapter(namespace: '', defaultLifetime: self::$SOURCE_CACHE_TTL, directory: self::$CACHE_DIR); |
| 146 | self::$cache = new Psr16Cache($psr6); |
| 147 | } |
| 148 | |
| 149 | public static function initialize(): void |
| 150 | { |
| 151 | self::initializeCache(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | Application::initialize(); |