Completed
Push — master ( d62417...7b2aa5 )
by René
10:14 queued 11s
created

SubscriptionController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 64
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 10 2
A get() 0 14 4
A __construct() 0 12 1
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 OCP\AppFramework\Db\MultipleObjectsReturnedException;
29
30
use OCP\IRequest;
31
use OCP\ILogger;
32
33
use OCP\AppFramework\Controller;
34
use OCP\AppFramework\Http;
35
use OCP\AppFramework\Http\DataResponse;
36
37
use OCA\Polls\Db\Subscription;
38
use OCA\Polls\Db\SubscriptionMapper;
39
40
class SubscriptionController extends Controller {
41
42
	private $userId;
43
	private $mapper;
44
	private $logger;
45
46
	/**
47
	 * SubscriptionController constructor.
48
	 * @param string $appName
49
	 * @param $UserId
50
	 * @param SubscriptionMapper $mapper
51
	 * @param IRequest $request
52
	 * @param ILogger $logger
53
	 */
54
55
	public function __construct(
56
		string $appName,
57
		$userId,
58
		SubscriptionMapper $mapper,
59
		IRequest $request,
60
		ILogger $logger
61
62
	) {
63
		parent::__construct($appName, $request);
64
		$this->userId = $userId;
65
		$this->mapper = $mapper;
66
		$this->logger = $logger;
67
	}
68
69
	/**
70
	 * @NoAdminRequired
71
	 * @param integer $pollId
72
	 * @return DataResponse
73
	 */
74
	public function get($pollId) {
75
76
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
77
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
78
		}
79
80
		try {
81
			$this->mapper->findByUserAndPoll($pollId, $this->userId);
82
		} catch (MultipleObjectsReturnedException $e) {
83
			// should not happen, but who knows
84
		} catch (DoesNotExistException $e) {
85
			return new DataResponse(null, Http::STATUS_NOT_FOUND);
86
		}
87
		return new DataResponse(null, Http::STATUS_OK);
88
	}
89
90
	/**
91
	 * @NoAdminRequired
92
	 * @param integer $pollId
93
	 */
94
	public function set($pollId, $subscribed) {
95
		if ($subscribed) {
96
			$subscription = new Subscription();
97
			$subscription->setPollId($pollId);
98
			$subscription->setUserId($this->userId);
99
			$this->mapper->insert($subscription);
100
			return true;
101
		} else {
102
			$this->mapper->unsubscribe($pollId, $this->userId);
103
			return false;
104
		}
105
	}
106
}
107