← All changes
|
app/Features/Notifications/NotificationsController.php
+94
-0
2.0.0
→
2.1.1
View file →
| @@ -1,0 +1,94 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Features\Notifications; | |
| 6 | + | |
| 7 | +if (!defined('ABSPATH')) { | |
| 8 | + exit; | |
| 9 | +} | |
| 10 | + | |
| 11 | +use Metricool\Interfaces\FeatureInterface; | |
| 12 | +use Metricool\Interfaces\NoticeInterface; | |
| 13 | +use Metricool\Services\DashboardService; | |
| 14 | +use Metricool\Traits\HasAllowlistControl; | |
| 15 | + | |
| 16 | +class NotificationsController implements FeatureInterface | |
| 17 | +{ | |
| 18 | + use HasAllowlistControl; | |
| 19 | + | |
| 20 | + private NotificationsEndpoints $endpoints; | |
| 21 | + private NotificationsService $service; | |
| 22 | + private NotificationListener $listener; | |
| 23 | + private DashboardService $dashboard; | |
| 24 | + | |
| 25 | + public function __construct(NotificationsEndpoints $endpoints, NotificationsService $service, NotificationListener $listener, DashboardService $dashboard) | |
| 26 | + { | |
| 27 | + $this->service = $service; | |
| 28 | + $this->endpoints = $endpoints; | |
| 29 | + $this->listener = $listener; | |
| 30 | + $this->dashboard = $dashboard; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public function register(): void | |
| 34 | + { | |
| 35 | + $this->endpoints->register(); | |
| 36 | + | |
| 37 | + if ($this->userCanManage() && $this->dashboard->isOnboardingCompleted()) { | |
| 38 | + $this->listener->listen(); | |
| 39 | + $this->initiateNotices(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + add_action('metricool_plugin_version_upgrade', [$this, 'upgradeNotices']); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * This method returns an array of Notice class-strings that should be added | |
| 47 | + * to the database. | |
| 48 | + * | |
| 49 | + * @internal New Notices should be added here. Upgrade the Notice version if | |
| 50 | + * the Notice should be updated. If a Notice should be removed, remove the | |
| 51 | + * Notice from this list. | |
| 52 | + * | |
| 53 | + * @return array<int,class-string<NoticeInterface>> Array of Notice class-strings | |
| 54 | + */ | |
| 55 | + private function getNoticeClassStrings(): array | |
| 56 | + { | |
| 57 | + return [ | |
| 58 | + Notices\FirstConnectionNotice::class, | |
| 59 | + ]; | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * This method adds the initial Notices to the database if they are not | |
| 64 | + * already present. | |
| 65 | + * @throws \Exception If notice class cannot be instantiated | |
| 66 | + */ | |
| 67 | + private function initiateNotices(): void | |
| 68 | + { | |
| 69 | + if ($this->service->hasNotices()) { | |
| 70 | + return; | |
| 71 | + } | |
| 72 | + | |
| 73 | + $this->service->addNotices( | |
| 74 | + $this->getNoticeClassStrings() | |
| 75 | + ); | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * This method makes sure that if new Notices are added in the update that | |
| 80 | + * these Notices are added in the database. Existing Notices will be updated | |
| 81 | + * if the version is higher than the current existing Notification with the same id. | |
| 82 | + * @throws \Exception If notice class cannot be instantiated | |
| 83 | + */ | |
| 84 | + public function upgradeNotices(): void | |
| 85 | + { | |
| 86 | + if ($this->service->hasNotices() === false) { | |
| 87 | + return; // Notices will be added by initiateNotifications() | |
| 88 | + } | |
| 89 | + | |
| 90 | + $this->service->upgradeNotices( | |
| 91 | + $this->getNoticeClassStrings() | |
| 92 | + ); | |
| 93 | + } | |
| 94 | +} | |