Passed
Push — master ( 4d0403...c21d3c )
by John
15:59 queued 14s
created
apps/updatenotification/lib/Notification/Notifier.php 2 patches
Indentation   +163 added lines, -163 removed lines patch added patch discarded remove patch
@@ -39,167 +39,167 @@
 block discarded – undo
39 39
 use OCP\Util;
40 40
 
41 41
 class Notifier implements INotifier {
42
-	/** @var IURLGenerator */
43
-	protected $url;
44
-
45
-	/** @var IConfig */
46
-	protected $config;
47
-
48
-	/** @var IManager */
49
-	protected $notificationManager;
50
-
51
-	/** @var IFactory */
52
-	protected $l10NFactory;
53
-
54
-	/** @var IUserSession */
55
-	protected $userSession;
56
-
57
-	/** @var IGroupManager */
58
-	protected $groupManager;
59
-
60
-	/** @var string[] */
61
-	protected $appVersions;
62
-
63
-	/**
64
-	 * Notifier constructor.
65
-	 *
66
-	 * @param IURLGenerator $url
67
-	 * @param IConfig $config
68
-	 * @param IManager $notificationManager
69
-	 * @param IFactory $l10NFactory
70
-	 * @param IUserSession $userSession
71
-	 * @param IGroupManager $groupManager
72
-	 */
73
-	public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) {
74
-		$this->url = $url;
75
-		$this->notificationManager = $notificationManager;
76
-		$this->config = $config;
77
-		$this->l10NFactory = $l10NFactory;
78
-		$this->userSession = $userSession;
79
-		$this->groupManager = $groupManager;
80
-		$this->appVersions = $this->getAppVersions();
81
-	}
82
-
83
-	/**
84
-	 * Identifier of the notifier, only use [a-z0-9_]
85
-	 *
86
-	 * @return string
87
-	 * @since 17.0.0
88
-	 */
89
-	public function getID(): string {
90
-		return 'updatenotification';
91
-	}
92
-
93
-	/**
94
-	 * Human readable name describing the notifier
95
-	 *
96
-	 * @return string
97
-	 * @since 17.0.0
98
-	 */
99
-	public function getName(): string {
100
-		return $this->l10NFactory->get('updatenotification')->t('Update notifications');
101
-	}
102
-
103
-	/**
104
-	 * @param INotification $notification
105
-	 * @param string $languageCode The code of the language that should be used to prepare the notification
106
-	 * @return INotification
107
-	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
108
-	 * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
109
-	 * @since 9.0.0
110
-	 */
111
-	public function prepare(INotification $notification, string $languageCode): INotification {
112
-		if ($notification->getApp() !== 'updatenotification') {
113
-			throw new \InvalidArgumentException('Unknown app id');
114
-		}
115
-
116
-		$l = $this->l10NFactory->get('updatenotification', $languageCode);
117
-		if ($notification->getSubject() === 'connection_error') {
118
-			$errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0);
119
-			if ($errors === 0) {
120
-				$this->notificationManager->markProcessed($notification);
121
-				throw new \InvalidArgumentException('Update checked worked again');
122
-			}
123
-
124
-			$notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors]))
125
-				->setParsedMessage($l->t('Please check the Nextcloud and server log files for errors.'));
126
-		} elseif ($notification->getObjectType() === 'core') {
127
-			$this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions());
128
-
129
-			$parameters = $notification->getSubjectParameters();
130
-			$notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']]))
131
-				->setRichSubject($l->t('Update to {serverAndVersion} is available.'), [
132
-					'serverAndVersion' => [
133
-						'type' => 'highlight',
134
-						'id' => $notification->getObjectType(),
135
-						'name' => $parameters['version'],
136
-					]
137
-				]);
138
-
139
-			if ($this->isAdmin()) {
140
-				$notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version');
141
-			}
142
-		} else {
143
-			$appInfo = $this->getAppInfo($notification->getObjectType(), $languageCode);
144
-			$appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name'];
145
-
146
-			if (isset($this->appVersions[$notification->getObjectType()])) {
147
-				$this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]);
148
-			}
149
-
150
-			$notification->setRichSubject($l->t('Update for {app} to version %s is available.', [$notification->getObjectId()]), [
151
-				'app' => [
152
-					'type' => 'app',
153
-					'id' => $notification->getObjectType(),
154
-					'name' => $appName,
155
-				]
156
-			]);
157
-
158
-			if ($this->isAdmin()) {
159
-				$notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType());
160
-			}
161
-		}
162
-
163
-		$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg')));
164
-
165
-		return $notification;
166
-	}
167
-
168
-	/**
169
-	 * Remove the notification and prevent rendering, when the update is installed
170
-	 *
171
-	 * @param INotification $notification
172
-	 * @param string $installedVersion
173
-	 * @throws AlreadyProcessedException When the update is already installed
174
-	 */
175
-	protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
176
-		if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
177
-			throw new AlreadyProcessedException();
178
-		}
179
-	}
180
-
181
-	/**
182
-	 * @return bool
183
-	 */
184
-	protected function isAdmin(): bool {
185
-		$user = $this->userSession->getUser();
186
-
187
-		if ($user instanceof IUser) {
188
-			return $this->groupManager->isAdmin($user->getUID());
189
-		}
190
-
191
-		return false;
192
-	}
193
-
194
-	protected function getCoreVersions(): string {
195
-		return implode('.', Util::getVersion());
196
-	}
197
-
198
-	protected function getAppVersions(): array {
199
-		return \OC_App::getAppVersions();
200
-	}
201
-
202
-	protected function getAppInfo($appId, $languageCode) {
203
-		return \OC_App::getAppInfo($appId, false, $languageCode);
204
-	}
42
+    /** @var IURLGenerator */
43
+    protected $url;
44
+
45
+    /** @var IConfig */
46
+    protected $config;
47
+
48
+    /** @var IManager */
49
+    protected $notificationManager;
50
+
51
+    /** @var IFactory */
52
+    protected $l10NFactory;
53
+
54
+    /** @var IUserSession */
55
+    protected $userSession;
56
+
57
+    /** @var IGroupManager */
58
+    protected $groupManager;
59
+
60
+    /** @var string[] */
61
+    protected $appVersions;
62
+
63
+    /**
64
+     * Notifier constructor.
65
+     *
66
+     * @param IURLGenerator $url
67
+     * @param IConfig $config
68
+     * @param IManager $notificationManager
69
+     * @param IFactory $l10NFactory
70
+     * @param IUserSession $userSession
71
+     * @param IGroupManager $groupManager
72
+     */
73
+    public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) {
74
+        $this->url = $url;
75
+        $this->notificationManager = $notificationManager;
76
+        $this->config = $config;
77
+        $this->l10NFactory = $l10NFactory;
78
+        $this->userSession = $userSession;
79
+        $this->groupManager = $groupManager;
80
+        $this->appVersions = $this->getAppVersions();
81
+    }
82
+
83
+    /**
84
+     * Identifier of the notifier, only use [a-z0-9_]
85
+     *
86
+     * @return string
87
+     * @since 17.0.0
88
+     */
89
+    public function getID(): string {
90
+        return 'updatenotification';
91
+    }
92
+
93
+    /**
94
+     * Human readable name describing the notifier
95
+     *
96
+     * @return string
97
+     * @since 17.0.0
98
+     */
99
+    public function getName(): string {
100
+        return $this->l10NFactory->get('updatenotification')->t('Update notifications');
101
+    }
102
+
103
+    /**
104
+     * @param INotification $notification
105
+     * @param string $languageCode The code of the language that should be used to prepare the notification
106
+     * @return INotification
107
+     * @throws \InvalidArgumentException When the notification was not prepared by a notifier
108
+     * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
109
+     * @since 9.0.0
110
+     */
111
+    public function prepare(INotification $notification, string $languageCode): INotification {
112
+        if ($notification->getApp() !== 'updatenotification') {
113
+            throw new \InvalidArgumentException('Unknown app id');
114
+        }
115
+
116
+        $l = $this->l10NFactory->get('updatenotification', $languageCode);
117
+        if ($notification->getSubject() === 'connection_error') {
118
+            $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0);
119
+            if ($errors === 0) {
120
+                $this->notificationManager->markProcessed($notification);
121
+                throw new \InvalidArgumentException('Update checked worked again');
122
+            }
123
+
124
+            $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors]))
125
+                ->setParsedMessage($l->t('Please check the Nextcloud and server log files for errors.'));
126
+        } elseif ($notification->getObjectType() === 'core') {
127
+            $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions());
128
+
129
+            $parameters = $notification->getSubjectParameters();
130
+            $notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']]))
131
+                ->setRichSubject($l->t('Update to {serverAndVersion} is available.'), [
132
+                    'serverAndVersion' => [
133
+                        'type' => 'highlight',
134
+                        'id' => $notification->getObjectType(),
135
+                        'name' => $parameters['version'],
136
+                    ]
137
+                ]);
138
+
139
+            if ($this->isAdmin()) {
140
+                $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version');
141
+            }
142
+        } else {
143
+            $appInfo = $this->getAppInfo($notification->getObjectType(), $languageCode);
144
+            $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name'];
145
+
146
+            if (isset($this->appVersions[$notification->getObjectType()])) {
147
+                $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]);
148
+            }
149
+
150
+            $notification->setRichSubject($l->t('Update for {app} to version %s is available.', [$notification->getObjectId()]), [
151
+                'app' => [
152
+                    'type' => 'app',
153
+                    'id' => $notification->getObjectType(),
154
+                    'name' => $appName,
155
+                ]
156
+            ]);
157
+
158
+            if ($this->isAdmin()) {
159
+                $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType());
160
+            }
161
+        }
162
+
163
+        $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg')));
164
+
165
+        return $notification;
166
+    }
167
+
168
+    /**
169
+     * Remove the notification and prevent rendering, when the update is installed
170
+     *
171
+     * @param INotification $notification
172
+     * @param string $installedVersion
173
+     * @throws AlreadyProcessedException When the update is already installed
174
+     */
175
+    protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
176
+        if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
177
+            throw new AlreadyProcessedException();
178
+        }
179
+    }
180
+
181
+    /**
182
+     * @return bool
183
+     */
184
+    protected function isAdmin(): bool {
185
+        $user = $this->userSession->getUser();
186
+
187
+        if ($user instanceof IUser) {
188
+            return $this->groupManager->isAdmin($user->getUID());
189
+        }
190
+
191
+        return false;
192
+    }
193
+
194
+    protected function getCoreVersions(): string {
195
+        return implode('.', Util::getVersion());
196
+    }
197
+
198
+    protected function getAppVersions(): array {
199
+        return \OC_App::getAppVersions();
200
+    }
201
+
202
+    protected function getAppInfo($appId, $languageCode) {
203
+        return \OC_App::getAppInfo($appId, false, $languageCode);
204
+    }
205 205
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
 				]);
138 138
 
139 139
 			if ($this->isAdmin()) {
140
-				$notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version');
140
+				$notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']).'#version');
141 141
 			}
142 142
 		} else {
143 143
 			$appInfo = $this->getAppInfo($notification->getObjectType(), $languageCode);
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
 			]);
157 157
 
158 158
 			if ($this->isAdmin()) {
159
-				$notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType());
159
+				$notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']).'#app-'.$notification->getObjectType());
160 160
 			}
161 161
 		}
162 162
 
Please login to merge, or discard this patch.