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

StatusesController::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
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\Controller;
27
28
use OCA\UserStatus\Db\UserStatus;
29
use OCA\UserStatus\Service\StatusService;
30
use OCP\AppFramework\Db\DoesNotExistException;
31
use OCP\AppFramework\Http\DataResponse;
32
use OCP\AppFramework\OCS\OCSNotFoundException;
33
use OCP\AppFramework\OCSController;
34
use OCP\IRequest;
35
36
class StatusesController extends OCSController {
37
38
	/** @var StatusService */
39
	private $service;
40
41
	/**
42
	 * StatusesController constructor.
43
	 *
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param StatusService $service
47
	 */
48
	public function __construct(string $appName,
49
								IRequest $request,
50
								StatusService $service) {
51
		parent::__construct($appName, $request);
52
		$this->service = $service;
53
	}
54
55
	/**
56
	 * @NoAdminRequired
57
	 *
58
	 * @param int|null $limit
59
	 * @param int|null $offset
60
	 * @return DataResponse
61
	 */
62
	public function findAll(?int $limit=null, ?int $offset=null): DataResponse {
63
		$allStatuses = $this->service->findAll($limit, $offset);
64
65
		return new DataResponse(array_map(function ($userStatus) {
66
			return $this->formatStatus($userStatus);
67
		}, $allStatuses));
68
	}
69
70
	/**
71
	 * @NoAdminRequired
72
	 *
73
	 * @param string $userId
74
	 * @return DataResponse
75
	 * @throws OCSNotFoundException
76
	 */
77
	public function find(string $userId): DataResponse {
78
		try {
79
			$userStatus = $this->service->findByUserId($userId);
80
		} catch (DoesNotExistException $ex) {
81
			throw new OCSNotFoundException('No status for the requested userId');
82
		}
83
84
		return new DataResponse($this->formatStatus($userStatus));
85
	}
86
87
	/**
88
	 * @NoAdminRequired
89
	 *
90
	 * @param UserStatus $status
91
	 * @return array
92
	 */
93
	private function formatStatus(UserStatus $status): array {
94
		$visibleStatus = $status->getStatus();
95
		if ($visibleStatus === 'invisible') {
96
			$visibleStatus = 'offline';
97
		}
98
99
		return [
100
			'userId' => $status->getUserId(),
101
			'message' => $status->getCustomMessage(),
102
			'icon' => $status->getCustomIcon(),
103
			'clearAt' => $status->getClearAt(),
104
			'status' => $visibleStatus,
105
		];
106
	}
107
}
108