Completed
Pull Request — master (#379)
by Maxence
01:44
created

MembersCreate::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
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\Circles\Command;
31
32
use Exception;
33
use OC\Core\Command\Base;
34
use OC\User\NoUserException;
35
use OCA\Circles\Db\MembersRequest;
36
use OCA\Circles\Model\Member;
37
use OCA\Circles\Service\MembersService;
38
use OCP\IL10N;
39
use OCP\IUserManager;
40
use Symfony\Component\Console\Input\InputArgument;
41
use Symfony\Component\Console\Input\InputInterface;
42
use Symfony\Component\Console\Output\OutputInterface;
43
44
45
/**
46
 * Class MembersCreate
47
 *
48
 * @package OCA\Circles\Command
49
 */
50
class MembersCreate extends Base {
51
52
53
	/** @var IL10N */
54
	private $l10n;
55
56
	/** @var IUserManager */
57
	private $userManager;
58
59
	/** @var MembersService */
60
	private $membersService;
61
62
	/** @var MembersRequest */
63
	private $membersRequest;
64
65
66
	/**
67
	 * MembersCreate constructor.
68
	 *
69
	 * @param IL10N $l10n
70
	 * @param IUserManager $userManager
71
	 * @param MembersService $membersService
72
	 * @param MembersRequest $membersRequest
73
	 */
74 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
		IL10N $l10n, IUserManager $userManager, MembersService $membersService, MembersRequest $membersRequest
76
	) {
77
		parent::__construct();
78
		$this->l10n = $l10n;
79
		$this->userManager = $userManager;
80
		$this->membersService = $membersService;
81
		$this->membersRequest = $membersRequest;
82
	}
83
84
85
	protected function configure() {
86
		parent::configure();
87
		$this->setName('circles:members:create')
88
			 ->setDescription('create a new member')
89
			 ->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the circle')
90
			 ->addArgument('user', InputArgument::REQUIRED, 'username of the member')
91
			 ->addArgument('level', InputArgument::OPTIONAL, 'level of the member', 'member');
92
	}
93
94
95
	/**
96
	 * @param InputInterface $input
97
	 * @param OutputInterface $output
98
	 *
99
	 * @return int
100
	 * @throws NoUserException
101
	 * @throws Exception
102
	 */
103
	protected function execute(InputInterface $input, OutputInterface $output): int {
104
		$circleId = $input->getArgument('circle_id');
105
		$userId = $input->getArgument('user');
106
		$level = $input->getArgument('level');
107
108
		$user = $this->userManager->get($userId);
109
		if ($user === null) {
110
			throw new NoUserException('user does not exist');
111
		}
112
		$userId = $user->getUID();
113
114
		$levels = [
115
			'member'    => Member::LEVEL_MEMBER,
116
			'moderator' => Member::LEVEL_MODERATOR,
117
			'admin'     => Member::LEVEL_ADMIN,
118
			'owner'     => Member::LEVEL_OWNER
119
		];
120
121 View Code Duplication
		if (!key_exists(strtolower($level), $levels)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
			throw new Exception('unknown level: ' . json_encode(array_keys($levels)));
123
		}
124
125
		$level = $levels[strtolower($level)];
126
127
		$this->membersService->addMember($circleId, $userId, Member::TYPE_USER, true);
128
		$this->membersService->levelMember($circleId, $userId, Member::TYPE_USER, $level, true);
129
130
		$member = $this->membersRequest->forceGetMember($circleId, $userId, Member::TYPE_USER);
131
		echo json_encode($member, JSON_PRETTY_PRINT) . "\n";
132
133
		return 0;
134
	}
135
136
}
137
138