PluginProbe
Metricool – Social media and site statistics / 2.0.2
Metricool – Social media and site statistics v2.0.2
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Features / Notifications / NotificationsController.php

NotificationsController.php in Metricool – Social media and site statistics 2.0.2, at app/Features/Notifications/NotificationsController.php

86 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
14 class NotificationsController implements FeatureInterface
15 {
16 private NotificationsEndpoints $endpoints;
17 private NotificationsService $service;
18 private NotificationListener $listener;
19
20 public function __construct(NotificationsEndpoints $endpoints, NotificationsService $service, NotificationListener $listener)
21 {
22 $this->service = $service;
23 $this->endpoints = $endpoints;
24 $this->listener = $listener;
25 }
26
27 public function register(): void
28 {
29 $this->endpoints->register();
30 $this->listener->listen();
31
32 $this->initiateNotices();
33 add_action('metricool_plugin_version_upgrade', [$this, 'upgradeNotices']);
34 }
35
36 /**
37 * This method returns an array of Notice class-strings that should be added
38 * to the database.
39 *
40 * @internal New Notices should be added here. Upgrade the Notice version if
41 * the Notice should be updated. If a Notice should be removed, remove the
42 * Notice from this list.
43 *
44 * @return array<int,class-string<NoticeInterface>> Array of Notice class-strings
45 */
46 private function getNoticeClassStrings(): array
47 {
48 return [
49 Notices\FirstConnectionNotice::class,
50 ];
51 }
52
53 /**
54 * This method adds the initial Notices to the database if they are not
55 * already present.
56 * @throws \Exception If notice class cannot be instantiated
57 */
58 private function initiateNotices(): void
59 {
60 if ($this->service->hasNotices()) {
61 return;
62 }
63
64 $this->service->addNotices(
65 $this->getNoticeClassStrings()
66 );
67 }
68
69 /**
70 * This method makes sure that if new Notices are added in the update that
71 * these Notices are added in the database. Existing Notices will be updated
72 * if the version is higher than the current existing Notification with the same id.
73 * @throws \Exception If notice class cannot be instantiated
74 */
75 public function upgradeNotices(): void
76 {
77 if ($this->service->hasNotices() === false) {
78 return; // Notices will be added by initiateNotifications()
79 }
80
81 $this->service->upgradeNotices(
82 $this->getNoticeClassStrings()
83 );
84 }
85 }
86