Passed
Pull Request — master (#1516)
by Daniel
03:43
created

Remove::removeUsers()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2021 Daniel Rudolf <[email protected]>
4
 *
5
 * @author Daniel Rudolf <[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\Command\Share;
25
26
use OC\Core\Command\Base;
0 ignored issues
show
Bug introduced by
The type OC\Core\Command\Base 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...
27
use OCA\Polls\Db\Poll;
28
use OCA\Polls\Db\Share;
29
use OCA\Polls\Model\Contact;
30
use OCA\Polls\Model\Email;
31
use OCA\Polls\Model\GenericUser;
32
use OCA\Polls\Model\Group;
33
use OCA\Polls\Model\User;
34
use OCP\AppFramework\Db\DoesNotExistException;
35
use Symfony\Component\Console\Input\InputArgument;
36
use Symfony\Component\Console\Input\InputInterface;
37
use Symfony\Component\Console\Input\InputOption;
38
use Symfony\Component\Console\Output\OutputInterface;
39
40
class Remove extends Base {
41
	use TShareCommand;
42
43
	protected function configure(): void {
44
		$this
45
			->setName('polls:share:remove')
46
			->setDescription('Remove user invitations from a poll')
47
			->addArgument(
48
				'id',
49
				InputArgument::REQUIRED,
50
				'ID of the poll to remove invitations from'
51
			)->addOption(
52
				'user',
53
				null,
54
				InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
55
				'Removes invitation of the given users from the poll'
56
			)->addOption(
57
				'group',
58
				null,
59
				InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
60
				'Removes invitations for all members of the given groups from the poll'
61
			)->addOption(
62
				'email',
63
				null,
64
				InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
65
				'Removes invitations for all users with the given e-mail addresses from the poll'
66
			);
67
	}
68
69
	protected function execute(InputInterface $input, OutputInterface $output): int {
70
		$pollId = $input->getArgument('id');
71
		$users = $input->getOption('user');
72
		$groups = $input->getOption('group');
73
		$emails = $input->getOption('email');
74
75
		try {
76
			$poll = $this->pollMapper->find($pollId);
0 ignored issues
show
Bug introduced by
$pollId of type null|string|string[] is incompatible with the type integer expected by parameter $id of OCA\Polls\Db\PollMapper::find(). ( Ignorable by Annotation )

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

76
			$poll = $this->pollMapper->find(/** @scrutinizer ignore-type */ $pollId);
Loading history...
77
		} catch (DoesNotExistException $e) {
78
			$output->writeln('<error>Poll not found.</error>');
79
			return 1;
80
		}
81
82
		$this->removeUsers($poll, $users);
0 ignored issues
show
Bug introduced by
It seems like $users can also be of type boolean and null and string; however, parameter $userIds of OCA\Polls\Command\Share\Remove::removeUsers() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

82
		$this->removeUsers($poll, /** @scrutinizer ignore-type */ $users);
Loading history...
83
		$this->removeGroups($poll, $groups);
0 ignored issues
show
Bug introduced by
It seems like $groups can also be of type boolean and null and string; however, parameter $groupIds of OCA\Polls\Command\Share\Remove::removeGroups() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

83
		$this->removeGroups($poll, /** @scrutinizer ignore-type */ $groups);
Loading history...
84
		$this->removeEmails($poll, $emails);
0 ignored issues
show
Bug introduced by
It seems like $emails can also be of type boolean and null and string; however, parameter $emails of OCA\Polls\Command\Share\Remove::removeEmails() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

84
		$this->removeEmails($poll, /** @scrutinizer ignore-type */ $emails);
Loading history...
85
86
		$output->writeln('<info>Poll invitations successfully revoked.</info>');
87
		return 0;
88
	}
89
90
	/**
91
	 * @param Poll     $poll
92
	 * @param string[] $userIds
93
	 */
94
	private function removeUsers(Poll $poll, array $userIds): void {
95
		foreach ($this->getUserShares($poll) as $share) {
96
			if (in_array($share->getUserId(), $userIds, true)) {
97
				$this->shareService->delete($share->getToken());
98
			}
99
		}
100
	}
101
102
	/**
103
	 * @param Poll     $poll
104
	 * @param string[] $groupIds
105
	 */
106
	private function removeGroups(Poll $poll, array $groupIds): void {
107
		foreach ($this->getGroupShares($poll) as $share) {
108
			if (in_array($share->getUserId(), $groupIds, true)) {
109
				$this->shareService->delete($share->getToken());
110
			}
111
		}
112
	}
113
114
	/**
115
	 * @param Poll     $poll
116
	 * @param string[] $emails
117
	 */
118
	private function removeEmails(Poll $poll, array $emails): void {
119
		foreach ($this->getEmailShares($poll) as $share) {
120
			if (in_array($share->getEmailAddress(), $emails, true)) {
121
				$this->shareService->delete($share->getToken());
122
			}
123
		}
124
	}
125
126
	/**
127
	 * @param Poll $poll
128
	 * @return Share[]
129
	 */
130
	private function getUserShares(Poll $poll): array {
131
		$shares = $this->shareMapper->findByPoll($poll->getId());
132
		return array_values(array_filter($shares, static function (Share $share): bool {
133
			return ($share->getType() === User::TYPE);
134
		}));
135
	}
136
137
	/**
138
	 * @param Poll $poll
139
	 * @return Share[]
140
	 */
141
	private function getGroupShares(Poll $poll): array {
142
		$shares = $this->shareMapper->findByPoll($poll->getId());
143
		return array_values(array_filter($shares, static function (Share $share): bool {
144
			return ($share->getType() === Group::TYPE);
145
		}));
146
	}
147
148
	/**
149
	 * @param Poll $poll
150
	 * @return Share[]
151
	 */
152
	private function getEmailShares(Poll $poll): array {
153
		$shares = $this->shareMapper->findByPoll($poll->getId());
154
		return array_values(array_filter($shares, static function (Share $share): bool {
155
			if (($share->getType() === GenericUser::TYPE) && $share->getEmailAddress()) {
156
				return true;
157
			}
158
159
			return (($share->getType() === Email::TYPE) || ($share->getType() === Contact::TYPE));
160
		}));
161
	}
162
}
163