Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.63% covered (success)
94.63%
194 / 205
92.16% covered (success)
92.16%
47 / 51
CRAP
0.00% covered (danger)
0.00%
0 / 1
EnvUtils
94.63% covered (success)
94.63%
194 / 205
92.16% covered (success)
92.16%
47 / 51
59.54
0.00% covered (danger)
0.00%
0 / 1
 lazyInit
62.50% covered (warning)
62.50%
10 / 16
0.00% covered (danger)
0.00%
0 / 1
6.32
 configure
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
1 / 1
1
 getPrivatePath
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDataPath
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getDataHref
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getCodePath
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getCodeHref
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSyslogPath
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getBaseHref
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getAppEnv
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getMysqlHost
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getMysqlPort
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getMysqlServer
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMysqlUsername
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getMysqlPassword
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getMysqlSchema
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 hasUnlimitedCron
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDateUtilsClassName
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDateUtilsClassArgs
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDatabaseBackupKey
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getEmailReactionKey
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getIdEncryptionKey
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getCronAuthenticityCode
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getStravaClientId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getStravaClientSecret
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getTelegramBotName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getTelegramBotToken
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getTelegramAuthenticityCode
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getImapHost
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getImapPort
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getImapFlags
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getImapUsername
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getImapPassword
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSmtpHost
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSmtpPort
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSmtpUsername
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSmtpPassword
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSmtpSecure
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSmtpDebug
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSmtpFrom
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getEmailForwardingHost
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getAppSearchEnginesUsername
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getAppSearchEnginesPassword
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getAppMonitoringUsername
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getAppMonitoringPassword
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getAppStatisticsUsername
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getAppStatisticsPassword
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getDeployStatus
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 checkNotNull
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 getConfigPath
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 fromEnv
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
1.04
1<?php
2
3namespace Olz\Utils;
4
5class EnvUtils {
6    protected ?string $private_path = null;
7    protected ?string $data_path = null;
8    protected ?string $data_href = null;
9    protected ?string $code_path = null;
10    protected ?string $code_href = null;
11
12    private ?string $syslog_path = null;
13    private ?string $base_href = null;
14    private ?string $app_env = null;
15
16    private ?string $mysql_host = null;
17    private ?string $mysql_port = null;
18    private ?string $mysql_username = null;
19    private ?string $mysql_password = null;
20    private ?string $mysql_schema = null;
21
22    private bool $has_unlimited_cron = false;
23
24    private string $date_utils_class_name = 'LiveDateUtils';
25    /** @var array<string, mixed> */
26    private array $date_utils_class_args = [];
27
28    private ?string $database_backup_key = null;
29    private ?string $email_reaction_key = null;
30    private ?string $id_encryption_key = null;
31
32    private ?string $cron_authenticity_code = null;
33
34    private ?string $strava_client_id = null;
35    private ?string $strava_client_secret = null;
36
37    private ?string $telegram_bot_name = null;
38    private ?string $telegram_bot_token = null;
39    private ?string $telegram_authenticity_code = null;
40
41    private ?string $imap_host = null;
42    private ?string $imap_port = null;
43    private ?string $imap_flags = '';
44    private ?string $imap_username = null;
45    private ?string $imap_password = null;
46
47    private ?string $smtp_host = null;
48    private ?string $smtp_port = null;
49    private ?string $smtp_username = null;
50    private ?string $smtp_password = null;
51    private ?string $smtp_secure = '';
52    private int $smtp_debug = 0;
53    private ?string $smtp_from = null;
54
55    private ?string $email_forwarding_host = null;
56
57    private ?string $app_search_engines_username = null;
58    private ?string $app_search_engines_password = null;
59
60    private ?string $app_monitoring_username = null;
61    private ?string $app_monitoring_password = null;
62
63    private ?string $app_statistics_username = null;
64    private ?string $app_statistics_password = null;
65
66    private bool $lazyInitCompleted = false;
67
68    public function lazyInit(): void {
69        if ($this->lazyInitCompleted) {
70            return;
71        }
72        global $_SERVER;
73
74        $this->data_href = '/';
75
76        $code_href = '/';
77        if (isset($_SERVER['OLZ_SYMFONY_HREF']) && preg_match('/^\/.+\/$/', $_SERVER['OLZ_SYMFONY_HREF'])) {
78            $code_href = $_SERVER['OLZ_SYMFONY_HREF'];
79        }
80        $this->code_href = $code_href;
81        $this->code_path = realpath(__DIR__.'/../../').'/';
82
83        $config_path = self::getConfigPath();
84        if (!is_file($config_path)) {
85            throw new \Exception("Konfigurationsdatei ({$config_path}) nicht gefunden!");
86        }
87
88        $configuration = require $config_path;
89        $configure_env_utils_function = $configuration['configure_env_utils'];
90        $configure_env_utils_function($this);
91
92        $this->lazyInitCompleted = true;
93    }
94
95    /** @param array<string, mixed> $config_dict */
96    public function configure(array $config_dict): void {
97        $this->data_path = $config_dict['data_path'] ?? $this->data_path;
98        $this->private_path = $config_dict['private_path'] ?? $this->private_path;
99        $this->syslog_path = $config_dict['syslog_path'] ?? $this->syslog_path;
100        $this->base_href = $config_dict['base_href'] ?? $this->base_href;
101        $this->app_env = $config_dict['app_env'] ?? $this->app_env;
102
103        $this->mysql_host = $config_dict['mysql_host'] ?? $this->mysql_host;
104        $this->mysql_port = $config_dict['mysql_port'] ?? $this->mysql_port;
105        $this->mysql_username = $config_dict['mysql_username'] ?? $this->mysql_username;
106        $this->mysql_password = $config_dict['mysql_password'] ?? $this->mysql_password;
107        $this->mysql_schema = $config_dict['mysql_schema'] ?? $this->mysql_schema;
108
109        $this->has_unlimited_cron = $config_dict['has_unlimited_cron'] ?? $this->has_unlimited_cron;
110
111        $this->date_utils_class_name = $config_dict['date_utils_class_name'] ?? $this->date_utils_class_name;
112        $this->date_utils_class_args = $config_dict['date_utils_class_args'] ?? $this->date_utils_class_args;
113
114        $this->database_backup_key = $config_dict['database_backup_key'] ?? $this->database_backup_key;
115        $this->email_reaction_key = $config_dict['email_reaction_key'] ?? $this->email_reaction_key;
116        $this->id_encryption_key = $config_dict['id_encryption_key'] ?? $this->id_encryption_key;
117
118        $this->cron_authenticity_code = $config_dict['cron_authenticity_code'] ?? $this->cron_authenticity_code;
119
120        $this->strava_client_id = $config_dict['strava_client_id'] ?? $this->strava_client_id;
121        $this->strava_client_secret = $config_dict['strava_client_secret'] ?? $this->strava_client_secret;
122
123        $this->telegram_bot_name = $config_dict['telegram_bot_name'] ?? $this->telegram_bot_name;
124        $this->telegram_bot_token = $config_dict['telegram_bot_token'] ?? $this->telegram_bot_token;
125        $this->telegram_authenticity_code = $config_dict['telegram_authenticity_code'] ?? $this->telegram_authenticity_code;
126
127        $this->imap_host = $config_dict['imap_host'] ?? $this->imap_host;
128        $this->imap_port = $config_dict['imap_port'] ?? $this->imap_port;
129        $this->imap_flags = $config_dict['imap_flags'] ?? $this->imap_flags;
130        $this->imap_username = $config_dict['imap_username'] ?? $this->imap_username;
131        $this->imap_password = $config_dict['imap_password'] ?? $this->imap_password;
132
133        $this->smtp_host = $config_dict['smtp_host'] ?? $this->smtp_host;
134        $this->smtp_port = $config_dict['smtp_port'] ?? $this->smtp_port;
135        $this->smtp_username = $config_dict['smtp_username'] ?? $this->smtp_username;
136        $this->smtp_password = $config_dict['smtp_password'] ?? $this->smtp_password;
137        $this->smtp_secure = $config_dict['smtp_secure'] ?? $this->smtp_secure;
138        $this->smtp_debug = $config_dict['smtp_debug'] ?? $this->smtp_debug;
139        $this->smtp_from = $config_dict['smtp_from'] ?? $this->smtp_from;
140
141        $this->email_forwarding_host = $config_dict['email_forwarding_host'] ?? $this->email_forwarding_host;
142
143        $this->app_search_engines_username = $config_dict['app_search_engines_username'] ?? $this->app_search_engines_username;
144        $this->app_search_engines_password = $config_dict['app_search_engines_password'] ?? $this->app_search_engines_password;
145
146        $this->app_monitoring_username = $config_dict['app_monitoring_username'] ?? $this->app_monitoring_username;
147        $this->app_monitoring_password = $config_dict['app_monitoring_password'] ?? $this->app_monitoring_password;
148
149        $this->app_statistics_username = $config_dict['app_statistics_username'] ?? $this->app_statistics_username;
150        $this->app_statistics_password = $config_dict['app_statistics_password'] ?? $this->app_statistics_password;
151    }
152
153    public function getPrivatePath(): ?string {
154        $this->lazyInit();
155        return $this->private_path;
156    }
157
158    public function getDataPath(): string {
159        $this->lazyInit();
160        $this->checkNotNull($this->data_path, "data_path not set");
161        return $this->data_path;
162    }
163
164    public function getDataHref(): string {
165        $this->lazyInit();
166        $this->checkNotNull($this->data_href, "data_href not set");
167        return $this->data_href;
168    }
169
170    public function getCodePath(): string {
171        $this->lazyInit();
172        $this->checkNotNull($this->code_path, "code_path not set");
173        return $this->code_path;
174    }
175
176    public function getCodeHref(): string {
177        $this->lazyInit();
178        $this->checkNotNull($this->code_href, "code_href not set");
179        return $this->code_href;
180    }
181
182    public function getSyslogPath(): string {
183        $this->lazyInit();
184        $this->checkNotNull($this->syslog_path, "syslog_path not set");
185        return $this->syslog_path;
186    }
187
188    public function getBaseHref(): string {
189        $this->lazyInit();
190        $this->checkNotNull($this->base_href, "base_href not set");
191        return $this->base_href;
192    }
193
194    public function getAppEnv(): string {
195        $this->lazyInit();
196        $this->checkNotNull($this->app_env, "app_env not set");
197        return $this->app_env;
198    }
199
200    public function getMysqlHost(): string {
201        $this->lazyInit();
202        $this->checkNotNull($this->mysql_host, "mysql_host not set");
203        return $this->mysql_host;
204    }
205
206    public function getMysqlPort(): string {
207        $this->lazyInit();
208        $this->checkNotNull($this->mysql_port, "mysql_port not set");
209        return $this->mysql_port;
210    }
211
212    public function getMysqlServer(): string {
213        $this->lazyInit();
214        return "{$this->mysql_host}:{$this->mysql_port}";
215    }
216
217    public function getMysqlUsername(): string {
218        $this->lazyInit();
219        $this->checkNotNull($this->mysql_username, "mysql_username not set");
220        return $this->mysql_username;
221    }
222
223    public function getMysqlPassword(): string {
224        $this->lazyInit();
225        $this->checkNotNull($this->mysql_password, "mysql_password not set");
226        return $this->mysql_password;
227    }
228
229    public function getMysqlSchema(): string {
230        $this->lazyInit();
231        $this->checkNotNull($this->mysql_schema, "mysql_schema not set");
232        return $this->mysql_schema;
233    }
234
235    public function hasUnlimitedCron(): bool {
236        $this->lazyInit();
237        return $this->has_unlimited_cron;
238    }
239
240    public function getDateUtilsClassName(): string {
241        $this->lazyInit();
242        return $this->date_utils_class_name;
243    }
244
245    /** @return array<mixed> */
246    public function getDateUtilsClassArgs(): array {
247        $this->lazyInit();
248        return $this->date_utils_class_args;
249    }
250
251    public function getDatabaseBackupKey(): string {
252        $this->lazyInit();
253        $this->checkNotNull($this->database_backup_key, "database_backup_key not set");
254        return $this->database_backup_key;
255    }
256
257    public function getEmailReactionKey(): string {
258        $this->lazyInit();
259        $this->checkNotNull($this->email_reaction_key, "email_reaction_key not set");
260        return $this->email_reaction_key;
261    }
262
263    public function getIdEncryptionKey(): string {
264        $this->lazyInit();
265        $this->checkNotNull($this->id_encryption_key, "id_encryption_key not set");
266        return $this->id_encryption_key;
267    }
268
269    public function getCronAuthenticityCode(): string {
270        $this->lazyInit();
271        $this->checkNotNull($this->cron_authenticity_code, "cron_authenticity_code not set");
272        return $this->cron_authenticity_code;
273    }
274
275    public function getStravaClientId(): string {
276        $this->lazyInit();
277        $this->checkNotNull($this->strava_client_id, "strava_client_id not set");
278        return $this->strava_client_id;
279    }
280
281    public function getStravaClientSecret(): string {
282        $this->lazyInit();
283        $this->checkNotNull($this->strava_client_secret, "strava_client_secret not set");
284        return $this->strava_client_secret;
285    }
286
287    public function getTelegramBotName(): string {
288        $this->lazyInit();
289        $this->checkNotNull($this->telegram_bot_name, "telegram_bot_name not set");
290        return $this->telegram_bot_name;
291    }
292
293    public function getTelegramBotToken(): string {
294        $this->lazyInit();
295        $this->checkNotNull($this->telegram_bot_token, "telegram_bot_token not set");
296        return $this->telegram_bot_token;
297    }
298
299    public function getTelegramAuthenticityCode(): string {
300        $this->lazyInit();
301        $this->checkNotNull($this->telegram_authenticity_code, "telegram_authenticity_code not set");
302        return $this->telegram_authenticity_code;
303    }
304
305    public function getImapHost(): string {
306        $this->lazyInit();
307        $this->checkNotNull($this->imap_host, "imap_host not set");
308        return $this->imap_host;
309    }
310
311    public function getImapPort(): string {
312        $this->lazyInit();
313        $this->checkNotNull($this->imap_port, "imap_port not set");
314        return $this->imap_port;
315    }
316
317    public function getImapFlags(): string {
318        $this->lazyInit();
319        $this->checkNotNull($this->imap_flags, "imap_flags not set");
320        return $this->imap_flags;
321    }
322
323    public function getImapUsername(): string {
324        $this->lazyInit();
325        $this->checkNotNull($this->imap_username, "imap_username not set");
326        return $this->imap_username;
327    }
328
329    public function getImapPassword(): string {
330        $this->lazyInit();
331        $this->checkNotNull($this->imap_password, "imap_password not set");
332        return $this->imap_password;
333    }
334
335    public function getSmtpHost(): string {
336        $this->lazyInit();
337        $this->checkNotNull($this->smtp_host, "smtp_host not set");
338        return $this->smtp_host;
339    }
340
341    public function getSmtpPort(): string {
342        $this->lazyInit();
343        $this->checkNotNull($this->smtp_port, "smtp_port not set");
344        return $this->smtp_port;
345    }
346
347    public function getSmtpUsername(): string {
348        $this->lazyInit();
349        $this->checkNotNull($this->smtp_username, "smtp_username not set");
350        return $this->smtp_username;
351    }
352
353    public function getSmtpPassword(): string {
354        $this->lazyInit();
355        $this->checkNotNull($this->smtp_password, "smtp_password not set");
356        return $this->smtp_password;
357    }
358
359    public function getSmtpSecure(): string {
360        $this->lazyInit();
361        $this->checkNotNull($this->smtp_secure, "smtp_secure not set");
362        return $this->smtp_secure;
363    }
364
365    public function getSmtpDebug(): int {
366        $this->lazyInit();
367        return $this->smtp_debug;
368    }
369
370    public function getSmtpFrom(): string {
371        $this->lazyInit();
372        $this->checkNotNull($this->smtp_from, "smtp_from not set");
373        return $this->smtp_from;
374    }
375
376    public function getEmailForwardingHost(): string {
377        $this->lazyInit();
378        $this->checkNotNull($this->email_forwarding_host, "email_forwarding_host not set");
379        return $this->email_forwarding_host;
380    }
381
382    public function getAppSearchEnginesUsername(): string {
383        $this->lazyInit();
384        $this->checkNotNull($this->app_search_engines_username, "app_search_engines_username not set");
385        return $this->app_search_engines_username;
386    }
387
388    public function getAppSearchEnginesPassword(): string {
389        $this->lazyInit();
390        $this->checkNotNull($this->app_search_engines_password, "app_search_engines_password not set");
391        return $this->app_search_engines_password;
392    }
393
394    public function getAppMonitoringUsername(): string {
395        $this->lazyInit();
396        $this->checkNotNull($this->app_monitoring_username, "app_monitoring_username not set");
397        return $this->app_monitoring_username;
398    }
399
400    public function getAppMonitoringPassword(): string {
401        $this->lazyInit();
402        $this->checkNotNull($this->app_monitoring_password, "app_monitoring_password not set");
403        return $this->app_monitoring_password;
404    }
405
406    public function getAppStatisticsUsername(): string {
407        $this->lazyInit();
408        $this->checkNotNull($this->app_statistics_username, "app_statistics_username not set");
409        return $this->app_statistics_username;
410    }
411
412    public function getAppStatisticsPassword(): string {
413        $this->lazyInit();
414        $this->checkNotNull($this->app_statistics_password, "app_statistics_password not set");
415        return $this->app_statistics_password;
416    }
417
418    /** @return array{status: ?string, date: ?string} */
419    public function getDeployStatus(): array {
420        $status_path = "{$this->getPrivatePath()}deploy/status.json";
421        $status = json_decode(@file_get_contents($status_path) ?: '{}', true);
422        return [
423            'status' => $status['status'] ?? null,
424            'date' => $status['date'] ?? null,
425        ];
426    }
427
428    /** @phpstan-assert !null $value */
429    protected function checkNotNull(mixed $value, string $error_message): void {
430        if ($value === null) {
431            throw new \Exception($error_message);
432        }
433    }
434
435    public static function getConfigPath(): string {
436        global $_ENV;
437        $env = $_ENV['APP_ENV'] ?? getenv('APP_ENV');
438        if ($env) {
439            $injected_env_path = dirname(__DIR__, 2)."/config/olz.{$env}.php";
440            if (is_file($injected_env_path)) {
441                return $injected_env_path;
442            }
443        }
444        $injected_path = dirname(__DIR__, 2).'/config/olz.php';
445        return $injected_path;
446    }
447
448    public static function fromEnv(): self {
449        $instance = new self();
450        $instance->lazyInit();
451        return $instance;
452    }
453}