Passed
Pull Request — master (#1284)
by René
04:02
created

SubscriptionService::set()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 22
ccs 0
cts 16
cp 0
rs 9.4888
cc 5
nc 10
nop 3
crap 30
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 OCP\AppFramework\Db\DoesNotExistException;
27
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
28
29
use OCA\Polls\Db\Subscription;
30
use OCA\Polls\Db\SubscriptionMapper;
31
use OCA\Polls\Model\Acl;
32
33
class SubscriptionService {
34
35
	/** @var Acl */
36
	private $acl;
37
38
	/** @var SubscriptionMapper */
39
	private $subscriptionMapper;
40
41
	public function __construct(
42
		SubscriptionMapper $subscriptionMapper,
43
		Acl $acl
44
	) {
45
		$this->subscriptionMapper = $subscriptionMapper;
46
		$this->acl = $acl;
47
	}
48
49
	public function get(int $pollId = 0, string $token = ''): bool {
50
		if ($token) {
51
			$this->acl->setToken($token);
52
		} else {
53
			$this->acl->setPollId($pollId);
54
		}
55
56
		try {
57
			$this->subscriptionMapper->findByPollAndUser($this->acl->getPollId(), $this->acl->getUserId());
58
			// Subscription exists
59
			return true;
60
		} catch (MultipleObjectsReturnedException $e) {
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\MultipleObjectsReturnedException 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...
61
			// subscription should be unique. If duplicates are found resubscribe
62
			// duplicates are removed in $this->set()
63
			return $this->set($pollId, $token, true);
64
		} catch (DoesNotExistException $e) {
65
			return false;
66
		}
67
	}
68
69
	public function add(int $pollId, string $userId): void {
70
		$subscription = new Subscription();
71
		$subscription->setPollId($pollId);
72
		$subscription->setUserId($userId);
73
		$this->subscriptionMapper->insert($subscription);
74
	}
75
76
	public function set(int $pollId = 0, string $token = '', bool $subscribed): bool {
77
		if ($token) {
78
			$this->acl->setToken($token);
79
		} else {
80
			$this->acl->setPollId($pollId);
81
		}
82
83
		if (!$subscribed) {
84
			try {
85
				$subscription = $this->subscriptionMapper->findByPollAndUser($this->acl->getPollId(), $this->acl->getUserId());
86
				$this->subscriptionMapper->delete($subscription);
87
			} catch (DoesNotExistException $e) {
88
				// catch silently (assume already unsubscribed)
89
			}
90
		} else {
91
			try {
92
				$this->add($this->acl->getPollId(), $this->acl->getUserId());
93
			} catch (UniqueConstraintViolationException $e) {
94
				// catch silently (assume already subscribed)
95
			}
96
		}
97
		return $subscribed;
98
	}
99
}
100