Passed
Pull Request — master (#1193)
by René
04:17
created

SubscriptionService::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 2
crap 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\Service;
25
26
use OCA\Polls\Exceptions\NotAuthorizedException;
27
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
28
use OCP\AppFramework\Db\DoesNotExistException;
29
30
use OCA\Polls\Db\Subscription;
31
use OCA\Polls\Db\SubscriptionMapper;
32
use OCA\Polls\Model\Acl;
33
34
class SubscriptionService {
35
36
	/** @var Acl */
37
	private $acl;
38
39
	/** @var SubscriptionMapper */
40
	private $subscriptionMapper;
41
42
	/**
43
	 * SubscriptionController constructor.
44
	 * @param SubscriptionMapper $subscriptionMapper
45
	 * @param Acl $acl
46
	 */
47
48
	public function __construct(
49
		SubscriptionMapper $subscriptionMapper,
50
		Acl $acl
51
	) {
52
		$this->subscriptionMapper = $subscriptionMapper;
53
		$this->acl = $acl;
54
	}
55
56
	/**
57
	 * @NoAdminRequired
58
	 * @param int $pollId
59
	 * @return bool
60
	 */
61
	public function get($pollId, $token) {
62
		if (!$this->acl->set($pollId, $token)->getAllowView()) {
63
			throw new NotAuthorizedException;
64
		}
65
		try {
66
			$this->subscriptionMapper->findByUserAndPoll($this->acl->getPollId(), $this->acl->getUserId());
67
			// Subscription exists
68
			return true;
69
		} catch (MultipleObjectsReturnedException $e) {
70
			// subscription should be unique. If duplicates are found resubscribe
71
			// duplicates are removed in $this->set()
72
			return $this->set($pollId, $token, true);
73
		}
74
	}
75
76
77
	/**
78
	 * @NoAdminRequired
79
	 * @param int $pollId
80
	 * @param string $userId
81
	 * @return void
82
	 */
83
	public function add($pollId, $userId) {
84
		$subscription = new Subscription();
85
		$subscription->setPollId($pollId);
86
		$subscription->setUserId($userId);
87
		$this->subscriptionMapper->insert($subscription);
88
	}
89
90
	/**
91
	 * @NoAdminRequired
92
	 * @param int $pollId
93
	 * @param string $token
94
	 * @param bool $subscribed
95
	 * @return bool
96
	 */
97
	public function set($pollId, $token, $subscribed) {
98
		if (!$this->acl->set($pollId, $token)->getAllowView()) {
99
			throw new NotAuthorizedException;
100
		}
101
		try {
102
			$subscription = $this->subscriptionMapper->findByUserAndPoll($this->acl->getPollId(), $this->acl->getUserId());
103
			if (!$subscribed) {
104
				$this->subscriptionMapper->delete($subscription);
105
			}
106
		} catch (DoesNotExistException $e) {
107
			if ($subscribed) {
108
				// subscription does not exist, add subscription
109
				$this->add($this->acl->getPollId(), $this->acl->getUserId());
110
			}
111
		} catch (MultipleObjectsReturnedException $e) {
112
			// Duplicates should not exist but if found, fix it
113
			// unsubscribe from all and resubscribe, if requested
114
			\OC::$server->getLogger()->debug('Multiple subscription (dulpicates) found');
115
			$this->subscriptionMapper->unsubscribe($this->acl->getPollId(), $this->acl->getUserId());
116
			\OC::$server->getLogger()->debug('Unsubscribed all for user ' . $this->acl->getUserId() . 'in poll' . $pollId);
117
			if ($subscribed) {
118
				$this->add($this->acl->getPollId(), $this->acl->getUserId());
119
				\OC::$server->getLogger()->debug('Added new subscription');
120
			}
121
		}
122
		return $subscribed;
123
	}
124
}
125