PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
← All changes | app/Features/Notifications/NotificationsService.php +126 -0 2.0.1 → 2.1.1 View file →
@@ -1,0 +1,126 @@
1 +<?php
2 +
3 +declare(strict_types=1);
4 +
5 +namespace Metricool\Features\Notifications;
6 +
7 +use Metricool\Bootstrap\App;
8 +use Metricool\Interfaces\NoticeInterface;
9 +
10 +class NotificationsService
11 +{
12 + private NotificationsRepository $repository;
13 +
14 + public function __construct(NotificationsRepository $repository)
15 + {
16 + $this->repository = $repository;
17 + }
18 +
19 + /**
20 + * Check if there are Notices
21 + */
22 + public function hasNotices(): bool
23 + {
24 + return !empty($this->getAllNotices());
25 + }
26 +
27 + /**
28 + * Get all Notices
29 + * @return NoticeInterface[]
30 + */
31 + public function getAllNotices(): array
32 + {
33 + return $this->repository->getAllNotices();
34 + }
35 +
36 + /**
37 + * Add multiple Notices at once
38 + * @param class-string<NoticeInterface>[] $notices
39 + * @throws \Exception If notice class cannot be instantiated
40 + */
41 + public function addNotices(array $notices): void
42 + {
43 + foreach ($notices as $noticeClassString) {
44 + $notice = App::getInstance()->make($noticeClassString);
45 + $this->repository->addNotice($notice, false);
46 + }
47 + $this->repository->saveNoticesToDatabase();
48 + }
49 +
50 + /**
51 + * Upgrade the Notices. Only replace existing Notices with same identifier
52 + * if the version is lower than the new Notice version. Add missing Notices
53 + * and remove Notices that are no longer present.
54 + * @param class-string<NoticeInterface>[] $notices
55 + * @throws \Exception If notice class cannot be instantiated
56 + */
57 + public function upgradeNotices(array $notices): void
58 + {
59 + // Remove Notices that are no longer present. Maybe that are them all?
60 + $deletableNoticeList = $this->repository->getAllNotices();
61 +
62 + foreach ($notices as $noticeClassString) {
63 + $notice = App::getInstance()->make($noticeClassString);
64 +
65 + $this->repository->upgradeNotice($notice, false);
66 +
67 + // Current Notices is not deletable so remove it from the list
68 + unset($deletableNoticeList[$notice->getId()]);
69 + }
70 +
71 + // If list still contains Notices, the upgrade requests them to be
72 + // removed
73 + if (!empty($deletableNoticeList)) {
74 + $this->removeDeletableNoticesAfterUpgrade($deletableNoticeList, false);
75 + }
76 +
77 + $this->repository->saveNoticesToDatabase();
78 + }
79 +
80 + /**
81 + * Remove Notices that are no longer present in our Notice Object list. Such
82 + * Notices are now a __PHP_Incomplete_Class and do not implement the
83 + * NoticeInterface. Because of this we cannot use the Notice classes.
84 + */
85 + private function removeDeletableNoticesAfterUpgrade(array $deletableNoticesList, bool $save = true): void
86 + {
87 + foreach ($deletableNoticesList as $noticeId => $deletedNotice) {
88 + $this->repository->removeNoticeById($noticeId, $save);
89 + }
90 +
91 + if ($save) {
92 + $this->repository->saveNoticesToDatabase();
93 + }
94 + }
95 +
96 + /**
97 + * Remove multiple Notices at once
98 + * @param NoticeInterface[] $notices
99 + */
100 + public function removeNotices(array $notices, bool $save = true): void
101 + {
102 + foreach ($notices as $notice) {
103 + $this->repository->removeNotice($notice, $save);
104 + }
105 +
106 + if ($save) {
107 + $this->repository->saveNoticesToDatabase();
108 + }
109 + }
110 +
111 + /**
112 + * Activate a task by default.
113 + */
114 + public function activate(string $noticeID): void
115 + {
116 + $this->repository->toggleNoticeServerSide($noticeID, true);
117 + }
118 +
119 + /**
120 + * Deactivate a task by default.
121 + */
122 + public function deactivate(string $noticeID): void
123 + {
124 + $this->repository->toggleNoticeServerSide($noticeID, false);
125 + }
126 +}