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