Passed
Pull Request — master (#966)
by René
04:27
created

SubscriptionService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 93
ccs 0
cts 57
cp 0
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B set() 0 40 7
A get() 0 10 3
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 $userId;
39
	private $acl;
40
	private $subscriptionMapper;
41
	private $logger;
42
43
	/**
44
	 * SubscriptionController constructor.
45
	 * @param string $appName
46
	 * @param $UserId
47
	 * @param SubscriptionMapper $subscriptionMapper
48
	 * @param IRequest $request
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
	 * @param ILogger $logger
50
	 * @param Acl $acl
51
	 */
52
53
	public function __construct(
54
		string $appName,
0 ignored issues
show
Unused Code introduced by
The parameter $appName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
		/** @scrutinizer ignore-unused */ string $appName,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
		$userId,
56
		SubscriptionMapper $subscriptionMapper,
57
		ILogger $logger,
58
		Acl $acl
59
	) {
60
		$this->userId = $userId;
61
		$this->subscriptionMapper = $subscriptionMapper;
62
		$this->acl = $acl;
63
		$this->logger = $logger;
64
	}
65
66
	/**
67
	 * @NoAdminRequired
68
	 * @param integer $pollId
69
	 * @return DataResponse
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\DataResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
	 */
71
	public function get($pollId) {
72
		if (!$this->acl->setPollId($pollId)->getAllowView()) {
73
			throw new NotAuthorizedException;
74
		}
75
		try {
76
			return $this->subscriptionMapper->findByUserAndPoll($pollId, $this->acl->getUserId());
77
		} catch (MultipleObjectsReturnedException $e) {
78
			// subscription should be unique. If duplicates are found resubscribe
79
			// duplicates are removed in $this->set()
80
			return $this->set($pollId, true);
81
		}
82
83
	}
84
85
	/**
86
	 * @NoAdminRequired
87
	 * @param integer $pollId
88
	 */
89
	public function set($pollId, $subscribed) {
90
		if (!$this->acl->setPollId($pollId)->getAllowView()) {
91
			throw new NotAuthorizedException;
92
		}
93
		try {
94
			$subscription = $this->subscriptionMapper->findByUserAndPoll($pollId, $this->acl->getUserId());
95
			if (!$subscribed) {
96
				$this->subscriptionMapper->delete($subscription);
97
				return 'Unsubscribed';
98
			} else {
99
				// subscription already exists, just return the existing subscription
100
				return $subscription;
101
			}
102
		} catch (DoesNotExistException $e){
103
			if ($subscribed) {
104
				$subscription = new Subscription();
105
				$subscription->setPollId($pollId);
106
				$subscription->setUserId($this->acl->getUserId());
107
108
				$this->subscriptionMapper->insert($subscription);
109
				return $subscription;
110
			} else {
111
				// subscription is not found, just approve the unsubscription
112
				return 'Unsubscribed';
113
			}
114
		} catch (MultipleObjectsReturnedException $e) {
115
			// Duplicates should not exist but if found, fix it
116
			// unsubscribe from all and resubscribe, if requested
117
			$this->logger->debug('Multiple subscription (dulpicates) found');
118
			$this->subscriptionMapper->unsubscribe($pollId, $this->acl->getUserId());
119
			$this->logger->debug('Unsubscribed all for user ' . $this->acl->getUserId() . 'in poll' . $pollId);
120
			if ($subscribed) {
121
				$subscription = new Subscription();
122
				$subscription->setPollId($pollId);
123
				$subscription->setUserId($this->acl->getUserId());
124
				$this->subscriptionMapper->insert($subscription);
125
				$this->logger->debug('Added new subscription');
126
				return $subscription;
127
			} else {
128
				return 'Unsubscribed';
129
			}
130
131
		}
132
133
	}
134
}
135