Passed
Push — master ( 103a0c...452073 )
by Morris
29:04 queued 18:51
created
lib/private/Activity/Manager.php 1 patch
Indentation   +348 added lines, -348 removed lines patch added patch discarded remove patch
@@ -42,352 +42,352 @@
 block discarded – undo
42 42
 use OCP\RichObjectStrings\IValidator;
43 43
 
44 44
 class Manager implements IManager {
45
-	/** @var IRequest */
46
-	protected $request;
47
-
48
-	/** @var IUserSession */
49
-	protected $session;
50
-
51
-	/** @var IConfig */
52
-	protected $config;
53
-
54
-	/** @var IValidator */
55
-	protected $validator;
56
-
57
-	/** @var string */
58
-	protected $formattingObjectType;
59
-
60
-	/** @var int */
61
-	protected $formattingObjectId;
62
-
63
-	/** @var bool */
64
-	protected $requirePNG = false;
65
-
66
-	/** @var string */
67
-	protected $currentUserId;
68
-
69
-	public function __construct(IRequest $request,
70
-								IUserSession $session,
71
-								IConfig $config,
72
-								IValidator $validator) {
73
-		$this->request = $request;
74
-		$this->session = $session;
75
-		$this->config = $config;
76
-		$this->validator = $validator;
77
-	}
78
-
79
-	/** @var \Closure[] */
80
-	private $consumersClosures = [];
81
-
82
-	/** @var IConsumer[] */
83
-	private $consumers = [];
84
-
85
-	/**
86
-	 * @return \OCP\Activity\IConsumer[]
87
-	 */
88
-	protected function getConsumers(): array {
89
-		if (!empty($this->consumers)) {
90
-			return $this->consumers;
91
-		}
92
-
93
-		$this->consumers = [];
94
-		foreach ($this->consumersClosures as $consumer) {
95
-			$c = $consumer();
96
-			if ($c instanceof IConsumer) {
97
-				$this->consumers[] = $c;
98
-			} else {
99
-				throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
100
-			}
101
-		}
102
-
103
-		return $this->consumers;
104
-	}
105
-
106
-	/**
107
-	 * Generates a new IEvent object
108
-	 *
109
-	 * Make sure to call at least the following methods before sending it to the
110
-	 * app with via the publish() method:
111
-	 *  - setApp()
112
-	 *  - setType()
113
-	 *  - setAffectedUser()
114
-	 *  - setSubject()
115
-	 *
116
-	 * @return IEvent
117
-	 */
118
-	public function generateEvent(): IEvent {
119
-		return new Event($this->validator);
120
-	}
121
-
122
-	/**
123
-	 * Publish an event to the activity consumers
124
-	 *
125
-	 * Make sure to call at least the following methods before sending an Event:
126
-	 *  - setApp()
127
-	 *  - setType()
128
-	 *  - setAffectedUser()
129
-	 *  - setSubject()
130
-	 *
131
-	 * @param IEvent $event
132
-	 * @throws \BadMethodCallException if required values have not been set
133
-	 */
134
-	public function publish(IEvent $event): void {
135
-		if ($event->getAuthor() === '') {
136
-			if ($this->session->getUser() instanceof IUser) {
137
-				$event->setAuthor($this->session->getUser()->getUID());
138
-			}
139
-		}
140
-
141
-		if (!$event->getTimestamp()) {
142
-			$event->setTimestamp(time());
143
-		}
144
-
145
-		if (!$event->isValid()) {
146
-			throw new \BadMethodCallException('The given event is invalid');
147
-		}
148
-
149
-		foreach ($this->getConsumers() as $c) {
150
-			$c->receive($event);
151
-		}
152
-	}
153
-
154
-	/**
155
-	 * In order to improve lazy loading a closure can be registered which will be called in case
156
-	 * activity consumers are actually requested
157
-	 *
158
-	 * $callable has to return an instance of OCA\Activity\IConsumer
159
-	 *
160
-	 * @param \Closure $callable
161
-	 */
162
-	public function registerConsumer(\Closure $callable): void {
163
-		$this->consumersClosures[] = $callable;
164
-		$this->consumers = [];
165
-	}
166
-
167
-	/** @var string[] */
168
-	protected $filterClasses = [];
169
-
170
-	/** @var IFilter[] */
171
-	protected $filters = [];
172
-
173
-	/**
174
-	 * @param string $filter Class must implement OCA\Activity\IFilter
175
-	 * @return void
176
-	 */
177
-	public function registerFilter(string $filter): void {
178
-		$this->filterClasses[$filter] = false;
179
-	}
180
-
181
-	/**
182
-	 * @return IFilter[]
183
-	 * @throws \InvalidArgumentException
184
-	 */
185
-	public function getFilters(): array {
186
-		foreach ($this->filterClasses as $class => $false) {
187
-			/** @var IFilter $filter */
188
-			$filter = \OC::$server->query($class);
189
-
190
-			if (!$filter instanceof IFilter) {
191
-				throw new \InvalidArgumentException('Invalid activity filter registered');
192
-			}
193
-
194
-			$this->filters[$filter->getIdentifier()] = $filter;
195
-
196
-			unset($this->filterClasses[$class]);
197
-		}
198
-		return $this->filters;
199
-	}
200
-
201
-	/**
202
-	 * @param string $id
203
-	 * @return IFilter
204
-	 * @throws \InvalidArgumentException when the filter was not found
205
-	 * @since 11.0.0
206
-	 */
207
-	public function getFilterById(string $id): IFilter {
208
-		$filters = $this->getFilters();
209
-
210
-		if (isset($filters[$id])) {
211
-			return $filters[$id];
212
-		}
213
-
214
-		throw new \InvalidArgumentException('Requested filter does not exist');
215
-	}
216
-
217
-	/** @var string[] */
218
-	protected $providerClasses = [];
219
-
220
-	/** @var IProvider[] */
221
-	protected $providers = [];
222
-
223
-	/**
224
-	 * @param string $provider Class must implement OCA\Activity\IProvider
225
-	 * @return void
226
-	 */
227
-	public function registerProvider(string $provider): void {
228
-		$this->providerClasses[$provider] = false;
229
-	}
230
-
231
-	/**
232
-	 * @return IProvider[]
233
-	 * @throws \InvalidArgumentException
234
-	 */
235
-	public function getProviders(): array {
236
-		foreach ($this->providerClasses as $class => $false) {
237
-			/** @var IProvider $provider */
238
-			$provider = \OC::$server->query($class);
239
-
240
-			if (!$provider instanceof IProvider) {
241
-				throw new \InvalidArgumentException('Invalid activity provider registered');
242
-			}
243
-
244
-			$this->providers[] = $provider;
245
-
246
-			unset($this->providerClasses[$class]);
247
-		}
248
-		return $this->providers;
249
-	}
250
-
251
-	/** @var string[] */
252
-	protected $settingsClasses = [];
253
-
254
-	/** @var ISetting[] */
255
-	protected $settings = [];
256
-
257
-	/**
258
-	 * @param string $setting Class must implement OCA\Activity\ISetting
259
-	 * @return void
260
-	 */
261
-	public function registerSetting(string $setting): void {
262
-		$this->settingsClasses[$setting] = false;
263
-	}
264
-
265
-	/**
266
-	 * @return ActivitySettings[]
267
-	 * @throws \InvalidArgumentException
268
-	 */
269
-	public function getSettings(): array {
270
-		foreach ($this->settingsClasses as $class => $false) {
271
-			/** @var ISetting $setting */
272
-			$setting = \OC::$server->query($class);
273
-
274
-			if ($setting instanceof ISetting) {
275
-				if (!$setting instanceof ActivitySettings) {
276
-					$setting = new ActivitySettingsAdapter($setting);
277
-				}
278
-			} else {
279
-				throw new \InvalidArgumentException('Invalid activity filter registered');
280
-			}
281
-
282
-			$this->settings[$setting->getIdentifier()] = $setting;
283
-
284
-			unset($this->settingsClasses[$class]);
285
-		}
286
-		return $this->settings;
287
-	}
288
-
289
-	/**
290
-	 * @param string $id
291
-	 * @return ActivitySettings
292
-	 * @throws \InvalidArgumentException when the setting was not found
293
-	 * @since 11.0.0
294
-	 */
295
-	public function getSettingById(string $id): ActivitySettings {
296
-		$settings = $this->getSettings();
297
-
298
-		if (isset($settings[$id])) {
299
-			return $settings[$id];
300
-		}
301
-
302
-		throw new \InvalidArgumentException('Requested setting does not exist');
303
-	}
304
-
305
-
306
-	/**
307
-	 * @param string $type
308
-	 * @param int $id
309
-	 */
310
-	public function setFormattingObject(string $type, int $id): void {
311
-		$this->formattingObjectType = $type;
312
-		$this->formattingObjectId = $id;
313
-	}
314
-
315
-	/**
316
-	 * @return bool
317
-	 */
318
-	public function isFormattingFilteredObject(): bool {
319
-		return $this->formattingObjectType !== null && $this->formattingObjectId !== null
320
-			&& $this->formattingObjectType === $this->request->getParam('object_type')
321
-			&& $this->formattingObjectId === (int) $this->request->getParam('object_id');
322
-	}
323
-
324
-	/**
325
-	 * @param bool $status Set to true, when parsing events should not use SVG icons
326
-	 */
327
-	public function setRequirePNG(bool $status): void {
328
-		$this->requirePNG = $status;
329
-	}
330
-
331
-	/**
332
-	 * @return bool
333
-	 */
334
-	public function getRequirePNG(): bool {
335
-		return $this->requirePNG;
336
-	}
337
-
338
-	/**
339
-	 * Set the user we need to use
340
-	 *
341
-	 * @param string|null $currentUserId
342
-	 * @throws \UnexpectedValueException If the user is invalid
343
-	 */
344
-	public function setCurrentUserId(string $currentUserId = null): void {
345
-		if (!is_string($currentUserId) && $currentUserId !== null) {
346
-			throw new \UnexpectedValueException('The given current user is invalid');
347
-		}
348
-		$this->currentUserId = $currentUserId;
349
-	}
350
-
351
-	/**
352
-	 * Get the user we need to use
353
-	 *
354
-	 * Either the user is logged in, or we try to get it from the token
355
-	 *
356
-	 * @return string
357
-	 * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
358
-	 */
359
-	public function getCurrentUserId(): string {
360
-		if ($this->currentUserId !== null) {
361
-			return $this->currentUserId;
362
-		}
363
-
364
-		if (!$this->session->isLoggedIn()) {
365
-			return $this->getUserFromToken();
366
-		}
367
-
368
-		return $this->session->getUser()->getUID();
369
-	}
370
-
371
-	/**
372
-	 * Get the user for the token
373
-	 *
374
-	 * @return string
375
-	 * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
376
-	 */
377
-	protected function getUserFromToken(): string {
378
-		$token = (string) $this->request->getParam('token', '');
379
-		if (strlen($token) !== 30) {
380
-			throw new \UnexpectedValueException('The token is invalid');
381
-		}
382
-
383
-		$users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
384
-
385
-		if (count($users) !== 1) {
386
-			// No unique user found
387
-			throw new \UnexpectedValueException('The token is invalid');
388
-		}
389
-
390
-		// Token found login as that user
391
-		return array_shift($users);
392
-	}
45
+    /** @var IRequest */
46
+    protected $request;
47
+
48
+    /** @var IUserSession */
49
+    protected $session;
50
+
51
+    /** @var IConfig */
52
+    protected $config;
53
+
54
+    /** @var IValidator */
55
+    protected $validator;
56
+
57
+    /** @var string */
58
+    protected $formattingObjectType;
59
+
60
+    /** @var int */
61
+    protected $formattingObjectId;
62
+
63
+    /** @var bool */
64
+    protected $requirePNG = false;
65
+
66
+    /** @var string */
67
+    protected $currentUserId;
68
+
69
+    public function __construct(IRequest $request,
70
+                                IUserSession $session,
71
+                                IConfig $config,
72
+                                IValidator $validator) {
73
+        $this->request = $request;
74
+        $this->session = $session;
75
+        $this->config = $config;
76
+        $this->validator = $validator;
77
+    }
78
+
79
+    /** @var \Closure[] */
80
+    private $consumersClosures = [];
81
+
82
+    /** @var IConsumer[] */
83
+    private $consumers = [];
84
+
85
+    /**
86
+     * @return \OCP\Activity\IConsumer[]
87
+     */
88
+    protected function getConsumers(): array {
89
+        if (!empty($this->consumers)) {
90
+            return $this->consumers;
91
+        }
92
+
93
+        $this->consumers = [];
94
+        foreach ($this->consumersClosures as $consumer) {
95
+            $c = $consumer();
96
+            if ($c instanceof IConsumer) {
97
+                $this->consumers[] = $c;
98
+            } else {
99
+                throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
100
+            }
101
+        }
102
+
103
+        return $this->consumers;
104
+    }
105
+
106
+    /**
107
+     * Generates a new IEvent object
108
+     *
109
+     * Make sure to call at least the following methods before sending it to the
110
+     * app with via the publish() method:
111
+     *  - setApp()
112
+     *  - setType()
113
+     *  - setAffectedUser()
114
+     *  - setSubject()
115
+     *
116
+     * @return IEvent
117
+     */
118
+    public function generateEvent(): IEvent {
119
+        return new Event($this->validator);
120
+    }
121
+
122
+    /**
123
+     * Publish an event to the activity consumers
124
+     *
125
+     * Make sure to call at least the following methods before sending an Event:
126
+     *  - setApp()
127
+     *  - setType()
128
+     *  - setAffectedUser()
129
+     *  - setSubject()
130
+     *
131
+     * @param IEvent $event
132
+     * @throws \BadMethodCallException if required values have not been set
133
+     */
134
+    public function publish(IEvent $event): void {
135
+        if ($event->getAuthor() === '') {
136
+            if ($this->session->getUser() instanceof IUser) {
137
+                $event->setAuthor($this->session->getUser()->getUID());
138
+            }
139
+        }
140
+
141
+        if (!$event->getTimestamp()) {
142
+            $event->setTimestamp(time());
143
+        }
144
+
145
+        if (!$event->isValid()) {
146
+            throw new \BadMethodCallException('The given event is invalid');
147
+        }
148
+
149
+        foreach ($this->getConsumers() as $c) {
150
+            $c->receive($event);
151
+        }
152
+    }
153
+
154
+    /**
155
+     * In order to improve lazy loading a closure can be registered which will be called in case
156
+     * activity consumers are actually requested
157
+     *
158
+     * $callable has to return an instance of OCA\Activity\IConsumer
159
+     *
160
+     * @param \Closure $callable
161
+     */
162
+    public function registerConsumer(\Closure $callable): void {
163
+        $this->consumersClosures[] = $callable;
164
+        $this->consumers = [];
165
+    }
166
+
167
+    /** @var string[] */
168
+    protected $filterClasses = [];
169
+
170
+    /** @var IFilter[] */
171
+    protected $filters = [];
172
+
173
+    /**
174
+     * @param string $filter Class must implement OCA\Activity\IFilter
175
+     * @return void
176
+     */
177
+    public function registerFilter(string $filter): void {
178
+        $this->filterClasses[$filter] = false;
179
+    }
180
+
181
+    /**
182
+     * @return IFilter[]
183
+     * @throws \InvalidArgumentException
184
+     */
185
+    public function getFilters(): array {
186
+        foreach ($this->filterClasses as $class => $false) {
187
+            /** @var IFilter $filter */
188
+            $filter = \OC::$server->query($class);
189
+
190
+            if (!$filter instanceof IFilter) {
191
+                throw new \InvalidArgumentException('Invalid activity filter registered');
192
+            }
193
+
194
+            $this->filters[$filter->getIdentifier()] = $filter;
195
+
196
+            unset($this->filterClasses[$class]);
197
+        }
198
+        return $this->filters;
199
+    }
200
+
201
+    /**
202
+     * @param string $id
203
+     * @return IFilter
204
+     * @throws \InvalidArgumentException when the filter was not found
205
+     * @since 11.0.0
206
+     */
207
+    public function getFilterById(string $id): IFilter {
208
+        $filters = $this->getFilters();
209
+
210
+        if (isset($filters[$id])) {
211
+            return $filters[$id];
212
+        }
213
+
214
+        throw new \InvalidArgumentException('Requested filter does not exist');
215
+    }
216
+
217
+    /** @var string[] */
218
+    protected $providerClasses = [];
219
+
220
+    /** @var IProvider[] */
221
+    protected $providers = [];
222
+
223
+    /**
224
+     * @param string $provider Class must implement OCA\Activity\IProvider
225
+     * @return void
226
+     */
227
+    public function registerProvider(string $provider): void {
228
+        $this->providerClasses[$provider] = false;
229
+    }
230
+
231
+    /**
232
+     * @return IProvider[]
233
+     * @throws \InvalidArgumentException
234
+     */
235
+    public function getProviders(): array {
236
+        foreach ($this->providerClasses as $class => $false) {
237
+            /** @var IProvider $provider */
238
+            $provider = \OC::$server->query($class);
239
+
240
+            if (!$provider instanceof IProvider) {
241
+                throw new \InvalidArgumentException('Invalid activity provider registered');
242
+            }
243
+
244
+            $this->providers[] = $provider;
245
+
246
+            unset($this->providerClasses[$class]);
247
+        }
248
+        return $this->providers;
249
+    }
250
+
251
+    /** @var string[] */
252
+    protected $settingsClasses = [];
253
+
254
+    /** @var ISetting[] */
255
+    protected $settings = [];
256
+
257
+    /**
258
+     * @param string $setting Class must implement OCA\Activity\ISetting
259
+     * @return void
260
+     */
261
+    public function registerSetting(string $setting): void {
262
+        $this->settingsClasses[$setting] = false;
263
+    }
264
+
265
+    /**
266
+     * @return ActivitySettings[]
267
+     * @throws \InvalidArgumentException
268
+     */
269
+    public function getSettings(): array {
270
+        foreach ($this->settingsClasses as $class => $false) {
271
+            /** @var ISetting $setting */
272
+            $setting = \OC::$server->query($class);
273
+
274
+            if ($setting instanceof ISetting) {
275
+                if (!$setting instanceof ActivitySettings) {
276
+                    $setting = new ActivitySettingsAdapter($setting);
277
+                }
278
+            } else {
279
+                throw new \InvalidArgumentException('Invalid activity filter registered');
280
+            }
281
+
282
+            $this->settings[$setting->getIdentifier()] = $setting;
283
+
284
+            unset($this->settingsClasses[$class]);
285
+        }
286
+        return $this->settings;
287
+    }
288
+
289
+    /**
290
+     * @param string $id
291
+     * @return ActivitySettings
292
+     * @throws \InvalidArgumentException when the setting was not found
293
+     * @since 11.0.0
294
+     */
295
+    public function getSettingById(string $id): ActivitySettings {
296
+        $settings = $this->getSettings();
297
+
298
+        if (isset($settings[$id])) {
299
+            return $settings[$id];
300
+        }
301
+
302
+        throw new \InvalidArgumentException('Requested setting does not exist');
303
+    }
304
+
305
+
306
+    /**
307
+     * @param string $type
308
+     * @param int $id
309
+     */
310
+    public function setFormattingObject(string $type, int $id): void {
311
+        $this->formattingObjectType = $type;
312
+        $this->formattingObjectId = $id;
313
+    }
314
+
315
+    /**
316
+     * @return bool
317
+     */
318
+    public function isFormattingFilteredObject(): bool {
319
+        return $this->formattingObjectType !== null && $this->formattingObjectId !== null
320
+            && $this->formattingObjectType === $this->request->getParam('object_type')
321
+            && $this->formattingObjectId === (int) $this->request->getParam('object_id');
322
+    }
323
+
324
+    /**
325
+     * @param bool $status Set to true, when parsing events should not use SVG icons
326
+     */
327
+    public function setRequirePNG(bool $status): void {
328
+        $this->requirePNG = $status;
329
+    }
330
+
331
+    /**
332
+     * @return bool
333
+     */
334
+    public function getRequirePNG(): bool {
335
+        return $this->requirePNG;
336
+    }
337
+
338
+    /**
339
+     * Set the user we need to use
340
+     *
341
+     * @param string|null $currentUserId
342
+     * @throws \UnexpectedValueException If the user is invalid
343
+     */
344
+    public function setCurrentUserId(string $currentUserId = null): void {
345
+        if (!is_string($currentUserId) && $currentUserId !== null) {
346
+            throw new \UnexpectedValueException('The given current user is invalid');
347
+        }
348
+        $this->currentUserId = $currentUserId;
349
+    }
350
+
351
+    /**
352
+     * Get the user we need to use
353
+     *
354
+     * Either the user is logged in, or we try to get it from the token
355
+     *
356
+     * @return string
357
+     * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
358
+     */
359
+    public function getCurrentUserId(): string {
360
+        if ($this->currentUserId !== null) {
361
+            return $this->currentUserId;
362
+        }
363
+
364
+        if (!$this->session->isLoggedIn()) {
365
+            return $this->getUserFromToken();
366
+        }
367
+
368
+        return $this->session->getUser()->getUID();
369
+    }
370
+
371
+    /**
372
+     * Get the user for the token
373
+     *
374
+     * @return string
375
+     * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
376
+     */
377
+    protected function getUserFromToken(): string {
378
+        $token = (string) $this->request->getParam('token', '');
379
+        if (strlen($token) !== 30) {
380
+            throw new \UnexpectedValueException('The token is invalid');
381
+        }
382
+
383
+        $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
384
+
385
+        if (count($users) !== 1) {
386
+            // No unique user found
387
+            throw new \UnexpectedValueException('The token is invalid');
388
+        }
389
+
390
+        // Token found login as that user
391
+        return array_shift($users);
392
+    }
393 393
 }
Please login to merge, or discard this patch.