Passed
Push — master ( fce6df...8e01ff )
by Georg
14:04 queued 11s
created

JSDataService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 51
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A jsonSerialize() 0 31 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Georg Ehrke
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\UserStatus\Service;
27
28
use OCP\AppFramework\Db\DoesNotExistException;
29
use OCP\IUserSession;
30
31
class JSDataService implements \JsonSerializable {
32
33
	/** @var IUserSession */
34
	private $userSession;
35
36
	/** @var StatusService */
37
	private $statusService;
38
39
	/**
40
	 * JSDataService constructor.
41
	 *
42
	 * @param IUserSession $userSession
43
	 * @param StatusService $statusService
44
	 */
45
	public function __construct(IUserSession $userSession,
46
								StatusService $statusService) {
47
		$this->userSession = $userSession;
48
		$this->statusService = $statusService;
49
	}
50
51
	public function jsonSerialize() {
52
		$user = $this->userSession->getUser();
53
54
		if ($user === null) {
55
			return [];
56
		}
57
58
		try {
59
			$status = $this->statusService->findByUserId($user->getUID());
60
		} catch (DoesNotExistException $ex) {
61
			return [
62
				'userId' => $user->getUID(),
63
				'message' => null,
64
				'messageId' => null,
65
				'messageIsPredefined' => false,
66
				'icon' => null,
67
				'clearAt' => null,
68
				'status' => 'offline',
69
				'statusIsUserDefined' => false,
70
			];
71
		}
72
73
		return [
74
			'userId' => $status->getUserId(),
75
			'message' => $status->getCustomMessage(),
76
			'messageId' => $status->getMessageId(),
77
			'messageIsPredefined' => $status->getMessageId() !== null,
78
			'icon' => $status->getCustomIcon(),
79
			'clearAt' => $status->getClearAt(),
80
			'status' => $status->getStatus(),
81
			'statusIsUserDefined' => $status->getIsUserDefined(),
82
		];
83
	}
84
}
85