@@ -39,320 +39,320 @@ |
||
| 39 | 39 | use OCP\RichObjectStrings\IValidator; |
| 40 | 40 | |
| 41 | 41 | class Manager implements IManager { |
| 42 | - /** @var IValidator */ |
|
| 43 | - protected $validator; |
|
| 44 | - /** @var ILogger */ |
|
| 45 | - protected $logger; |
|
| 46 | - |
|
| 47 | - /** @var IApp[] */ |
|
| 48 | - protected $apps; |
|
| 49 | - /** @var string[] */ |
|
| 50 | - protected $appClasses; |
|
| 51 | - |
|
| 52 | - /** @var INotifier[] */ |
|
| 53 | - protected $notifiers; |
|
| 54 | - /** @var string[] */ |
|
| 55 | - protected $notifierClasses; |
|
| 56 | - |
|
| 57 | - /** @var bool */ |
|
| 58 | - protected $preparingPushNotification; |
|
| 59 | - /** @var bool */ |
|
| 60 | - protected $deferPushing; |
|
| 61 | - |
|
| 62 | - public function __construct(IValidator $validator, |
|
| 63 | - ILogger $logger) { |
|
| 64 | - $this->validator = $validator; |
|
| 65 | - $this->logger = $logger; |
|
| 66 | - $this->apps = []; |
|
| 67 | - $this->notifiers = []; |
|
| 68 | - $this->appClasses = []; |
|
| 69 | - $this->notifierClasses = []; |
|
| 70 | - $this->preparingPushNotification = false; |
|
| 71 | - $this->deferPushing = false; |
|
| 72 | - } |
|
| 73 | - /** |
|
| 74 | - * @param string $appClass The service must implement IApp, otherwise a |
|
| 75 | - * \InvalidArgumentException is thrown later |
|
| 76 | - * @since 17.0.0 |
|
| 77 | - */ |
|
| 78 | - public function registerApp(string $appClass): void { |
|
| 79 | - $this->appClasses[] = $appClass; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @param \Closure $service The service must implement INotifier, otherwise a |
|
| 84 | - * \InvalidArgumentException is thrown later |
|
| 85 | - * @param \Closure $info An array with the keys 'id' and 'name' containing |
|
| 86 | - * the app id and the app name |
|
| 87 | - * @deprecated 17.0.0 use registerNotifierService instead. |
|
| 88 | - * @since 8.2.0 - Parameter $info was added in 9.0.0 |
|
| 89 | - */ |
|
| 90 | - public function registerNotifier(\Closure $service, \Closure $info) { |
|
| 91 | - $infoData = $info(); |
|
| 92 | - $this->logger->logException(new \InvalidArgumentException( |
|
| 93 | - 'Notifier ' . $infoData['name'] . ' (id: ' . $infoData['id'] . ') is not considered because it is using the old way to register.' |
|
| 94 | - )); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @param string $notifierService The service must implement INotifier, otherwise a |
|
| 99 | - * \InvalidArgumentException is thrown later |
|
| 100 | - * @since 17.0.0 |
|
| 101 | - */ |
|
| 102 | - public function registerNotifierService(string $notifierService): void { |
|
| 103 | - $this->notifierClasses[] = $notifierService; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return IApp[] |
|
| 108 | - */ |
|
| 109 | - protected function getApps(): array { |
|
| 110 | - if (empty($this->appClasses)) { |
|
| 111 | - return $this->apps; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - foreach ($this->appClasses as $appClass) { |
|
| 115 | - try { |
|
| 116 | - $app = \OC::$server->query($appClass); |
|
| 117 | - } catch (QueryException $e) { |
|
| 118 | - $this->logger->logException($e, [ |
|
| 119 | - 'message' => 'Failed to load notification app class: ' . $appClass, |
|
| 120 | - 'app' => 'notifications', |
|
| 121 | - ]); |
|
| 122 | - continue; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if (!($app instanceof IApp)) { |
|
| 126 | - $this->logger->error('Notification app class ' . $appClass . ' is not implementing ' . IApp::class, [ |
|
| 127 | - 'app' => 'notifications', |
|
| 128 | - ]); |
|
| 129 | - continue; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - $this->apps[] = $app; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $this->appClasses = []; |
|
| 136 | - |
|
| 137 | - return $this->apps; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return INotifier[] |
|
| 142 | - */ |
|
| 143 | - public function getNotifiers(): array { |
|
| 144 | - if (empty($this->notifierClasses)) { |
|
| 145 | - return $this->notifiers; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - foreach ($this->notifierClasses as $notifierClass) { |
|
| 149 | - try { |
|
| 150 | - $notifier = \OC::$server->query($notifierClass); |
|
| 151 | - } catch (QueryException $e) { |
|
| 152 | - $this->logger->logException($e, [ |
|
| 153 | - 'message' => 'Failed to load notification notifier class: ' . $notifierClass, |
|
| 154 | - 'app' => 'notifications', |
|
| 155 | - ]); |
|
| 156 | - continue; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if (!($notifier instanceof INotifier)) { |
|
| 160 | - $this->logger->error('Notification notifier class ' . $notifierClass . ' is not implementing ' . INotifier::class, [ |
|
| 161 | - 'app' => 'notifications', |
|
| 162 | - ]); |
|
| 163 | - continue; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $this->notifiers[] = $notifier; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - $this->notifierClasses = []; |
|
| 170 | - |
|
| 171 | - return $this->notifiers; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @return INotification |
|
| 176 | - * @since 8.2.0 |
|
| 177 | - */ |
|
| 178 | - public function createNotification(): INotification { |
|
| 179 | - return new Notification($this->validator); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @return bool |
|
| 184 | - * @since 8.2.0 |
|
| 185 | - */ |
|
| 186 | - public function hasNotifiers(): bool { |
|
| 187 | - return !empty($this->notifiers) || !empty($this->notifierClasses); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @param bool $preparingPushNotification |
|
| 192 | - * @since 14.0.0 |
|
| 193 | - */ |
|
| 194 | - public function setPreparingPushNotification(bool $preparingPushNotification): void { |
|
| 195 | - $this->preparingPushNotification = $preparingPushNotification; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * @return bool |
|
| 200 | - * @since 14.0.0 |
|
| 201 | - */ |
|
| 202 | - public function isPreparingPushNotification(): bool { |
|
| 203 | - return $this->preparingPushNotification; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * The calling app should only "flush" when it got returned true on the defer call |
|
| 208 | - * @return bool |
|
| 209 | - * @since 20.0.0 |
|
| 210 | - */ |
|
| 211 | - public function defer(): bool { |
|
| 212 | - $alreadyDeferring = $this->deferPushing; |
|
| 213 | - $this->deferPushing = true; |
|
| 214 | - |
|
| 215 | - $apps = $this->getApps(); |
|
| 216 | - |
|
| 217 | - foreach ($apps as $app) { |
|
| 218 | - if ($app instanceof IDeferrableApp) { |
|
| 219 | - $app->defer(); |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return !$alreadyDeferring; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @since 20.0.0 |
|
| 228 | - */ |
|
| 229 | - public function flush(): void { |
|
| 230 | - $apps = $this->getApps(); |
|
| 231 | - |
|
| 232 | - foreach ($apps as $app) { |
|
| 233 | - if (!$app instanceof IDeferrableApp) { |
|
| 234 | - continue; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - try { |
|
| 238 | - $app->flush(); |
|
| 239 | - } catch (\InvalidArgumentException $e) { |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - $this->deferPushing = false; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param INotification $notification |
|
| 248 | - * @throws \InvalidArgumentException When the notification is not valid |
|
| 249 | - * @since 8.2.0 |
|
| 250 | - */ |
|
| 251 | - public function notify(INotification $notification): void { |
|
| 252 | - if (!$notification->isValid()) { |
|
| 253 | - throw new \InvalidArgumentException('The given notification is invalid'); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - $apps = $this->getApps(); |
|
| 257 | - |
|
| 258 | - foreach ($apps as $app) { |
|
| 259 | - try { |
|
| 260 | - $app->notify($notification); |
|
| 261 | - } catch (\InvalidArgumentException $e) { |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Identifier of the notifier, only use [a-z0-9_] |
|
| 268 | - * |
|
| 269 | - * @return string |
|
| 270 | - * @since 17.0.0 |
|
| 271 | - */ |
|
| 272 | - public function getID(): string { |
|
| 273 | - return 'core'; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Human readable name describing the notifier |
|
| 278 | - * |
|
| 279 | - * @return string |
|
| 280 | - * @since 17.0.0 |
|
| 281 | - */ |
|
| 282 | - public function getName(): string { |
|
| 283 | - return 'core'; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @param INotification $notification |
|
| 288 | - * @param string $languageCode The code of the language that should be used to prepare the notification |
|
| 289 | - * @return INotification |
|
| 290 | - * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
| 291 | - * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted |
|
| 292 | - * @since 8.2.0 |
|
| 293 | - */ |
|
| 294 | - public function prepare(INotification $notification, string $languageCode): INotification { |
|
| 295 | - $notifiers = $this->getNotifiers(); |
|
| 296 | - |
|
| 297 | - foreach ($notifiers as $notifier) { |
|
| 298 | - try { |
|
| 299 | - $notification = $notifier->prepare($notification, $languageCode); |
|
| 300 | - } catch (\InvalidArgumentException $e) { |
|
| 301 | - continue; |
|
| 302 | - } catch (AlreadyProcessedException $e) { |
|
| 303 | - $this->markProcessed($notification); |
|
| 304 | - throw new \InvalidArgumentException('The given notification has been processed'); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - if (!($notification instanceof INotification) || !$notification->isValidParsed()) { |
|
| 308 | - throw new \InvalidArgumentException('The given notification has not been handled'); |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - if (!($notification instanceof INotification) || !$notification->isValidParsed()) { |
|
| 313 | - throw new \InvalidArgumentException('The given notification has not been handled'); |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - return $notification; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * @param INotification $notification |
|
| 321 | - */ |
|
| 322 | - public function markProcessed(INotification $notification): void { |
|
| 323 | - $apps = $this->getApps(); |
|
| 324 | - |
|
| 325 | - foreach ($apps as $app) { |
|
| 326 | - $app->markProcessed($notification); |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * @param INotification $notification |
|
| 332 | - * @return int |
|
| 333 | - */ |
|
| 334 | - public function getCount(INotification $notification): int { |
|
| 335 | - $apps = $this->getApps(); |
|
| 336 | - |
|
| 337 | - $count = 0; |
|
| 338 | - foreach ($apps as $app) { |
|
| 339 | - $count += $app->getCount($notification); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return $count; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - public function dismissNotification(INotification $notification): void { |
|
| 346 | - $notifiers = $this->getNotifiers(); |
|
| 347 | - |
|
| 348 | - foreach ($notifiers as $notifier) { |
|
| 349 | - if ($notifier instanceof IDismissableNotifier) { |
|
| 350 | - try { |
|
| 351 | - $notifier->dismissNotification($notification); |
|
| 352 | - } catch (\InvalidArgumentException $e) { |
|
| 353 | - continue; |
|
| 354 | - } |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - } |
|
| 42 | + /** @var IValidator */ |
|
| 43 | + protected $validator; |
|
| 44 | + /** @var ILogger */ |
|
| 45 | + protected $logger; |
|
| 46 | + |
|
| 47 | + /** @var IApp[] */ |
|
| 48 | + protected $apps; |
|
| 49 | + /** @var string[] */ |
|
| 50 | + protected $appClasses; |
|
| 51 | + |
|
| 52 | + /** @var INotifier[] */ |
|
| 53 | + protected $notifiers; |
|
| 54 | + /** @var string[] */ |
|
| 55 | + protected $notifierClasses; |
|
| 56 | + |
|
| 57 | + /** @var bool */ |
|
| 58 | + protected $preparingPushNotification; |
|
| 59 | + /** @var bool */ |
|
| 60 | + protected $deferPushing; |
|
| 61 | + |
|
| 62 | + public function __construct(IValidator $validator, |
|
| 63 | + ILogger $logger) { |
|
| 64 | + $this->validator = $validator; |
|
| 65 | + $this->logger = $logger; |
|
| 66 | + $this->apps = []; |
|
| 67 | + $this->notifiers = []; |
|
| 68 | + $this->appClasses = []; |
|
| 69 | + $this->notifierClasses = []; |
|
| 70 | + $this->preparingPushNotification = false; |
|
| 71 | + $this->deferPushing = false; |
|
| 72 | + } |
|
| 73 | + /** |
|
| 74 | + * @param string $appClass The service must implement IApp, otherwise a |
|
| 75 | + * \InvalidArgumentException is thrown later |
|
| 76 | + * @since 17.0.0 |
|
| 77 | + */ |
|
| 78 | + public function registerApp(string $appClass): void { |
|
| 79 | + $this->appClasses[] = $appClass; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @param \Closure $service The service must implement INotifier, otherwise a |
|
| 84 | + * \InvalidArgumentException is thrown later |
|
| 85 | + * @param \Closure $info An array with the keys 'id' and 'name' containing |
|
| 86 | + * the app id and the app name |
|
| 87 | + * @deprecated 17.0.0 use registerNotifierService instead. |
|
| 88 | + * @since 8.2.0 - Parameter $info was added in 9.0.0 |
|
| 89 | + */ |
|
| 90 | + public function registerNotifier(\Closure $service, \Closure $info) { |
|
| 91 | + $infoData = $info(); |
|
| 92 | + $this->logger->logException(new \InvalidArgumentException( |
|
| 93 | + 'Notifier ' . $infoData['name'] . ' (id: ' . $infoData['id'] . ') is not considered because it is using the old way to register.' |
|
| 94 | + )); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @param string $notifierService The service must implement INotifier, otherwise a |
|
| 99 | + * \InvalidArgumentException is thrown later |
|
| 100 | + * @since 17.0.0 |
|
| 101 | + */ |
|
| 102 | + public function registerNotifierService(string $notifierService): void { |
|
| 103 | + $this->notifierClasses[] = $notifierService; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return IApp[] |
|
| 108 | + */ |
|
| 109 | + protected function getApps(): array { |
|
| 110 | + if (empty($this->appClasses)) { |
|
| 111 | + return $this->apps; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + foreach ($this->appClasses as $appClass) { |
|
| 115 | + try { |
|
| 116 | + $app = \OC::$server->query($appClass); |
|
| 117 | + } catch (QueryException $e) { |
|
| 118 | + $this->logger->logException($e, [ |
|
| 119 | + 'message' => 'Failed to load notification app class: ' . $appClass, |
|
| 120 | + 'app' => 'notifications', |
|
| 121 | + ]); |
|
| 122 | + continue; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if (!($app instanceof IApp)) { |
|
| 126 | + $this->logger->error('Notification app class ' . $appClass . ' is not implementing ' . IApp::class, [ |
|
| 127 | + 'app' => 'notifications', |
|
| 128 | + ]); |
|
| 129 | + continue; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $this->apps[] = $app; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $this->appClasses = []; |
|
| 136 | + |
|
| 137 | + return $this->apps; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return INotifier[] |
|
| 142 | + */ |
|
| 143 | + public function getNotifiers(): array { |
|
| 144 | + if (empty($this->notifierClasses)) { |
|
| 145 | + return $this->notifiers; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + foreach ($this->notifierClasses as $notifierClass) { |
|
| 149 | + try { |
|
| 150 | + $notifier = \OC::$server->query($notifierClass); |
|
| 151 | + } catch (QueryException $e) { |
|
| 152 | + $this->logger->logException($e, [ |
|
| 153 | + 'message' => 'Failed to load notification notifier class: ' . $notifierClass, |
|
| 154 | + 'app' => 'notifications', |
|
| 155 | + ]); |
|
| 156 | + continue; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if (!($notifier instanceof INotifier)) { |
|
| 160 | + $this->logger->error('Notification notifier class ' . $notifierClass . ' is not implementing ' . INotifier::class, [ |
|
| 161 | + 'app' => 'notifications', |
|
| 162 | + ]); |
|
| 163 | + continue; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $this->notifiers[] = $notifier; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + $this->notifierClasses = []; |
|
| 170 | + |
|
| 171 | + return $this->notifiers; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @return INotification |
|
| 176 | + * @since 8.2.0 |
|
| 177 | + */ |
|
| 178 | + public function createNotification(): INotification { |
|
| 179 | + return new Notification($this->validator); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @return bool |
|
| 184 | + * @since 8.2.0 |
|
| 185 | + */ |
|
| 186 | + public function hasNotifiers(): bool { |
|
| 187 | + return !empty($this->notifiers) || !empty($this->notifierClasses); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @param bool $preparingPushNotification |
|
| 192 | + * @since 14.0.0 |
|
| 193 | + */ |
|
| 194 | + public function setPreparingPushNotification(bool $preparingPushNotification): void { |
|
| 195 | + $this->preparingPushNotification = $preparingPushNotification; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * @return bool |
|
| 200 | + * @since 14.0.0 |
|
| 201 | + */ |
|
| 202 | + public function isPreparingPushNotification(): bool { |
|
| 203 | + return $this->preparingPushNotification; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * The calling app should only "flush" when it got returned true on the defer call |
|
| 208 | + * @return bool |
|
| 209 | + * @since 20.0.0 |
|
| 210 | + */ |
|
| 211 | + public function defer(): bool { |
|
| 212 | + $alreadyDeferring = $this->deferPushing; |
|
| 213 | + $this->deferPushing = true; |
|
| 214 | + |
|
| 215 | + $apps = $this->getApps(); |
|
| 216 | + |
|
| 217 | + foreach ($apps as $app) { |
|
| 218 | + if ($app instanceof IDeferrableApp) { |
|
| 219 | + $app->defer(); |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return !$alreadyDeferring; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @since 20.0.0 |
|
| 228 | + */ |
|
| 229 | + public function flush(): void { |
|
| 230 | + $apps = $this->getApps(); |
|
| 231 | + |
|
| 232 | + foreach ($apps as $app) { |
|
| 233 | + if (!$app instanceof IDeferrableApp) { |
|
| 234 | + continue; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + try { |
|
| 238 | + $app->flush(); |
|
| 239 | + } catch (\InvalidArgumentException $e) { |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + $this->deferPushing = false; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param INotification $notification |
|
| 248 | + * @throws \InvalidArgumentException When the notification is not valid |
|
| 249 | + * @since 8.2.0 |
|
| 250 | + */ |
|
| 251 | + public function notify(INotification $notification): void { |
|
| 252 | + if (!$notification->isValid()) { |
|
| 253 | + throw new \InvalidArgumentException('The given notification is invalid'); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + $apps = $this->getApps(); |
|
| 257 | + |
|
| 258 | + foreach ($apps as $app) { |
|
| 259 | + try { |
|
| 260 | + $app->notify($notification); |
|
| 261 | + } catch (\InvalidArgumentException $e) { |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Identifier of the notifier, only use [a-z0-9_] |
|
| 268 | + * |
|
| 269 | + * @return string |
|
| 270 | + * @since 17.0.0 |
|
| 271 | + */ |
|
| 272 | + public function getID(): string { |
|
| 273 | + return 'core'; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Human readable name describing the notifier |
|
| 278 | + * |
|
| 279 | + * @return string |
|
| 280 | + * @since 17.0.0 |
|
| 281 | + */ |
|
| 282 | + public function getName(): string { |
|
| 283 | + return 'core'; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @param INotification $notification |
|
| 288 | + * @param string $languageCode The code of the language that should be used to prepare the notification |
|
| 289 | + * @return INotification |
|
| 290 | + * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
| 291 | + * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted |
|
| 292 | + * @since 8.2.0 |
|
| 293 | + */ |
|
| 294 | + public function prepare(INotification $notification, string $languageCode): INotification { |
|
| 295 | + $notifiers = $this->getNotifiers(); |
|
| 296 | + |
|
| 297 | + foreach ($notifiers as $notifier) { |
|
| 298 | + try { |
|
| 299 | + $notification = $notifier->prepare($notification, $languageCode); |
|
| 300 | + } catch (\InvalidArgumentException $e) { |
|
| 301 | + continue; |
|
| 302 | + } catch (AlreadyProcessedException $e) { |
|
| 303 | + $this->markProcessed($notification); |
|
| 304 | + throw new \InvalidArgumentException('The given notification has been processed'); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + if (!($notification instanceof INotification) || !$notification->isValidParsed()) { |
|
| 308 | + throw new \InvalidArgumentException('The given notification has not been handled'); |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + if (!($notification instanceof INotification) || !$notification->isValidParsed()) { |
|
| 313 | + throw new \InvalidArgumentException('The given notification has not been handled'); |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + return $notification; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * @param INotification $notification |
|
| 321 | + */ |
|
| 322 | + public function markProcessed(INotification $notification): void { |
|
| 323 | + $apps = $this->getApps(); |
|
| 324 | + |
|
| 325 | + foreach ($apps as $app) { |
|
| 326 | + $app->markProcessed($notification); |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * @param INotification $notification |
|
| 332 | + * @return int |
|
| 333 | + */ |
|
| 334 | + public function getCount(INotification $notification): int { |
|
| 335 | + $apps = $this->getApps(); |
|
| 336 | + |
|
| 337 | + $count = 0; |
|
| 338 | + foreach ($apps as $app) { |
|
| 339 | + $count += $app->getCount($notification); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return $count; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + public function dismissNotification(INotification $notification): void { |
|
| 346 | + $notifiers = $this->getNotifiers(); |
|
| 347 | + |
|
| 348 | + foreach ($notifiers as $notifier) { |
|
| 349 | + if ($notifier instanceof IDismissableNotifier) { |
|
| 350 | + try { |
|
| 351 | + $notifier->dismissNotification($notification); |
|
| 352 | + } catch (\InvalidArgumentException $e) { |
|
| 353 | + continue; |
|
| 354 | + } |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | 358 | } |
@@ -32,17 +32,17 @@ |
||
| 32 | 32 | * @since 20.0.0 |
| 33 | 33 | */ |
| 34 | 34 | interface IDeferrableApp extends IApp { |
| 35 | - /** |
|
| 36 | - * Start deferring notifications until `flush()` is called |
|
| 37 | - * |
|
| 38 | - * @since 20.0.0 |
|
| 39 | - */ |
|
| 40 | - public function defer(): void; |
|
| 35 | + /** |
|
| 36 | + * Start deferring notifications until `flush()` is called |
|
| 37 | + * |
|
| 38 | + * @since 20.0.0 |
|
| 39 | + */ |
|
| 40 | + public function defer(): void; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Send all deferred notifications that have been stored since `defer()` was called |
|
| 44 | - * |
|
| 45 | - * @since 20.0.0 |
|
| 46 | - */ |
|
| 47 | - public function flush(): void; |
|
| 42 | + /** |
|
| 43 | + * Send all deferred notifications that have been stored since `defer()` was called |
|
| 44 | + * |
|
| 45 | + * @since 20.0.0 |
|
| 46 | + */ |
|
| 47 | + public function flush(): void; |
|
| 48 | 48 | } |
@@ -33,79 +33,79 @@ |
||
| 33 | 33 | * @since 9.0.0 |
| 34 | 34 | */ |
| 35 | 35 | interface IManager extends IApp, INotifier { |
| 36 | - /** |
|
| 37 | - * @param string $appClass The service must implement IApp, otherwise a |
|
| 38 | - * \InvalidArgumentException is thrown later |
|
| 39 | - * @since 17.0.0 |
|
| 40 | - */ |
|
| 41 | - public function registerApp(string $appClass): void; |
|
| 36 | + /** |
|
| 37 | + * @param string $appClass The service must implement IApp, otherwise a |
|
| 38 | + * \InvalidArgumentException is thrown later |
|
| 39 | + * @since 17.0.0 |
|
| 40 | + */ |
|
| 41 | + public function registerApp(string $appClass): void; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param \Closure $service The service must implement INotifier, otherwise a |
|
| 45 | - * \InvalidArgumentException is thrown later |
|
| 46 | - * @param \Closure $info An array with the keys 'id' and 'name' containing |
|
| 47 | - * the app id and the app name |
|
| 48 | - * @deprecated 17.0.0 use registerNotifierService instead. |
|
| 49 | - * @since 8.2.0 - Parameter $info was added in 9.0.0 |
|
| 50 | - */ |
|
| 51 | - public function registerNotifier(\Closure $service, \Closure $info); |
|
| 43 | + /** |
|
| 44 | + * @param \Closure $service The service must implement INotifier, otherwise a |
|
| 45 | + * \InvalidArgumentException is thrown later |
|
| 46 | + * @param \Closure $info An array with the keys 'id' and 'name' containing |
|
| 47 | + * the app id and the app name |
|
| 48 | + * @deprecated 17.0.0 use registerNotifierService instead. |
|
| 49 | + * @since 8.2.0 - Parameter $info was added in 9.0.0 |
|
| 50 | + */ |
|
| 51 | + public function registerNotifier(\Closure $service, \Closure $info); |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @param string $notifierService The service must implement INotifier, otherwise a |
|
| 55 | - * \InvalidArgumentException is thrown later |
|
| 56 | - * @since 17.0.0 |
|
| 57 | - */ |
|
| 58 | - public function registerNotifierService(string $notifierService): void; |
|
| 53 | + /** |
|
| 54 | + * @param string $notifierService The service must implement INotifier, otherwise a |
|
| 55 | + * \InvalidArgumentException is thrown later |
|
| 56 | + * @since 17.0.0 |
|
| 57 | + */ |
|
| 58 | + public function registerNotifierService(string $notifierService): void; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @return INotifier[] |
|
| 62 | - * @since 9.0.0 |
|
| 63 | - */ |
|
| 64 | - public function getNotifiers(): array; |
|
| 60 | + /** |
|
| 61 | + * @return INotifier[] |
|
| 62 | + * @since 9.0.0 |
|
| 63 | + */ |
|
| 64 | + public function getNotifiers(): array; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @return INotification |
|
| 68 | - * @since 9.0.0 |
|
| 69 | - */ |
|
| 70 | - public function createNotification(): INotification; |
|
| 66 | + /** |
|
| 67 | + * @return INotification |
|
| 68 | + * @since 9.0.0 |
|
| 69 | + */ |
|
| 70 | + public function createNotification(): INotification; |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @return bool |
|
| 74 | - * @since 9.0.0 |
|
| 75 | - */ |
|
| 76 | - public function hasNotifiers(): bool; |
|
| 72 | + /** |
|
| 73 | + * @return bool |
|
| 74 | + * @since 9.0.0 |
|
| 75 | + */ |
|
| 76 | + public function hasNotifiers(): bool; |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @param bool $preparingPushNotification |
|
| 80 | - * @since 14.0.0 |
|
| 81 | - */ |
|
| 82 | - public function setPreparingPushNotification(bool $preparingPushNotification): void; |
|
| 78 | + /** |
|
| 79 | + * @param bool $preparingPushNotification |
|
| 80 | + * @since 14.0.0 |
|
| 81 | + */ |
|
| 82 | + public function setPreparingPushNotification(bool $preparingPushNotification): void; |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * @return bool |
|
| 86 | - * @since 14.0.0 |
|
| 87 | - */ |
|
| 88 | - public function isPreparingPushNotification(): bool; |
|
| 84 | + /** |
|
| 85 | + * @return bool |
|
| 86 | + * @since 14.0.0 |
|
| 87 | + */ |
|
| 88 | + public function isPreparingPushNotification(): bool; |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * @since 18.0.0 |
|
| 92 | - */ |
|
| 93 | - public function dismissNotification(INotification $notification): void; |
|
| 90 | + /** |
|
| 91 | + * @since 18.0.0 |
|
| 92 | + */ |
|
| 93 | + public function dismissNotification(INotification $notification): void; |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Start deferring notifications until `flush()` is called |
|
| 97 | - * |
|
| 98 | - * The calling app should only "flush" when it got returned true on the defer call, |
|
| 99 | - * otherwise another app is deferring the sending already. |
|
| 100 | - * @return bool |
|
| 101 | - * @since 20.0.0 |
|
| 102 | - */ |
|
| 103 | - public function defer(): bool; |
|
| 95 | + /** |
|
| 96 | + * Start deferring notifications until `flush()` is called |
|
| 97 | + * |
|
| 98 | + * The calling app should only "flush" when it got returned true on the defer call, |
|
| 99 | + * otherwise another app is deferring the sending already. |
|
| 100 | + * @return bool |
|
| 101 | + * @since 20.0.0 |
|
| 102 | + */ |
|
| 103 | + public function defer(): bool; |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Send all deferred notifications that have been stored since `defer()` was called |
|
| 107 | - * |
|
| 108 | - * @since 20.0.0 |
|
| 109 | - */ |
|
| 110 | - public function flush(): void; |
|
| 105 | + /** |
|
| 106 | + * Send all deferred notifications that have been stored since `defer()` was called |
|
| 107 | + * |
|
| 108 | + * @since 20.0.0 |
|
| 109 | + */ |
|
| 110 | + public function flush(): void; |
|
| 111 | 111 | } |