| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of CriticalMessageService |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
class CriticalMessageService |
| 12 |
{ |
| 13 |
public const TYPE_TOKEN_MISSING = 'token_missing'; |
| 14 |
public const TYPE_TOKEN_EXPIRED = 'token_expired'; |
| 15 |
public const TYPE_NO_DEFAULT_TOKEN_SELECTED = 'no_default_token_selected'; |
| 16 |
public const TYPE_TOKEN_REFRESHED = 'token_refreshed'; |
| 17 |
|
| 18 |
private Settings $SettingsService; |
| 19 |
|
| 20 |
public function __construct(Settings $SettingsService) |
| 21 |
{ |
| 22 |
$this->SettingsService = $SettingsService; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get all active (not-resolved) critical messages. |
| 27 |
* Info-type messages are auto-resolved after being retrieved (shown once). |
| 28 |
* |
| 29 |
* @return array<int,array{code:string,text:string,type:string,link:?string,resolved:bool}> |
| 30 |
*/ |
| 31 |
public function getActiveMessages(): array |
| 32 |
{ |
| 33 |
$messages = $this->getMessagesData(); |
| 34 |
|
| 35 |
$activeMessages = []; |
| 36 |
$needsSave = false; |
| 37 |
|
| 38 |
foreach ($messages as $key => $message) { |
| 39 |
if (!$message['resolved']) { |
| 40 |
$activeMessages[] = $message; |
| 41 |
|
| 42 |
if (($message['type'] ?? '') === 'info') { |
| 43 |
$messages[$key]['resolved'] = true; |
| 44 |
$needsSave = true; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if ($needsSave) { |
| 50 |
$this->SettingsService->set(Settings::SETTING_CRITICAL_MESSAGES, $messages); |
| 51 |
$this->SettingsService->commit(); |
| 52 |
} |
| 53 |
|
| 54 |
return array_filter($activeMessages, fn($m) => !$m['resolved']); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get first active message (if it needs to show just one) |
| 59 |
*/ |
| 60 |
public function getFirstActiveMessage(): ?array |
| 61 |
{ |
| 62 |
foreach ($this->getActiveMessages() as $message) { |
| 63 |
return $message; |
| 64 |
} |
| 65 |
return null; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Add one new critical message |
| 70 |
*/ |
| 71 |
public function addMessage( |
| 72 |
string $code, |
| 73 |
string $text, |
| 74 |
string $type = 'error', |
| 75 |
?string $link = null, |
| 76 |
?string $link_text = null |
| 77 |
): void { |
| 78 |
$messages = $this->getMessagesData(); |
| 79 |
|
| 80 |
// Check, whether such message exists |
| 81 |
foreach ($messages as $message) { |
| 82 |
if ($message['code'] === $code && !$message['resolved']) { |
| 83 |
return; // already active |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
$messages[] = [ |
| 88 |
'code' => $code, |
| 89 |
'text' => $text, |
| 90 |
'type' => $type, // error | warning | success |
| 91 |
'link' => $link, // optional URL |
| 92 |
'link_text' => $link_text, // optional link text |
| 93 |
'resolved' => false, |
| 94 |
]; |
| 95 |
|
| 96 |
$this->SettingsService->set(Settings::SETTING_CRITICAL_MESSAGES, $messages); |
| 97 |
$this->SettingsService->commit(); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Mark message as resolved |
| 102 |
*/ |
| 103 |
public function resolveMessage(string $code): void |
| 104 |
{ |
| 105 |
$messages = $this->getMessagesData(); |
| 106 |
|
| 107 |
foreach ($messages as &$message) { |
| 108 |
if ($message['code'] === $code) { |
| 109 |
$message['resolved'] = true; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$this->SettingsService->set(Settings::SETTING_CRITICAL_MESSAGES, $messages); |
| 114 |
$this->SettingsService->commit(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Clear all critical messages |
| 119 |
*/ |
| 120 |
public function clear(): void |
| 121 |
{ |
| 122 |
$this->SettingsService->set(Settings::SETTING_CRITICAL_MESSAGES, []); |
| 123 |
$this->SettingsService->commit(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get all messages from settings |
| 128 |
* |
| 129 |
* @return array<int,array{code:string,text:string,type:string,link:?string,resolved:bool}> |
| 130 |
*/ |
| 131 |
private function getMessagesData(): array |
| 132 |
{ |
| 133 |
$messages = $this->SettingsService->get(Settings::SETTING_CRITICAL_MESSAGES, []); |
| 134 |
return is_array($messages) ? $messages : []; |
| 135 |
} |
| 136 |
} |
| 137 |
|