Issues (36)

lib/Command/Addresses.php (6 issues)

1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Files_FromMail - Recover your email attachments from your cloud.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 *  You should have received a copy of the GNU Affero General Public License
25
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Files_FromMail\Command;
31
32
use Exception;
33
use OC\Core\Command\Base;
0 ignored issues
show
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...
34
use OCA\Files_FromMail\Exceptions\AddressAlreadyExistException;
35
use OCA\Files_FromMail\Exceptions\FakeException;
36
use OCA\Files_FromMail\Exceptions\InvalidAddressException;
37
use OCA\Files_FromMail\Exceptions\MissingArgumentException;
38
use OCA\Files_FromMail\Exceptions\UnknownAddressException;
39
use OCA\Files_FromMail\Service\MailService;
40
use Symfony\Component\Console\Input\InputArgument;
0 ignored issues
show
The type Symfony\Component\Console\Input\InputArgument 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...
41
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Input\InputInterface 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...
42
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
The type Symfony\Component\Console\Input\InputOption 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...
43
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Output\OutputInterface 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...
44
45
46
/**
47
 * Class Addresses
48
 *
49
 * @package OCA\Files_FromMail\Command
50
 */
51
class Addresses extends Base {
52
53
54
	/** @var MailService */
55
	private $mailService;
56
57
	/**
58
	 * Addresses constructor.
59
	 *
60
	 * @param MailService $mailService
61
	 */
62
	public function __construct(MailService $mailService) {
63
		parent::__construct();
64
65
		$this->mailService = $mailService;
66
	}
67
68
69
	/**
70
	 * ./occ files_frommail:address to manage the mail address to get caught by the app.
71
	 *
72
	 * ./occ files_frommail:address --list
73
	 * ./occ files_frommail:address --add mail_address
74
	 * ./occ files_frommail:address --remove mail_address
75
	 * ./occ files_frommail:address --password mail_address password
76
	 *
77
	 */
78
	protected function configure() {
79
		parent::configure();
80
		$this->setName('files_frommail:address')
81
			 ->setDescription('manage the linked groups')
82
			 ->addOption('add', 'a', InputOption::VALUE_NONE, 'add a new mail address')
83
			 ->addOption('list', 'l', InputOption::VALUE_NONE, 'list all mail addresses')
84
			 ->addOption('remove', 'r', InputOption::VALUE_NONE, 'remove a mail address')
85
			 ->addOption(
86
				 'password', 'p', InputOption::VALUE_NONE, 'add a password to protect a mail address'
87
			 )
88
			 ->addArgument('address', InputArgument::OPTIONAL, 'mail address')
89
			 ->addArgument('password', InputArgument::OPTIONAL, 'password', '');
90
	}
91
92
93
	/**
94
	 * @param InputInterface $input
95
	 * @param OutputInterface $output
96
	 *
97
	 * @return int
98
	 * @throws Exception
99
	 */
100
	protected function execute(InputInterface $input, OutputInterface $output): int {
101
102
		try {
103
			$this->listMailAddresses($input, $output);
104
			$this->addMailAddress($input);
105
			$this->removeMailAddress($input);
106
			$this->setMailAddressPassword($input, $output);
107
108
			$output->writeln('please specify an action or use --help');
109
		} catch (FakeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
110
		} catch (Exception $e) {
111
			throw $e;
112
		}
113
114
		return 0;
115
	}
116
117
118
	/**
119
	 * @param InputInterface $input
120
	 * @param OutputInterface $output
121
	 *
122
	 * @throws FakeException
123
	 */
124
	private function listMailAddresses(InputInterface $input, OutputInterface $output): void {
125
		if ($input->getOption('list') !== true) {
126
			return;
127
		}
128
129
		$addresses = $this->mailService->getMailAddresses();
130
131
		if (sizeof($addresses) === 0) {
132
			$output->writeln('no mail address');
133
			throw new FakeException();
134
		}
135
136
		foreach ($addresses as $entry) {
137
			$output->writeln($this->formatMailAddress($entry));
138
		}
139
140
		throw new FakeException();
141
	}
142
143
144
	/**
145
	 * @param InputInterface $input
146
	 *
147
	 * @throws FakeException
148
	 * @throws MissingArgumentException
149
	 * @throws AddressAlreadyExistException
150
	 * @throws InvalidAddressException
151
	 */
152
	private function addMailAddress(InputInterface $input): void {
153
		if ($input->getOption('add') !== true) {
154
			return;
155
		}
156
157
		$mail = $this->checkMailAddress($input);
158
		$this->mailService->addMailAddress($mail);
159
160
		throw new FakeException();
161
	}
162
163
164
	/**
165
	 * @param InputInterface $input
166
	 *
167
	 * @throws FakeException
168
	 * @throws MissingArgumentException
169
	 * @throws UnknownAddressException
170
	 */
171
	private function removeMailAddress(InputInterface $input): void {
172
		if ($input->getOption('remove') !== true) {
173
			return;
174
		}
175
176
		$mail = $this->checkMailAddress($input);
177
		$this->mailService->removeMailAddress($mail);
178
179
		throw new FakeException();
180
	}
181
182
183
	/**
184
	 * @param InputInterface $input
185
	 * @param OutputInterface $output
186
	 *
187
	 * @throws FakeException
188
	 * @throws MissingArgumentException
189
	 * @throws UnknownAddressException
190
	 */
191
	private function setMailAddressPassword(InputInterface $input, OutputInterface $output): void {
192
		if ($input->getOption('password') !== true) {
193
			return;
194
		}
195
196
		$mail = $this->checkMailAddress($input);
197
		$password = $input->getArgument('password');
198
		$this->mailService->setMailPassword($mail, $password);
199
200
		if ($password === '') {
201
			$output->writeln('Password for ' . $mail . ' is now UNSET');
202
			throw new FakeException();
203
		}
204
205
		$output->writeln('Password for ' . $mail . ' is now SET to ' . $password);
206
		throw new FakeException();
207
	}
208
209
210
	/**
211
	 * @param InputInterface $input
212
	 *
213
	 * @return string
214
	 * @throws MissingArgumentException
215
	 */
216
	private function checkMailAddress(InputInterface $input): string {
217
		$mail = $input->getArgument('address');
218
		if ($mail === null) {
219
			throw new MissingArgumentException('missing email address');
220
		}
221
222
		return $mail;
223
	}
224
225
226
	/**
227
	 * @param array $entry
228
	 *
229
	 * @return string
230
	 */
231
	private function formatMailAddress($entry): string {
232
		$line = '- ' . $entry['address'];
233
		if (array_key_exists('password', $entry) && $entry['password'] !== '') {
234
			$line .= ' [:' . $entry['password'] . ']';
235
		}
236
237
		return $line;
238
	}
239
240
}
241
242