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

SubscriptionService::set()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

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