Completed
Pull Request — master (#966)
by René
04:37
created

SubscriptionApiController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 29
c 1
b 0
f 0
dl 0
loc 77
ccs 0
cts 40
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A subscribe() 0 6 2
A get() 0 8 3
A __construct() 0 16 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 Exception;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
use OCA\Polls\Exceptions\NotAuthorizedException;
29
30
use OCP\IRequest;
31
use OCP\ILogger;
32
33
use OCP\AppFramework\ApiController;
34
use OCP\AppFramework\Http;
35
use OCP\AppFramework\Http\DataResponse;
36
37
use OCA\Polls\Service\SubscriptionService;
38
39
class SubscriptionApiController extends ApiController {
40
41
	private $userId;
42
	private $subscriptionService;
43
	private $logger;
44
45
	/**
46
	 * SubscriptionController constructor.
47
	 * @param string $appName
48
	 * @param $UserId
49
	 * @param SubscriptionService $subscriptionService
50
	 * @param IRequest $request
51
	 * @param ILogger $logger
52
	 */
53
54
	public function __construct(
55
		string $appName,
56
		$userId,
57
		SubscriptionService $subscriptionService,
58
		IRequest $request,
59
		ILogger $logger
60
61
	) {
62
		parent::__construct($appName,
63
			$request,
64
			'PUT, GET, DELETE',
65
            'Authorization, Content-Type, Accept',
66
            1728000);
67
		$this->userId = $userId;
68
		$this->subscriptionService = $subscriptionService;
69
		$this->logger = $logger;
70
	}
71
72
	/**
73
	 * @NoAdminRequired
74
	 * CORS
75
	 * @NoCSRFRequired
76
	 * @param integer $pollId
77
	 * @return DataResponse
78
	 */
79
	public function get($pollId) {
80
		try {
81
			$this->subscriptionService->get($pollId);
82
			return new DataResponse(['status' => 'Subscribed to poll ' . $pollId], Http::STATUS_OK);
83
		} catch (DoesNotExistException $e) {
84
			return new DataResponse(['status' => 'Not subscribed to poll ' . $pollId], Http::STATUS_NOT_FOUND);
85
		} catch (NotAuthorizedException $e) {
86
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
87
		}
88
	}
89
90
	/**
91
	 * @NoAdminRequired
92
	 * @CORS
93
	 * @NoCSRFRequired
94
	 * @param integer $pollId
95
	 */
96
	public function subscribe($pollId) {
97
		try {
98
			$this->subscriptionService->set($pollId, true);
99
			return new DataResponse(['status' => 'Subscribed to poll ' . $pollId], Http::STATUS_OK);
100
		} catch (NotAuthorizedException $e) {
101
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
102
		}
103
	}
104
	/**
105
	 * @NoAdminRequired
106
	 * @CORS
107
	 * @NoCSRFRequired
108
	 * @param integer $pollId
109
	 */
110
	public function unsubscribe($pollId) {
111
		try {
112
			$this->subscriptionService->set($pollId, false);
113
			return new DataResponse(['status' => 'Unsubscribed from poll ' . $pollId], Http::STATUS_OK);
114
		} catch (NotAuthorizedException $e) {
115
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
116
		}
117
	}
118
}
119