CreateAccount::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 17
cts 17
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * Mail
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Mail\Command;
23
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\InputArgument;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use OCP\Security\ICrypto;
29
use OCA\Mail\Db\MailAccount;
30
use OCA\Mail\Service\AccountService;
31
32
class CreateAccount extends Command {
33
34
	const ARGUMENT_USER_ID = 'user-id';
35
	const ARGUMENT_NAME = 'name';
36
	const ARGUMENT_EMAIL = 'email';
37
	const ARGUMENT_IMAP_HOST = 'imap-host';
38
	const ARGUMENT_IMAP_PORT = 'imap-port';
39
	const ARGUMENT_IMAP_SSL_MODE = 'imap-ssl-mode';
40
	const ARGUMENT_IMAP_USER = 'imap-user';
41
	const ARGUMENT_IMAP_PASSWORD = 'imap-password';
42
	const ARGUMENT_SMTP_HOST = 'smtp-host';
43
	const ARGUMENT_SMTP_PORT = 'smtp-port';
44
	const ARGUMENT_SMTP_SSL_MODE = 'smtp-ssl-mode';
45
	const ARGUMENT_SMTP_USER = 'smtp-user';
46
	const ARGUMENT_SMTP_PASSWORD = 'smtp-password';
47
48
	/** @var AccountService */
49
	private $accountService;
50
51
	/** @var \OCP\Security\ICrypto */
52
	private $crypto;
53
54 3
	public function __construct(AccountService $service, ICrypto $crypto) {
55 3
		parent::__construct();
56
57 3
		$this->accountService = $service;
58 3
		$this->crypto = $crypto;
59 3
	}
60
61 3
	protected function configure() {
62 3
		$this->setName('mail:account:create');
63 3
		$this->setDescription('creates IMAP account');
64 3
		$this->addArgument(self::ARGUMENT_USER_ID, InputArgument::REQUIRED);
65 3
		$this->addArgument(self::ARGUMENT_NAME, InputArgument::REQUIRED);
66 3
		$this->addArgument(self::ARGUMENT_EMAIL, InputArgument::REQUIRED);
67
68 3
		$this->addArgument(self::ARGUMENT_IMAP_HOST, InputArgument::REQUIRED);
69 3
		$this->addArgument(self::ARGUMENT_IMAP_PORT, InputArgument::REQUIRED);
70 3
		$this->addArgument(self::ARGUMENT_IMAP_SSL_MODE, InputArgument::REQUIRED);
71 3
		$this->addArgument(self::ARGUMENT_IMAP_USER, InputArgument::REQUIRED);
72 3
		$this->addArgument(self::ARGUMENT_IMAP_PASSWORD, InputArgument::REQUIRED);
73
74 3
		$this->addArgument(self::ARGUMENT_SMTP_HOST, InputArgument::REQUIRED);
75 3
		$this->addArgument(self::ARGUMENT_SMTP_PORT, InputArgument::REQUIRED);
76 3
		$this->addArgument(self::ARGUMENT_SMTP_SSL_MODE, InputArgument::REQUIRED);
77 3
		$this->addArgument(self::ARGUMENT_SMTP_USER, InputArgument::REQUIRED);
78 3
		$this->addArgument(self::ARGUMENT_SMTP_PASSWORD, InputArgument::REQUIRED);
79 3
	}
80
81
	protected function execute(InputInterface $input, OutputInterface $output) {
82
		$userId = $input->getArgument(self::ARGUMENT_USER_ID);
83
		$name = $input->getArgument(self::ARGUMENT_NAME);
84
		$email = $input->getArgument(self::ARGUMENT_EMAIL);
85
86
		$imapHost = $input->getArgument(self::ARGUMENT_IMAP_HOST);
87
		$imapPort = $input->getArgument(self::ARGUMENT_IMAP_PORT);
88
		$imapSslMode = $input->getArgument(self::ARGUMENT_IMAP_SSL_MODE);
89
		$imapUser = $input->getArgument(self::ARGUMENT_IMAP_USER);
90
		$imapPassword = $input->getArgument(self::ARGUMENT_IMAP_PASSWORD);
91
92
		$smtpHost = $input->getArgument(self::ARGUMENT_SMTP_HOST);
93
		$smtpPort = $input->getArgument(self::ARGUMENT_SMTP_PORT);
94
		$smtpSslMode = $input->getArgument(self::ARGUMENT_SMTP_SSL_MODE);
95
		$smtpUser = $input->getArgument(self::ARGUMENT_SMTP_USER);
96
		$smtpPassword = $input->getArgument(self::ARGUMENT_SMTP_PASSWORD);
97
98
		$account = new MailAccount();
99
		$account->setUserId($userId);
100
		$account->setName($name);
101
		$account->setEmail($email);
102
103
		$account->setInboundHost($imapHost);
104
		$account->setInboundPort($imapPort);
105
		$account->setInboundSslMode($imapSslMode);
106
		$account->setInboundUser($imapUser);
107
		$account->setInboundPassword($this->crypto->encrypt($imapPassword));
108
109
		$account->setOutboundHost($smtpHost);
110
		$account->setOutboundPort($smtpPort);
111
		$account->setOutboundSslMode($smtpSslMode);
112
		$account->setOutboundUser($smtpUser);
113
		$account->setOutboundPassword($this->crypto->encrypt($smtpPassword));
114
115
		$this->accountService->save($account);
116
117
		$output->writeln("<info>Account $email created</info>");
118
	}
119
120
}
121