SubscriptionApiController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
eloc 25
c 0
b 0
f 0
dl 0
loc 63
rs 10
ccs 0
cts 30
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A subscribe() 0 6 2
A get() 0 8 3
A __construct() 0 12 1
A unsubscribe() 0 6 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Controller;
25
26
use OCP\AppFramework\Db\DoesNotExistException;
27
use OCA\Polls\Exceptions\Exception;
28
29
use OCP\IRequest;
30
31
use OCP\AppFramework\ApiController;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
35
use OCA\Polls\Service\SubscriptionService;
36
37
class SubscriptionApiController extends ApiController {
38
39
	/** @var SubscriptionService */
40
	private $subscriptionService;
41
42
	public function __construct(
43
		string $appName,
44
		SubscriptionService $subscriptionService,
45
		IRequest $request
46
47
	) {
48
		parent::__construct($appName,
49
			$request,
50
			'PUT, GET, DELETE',
51
			'Authorization, Content-Type, Accept',
52
			1728000);
53
		$this->subscriptionService = $subscriptionService;
54
	}
55
56
	/**
57
	 * Get subscription status
58
	 * @NoAdminRequired
59
	 * @CORS
60
	 * @NoCSRFRequired
61
	 */
62
	public function get(int $pollId): DataResponse {
63
		try {
64
			$this->subscriptionService->get($pollId, '');
65
			return new DataResponse(['status' => 'Subscribed to poll ' . $pollId], Http::STATUS_OK);
66
		} catch (DoesNotExistException $e) {
67
			return new DataResponse(['status' => 'Not subscribed to poll ' . $pollId], Http::STATUS_NOT_FOUND);
68
		} catch (Exception $e) {
69
			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
70
		}
71
	}
72
73
	/**
74
	 * Subscribe to poll
75
	 * @NoAdminRequired
76
	 * @CORS
77
	 * @NoCSRFRequired
78
	 */
79
	public function subscribe(int $pollId): DataResponse {
80
		try {
81
			$this->subscriptionService->set($pollId, '', true);
82
			return new DataResponse(['status' => 'Subscribed to poll ' . $pollId], Http::STATUS_OK);
83
		} catch (Exception $e) {
84
			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
85
		}
86
	}
87
88
	/**
89
	 * Unsubscribe from poll
90
	 * @NoAdminRequired
91
	 * @CORS
92
	 * @NoCSRFRequired
93
	 */
94
	public function unsubscribe(int $pollId): DataResponse {
95
		try {
96
			$this->subscriptionService->set($pollId, '', false);
97
			return new DataResponse(['status' => 'Unsubscribed from poll ' . $pollId], Http::STATUS_OK);
98
		} catch (Exception $e) {
99
			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
100
		}
101
	}
102
}
103