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

Add::inviteEmails()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
dl 0
loc 6
rs 10
c 1
b 0
f 1
cc 3
nc 4
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\Exceptions\ShareAlreadyExistsException;
29
use OCA\Polls\Model\Email;
30
use OCA\Polls\Model\Group;
31
use OCA\Polls\Model\User;
32
use OCP\AppFramework\Db\DoesNotExistException;
33
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
0 ignored issues
show
Bug introduced by
The type Stecman\Component\Symfon...etion\CompletionContext 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...
34
use Symfony\Component\Console\Input\InputArgument;
35
use Symfony\Component\Console\Input\InputInterface;
36
use Symfony\Component\Console\Input\InputOption;
37
use Symfony\Component\Console\Output\OutputInterface;
38
39
class Add extends Base {
40
	use TShareCommand;
41
42
	protected function configure(): void {
43
		$this
44
			->setName('polls:share:add')
45
			->setDescription('Invites users to a poll')
46
			->addArgument(
47
				'id',
48
				InputArgument::REQUIRED,
49
				'ID of the poll to invite users to'
50
			)->addOption(
51
				'user',
52
				null,
53
				InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
54
				'Invites the given users to the poll'
55
			)->addOption(
56
				'group',
57
				null,
58
				InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
59
				'Invites all members of the given groups to the poll'
60
			)->addOption(
61
				'email',
62
				null,
63
				InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
64
				'Sends invitation e-mails to the given addresses to participate in the poll'
65
			);
66
	}
67
68
	protected function execute(InputInterface $input, OutputInterface $output): int {
69
		$pollId = $input->getArgument('id');
70
		$users = $input->getOption('user');
71
		$groups = $input->getOption('group');
72
		$emails = $input->getOption('email');
73
74
		try {
75
			$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

75
			$poll = $this->pollMapper->find(/** @scrutinizer ignore-type */ $pollId);
Loading history...
76
		} catch (DoesNotExistException $e) {
77
			$output->writeln('<error>Poll not found.</error>');
78
			return 1;
79
		}
80
81
		$this->inviteUsers($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\Add::inviteUsers() 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

81
		$this->inviteUsers($poll, /** @scrutinizer ignore-type */ $users);
Loading history...
82
		$this->inviteGroups($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\Add::inviteGroups() 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->inviteGroups($poll, /** @scrutinizer ignore-type */ $groups);
Loading history...
83
		$this->inviteEmails($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\Add::inviteEmails() 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->inviteEmails($poll, /** @scrutinizer ignore-type */ $emails);
Loading history...
84
85
		$output->writeln('<info>Users successfully invited to poll.</info>');
86
		return 0;
87
	}
88
89
	/**
90
	 * @param Poll     $poll
91
	 * @param string[] $userIds
92
	 */
93
	private function inviteUsers(Poll $poll, array $userIds): void {
94
		foreach ($userIds as $userId) {
95
			try {
96
				$share = $this->shareService->add($poll->getId(), User::TYPE, $userId);
97
				$this->shareService->sendInvitation($share->getToken());
98
			} catch (ShareAlreadyExistsException $e) {
99
				// silently ignore already existing shares
100
			}
101
		}
102
	}
103
104
	/**
105
	 * @param Poll     $poll
106
	 * @param string[] $groupIds
107
	 */
108
	private function inviteGroups(Poll $poll, array $groupIds): void {
109
		foreach ($groupIds as $groupId) {
110
			try {
111
				$share = $this->shareService->add($poll->getId(), Group::TYPE, $groupId);
112
				$this->shareService->sendInvitation($share->getToken());
113
			} catch (ShareAlreadyExistsException $e) {
114
				// silently ignore already existing shares
115
			}
116
		}
117
	}
118
119
	/**
120
	 * @param Poll     $poll
121
	 * @param string[] $emails
122
	 */
123
	private function inviteEmails(Poll $poll, array $emails): void {
124
		foreach ($emails as $email) {
125
			try {
126
				$share = $this->shareService->add($poll->getId(), Email::TYPE, $email);
127
				$this->shareService->sendInvitation($share->getToken());
128
			} catch (ShareAlreadyExistsException $e) {
129
				// silently ignore already existing shares
130
			}
131
		}
132
	}
133
134
	public function completeOptionValues($optionName, CompletionContext $context) {
135
		switch ($optionName) {
136
			case 'user':
137
				return $this->completeUserValues($context);
138
139
			case 'group':
140
				return $this->completeGroupValues($context);
141
		}
142
143
		return parent::completeOptionValues($optionName, $context);
144
	}
145
}
146