Completed
Push — master ( b87cc9...cbc740 )
by Maxence
02:28
created

CirclesEdit::execute()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.6897
c 0
b 0
f 0
cc 6
nc 12
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Command;
33
34
use OC\Core\Command\Base;
35
use OCA\Circles\Exceptions\CircleNotFoundException;
36
use OCA\Circles\Exceptions\FederatedItemException;
37
use OCA\Circles\Exceptions\FederatedUserException;
38
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
39
use OCA\Circles\Exceptions\InvalidIdException;
40
use OCA\Circles\Exceptions\MemberNotFoundException;
41
use OCA\Circles\Exceptions\OwnerNotFoundException;
42
use OCA\Circles\Exceptions\RemoteInstanceException;
43
use OCA\Circles\Exceptions\RemoteNotFoundException;
44
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
45
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
46
use OCA\Circles\Exceptions\UnknownRemoteException;
47
use OCA\Circles\Exceptions\UserTypeNotFoundException;
48
use OCA\Circles\Service\CircleService;
49
use OCA\Circles\Service\ConfigService;
50
use OCA\Circles\Service\FederatedUserService;
51
use OCP\IL10N;
52
use Symfony\Component\Console\Exception\InvalidArgumentException;
53
use Symfony\Component\Console\Input\InputArgument;
54
use Symfony\Component\Console\Input\InputInterface;
55
use Symfony\Component\Console\Input\InputOption;
56
use Symfony\Component\Console\Output\OutputInterface;
57
58
59
/**
60
 * Class CirclesEdit
61
 *
62
 * @package OCA\Circles\Command
63
 */
64
class CirclesEdit extends Base {
65
66
67
	/** @var FederatedUserService */
68
	private $federatedUserService;
69
70
	/** @var CircleService */
71
	private $circleService;
72
73
74
	/**
75
	 * CirclesEdit constructor.
76
	 *
77
	 * @param IL10N $l10n
78
	 * @param FederatedUserService $federatedUserService
79
	 * @param CircleService $circlesService
80
	 * @param ConfigService $configService
81
	 */
82
	public function __construct(
83
		IL10N $l10n, FederatedUserService $federatedUserService, CircleService $circlesService,
84
		ConfigService $configService
85
	) {
86
		parent::__construct();
87
88
		$this->federatedUserService = $federatedUserService;
89
		$this->circleService = $circlesService;
90
	}
91
92
93
	/**
94
	 *
95
	 */
96 View Code Duplication
	protected function configure() {
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...
97
		parent::configure();
98
		$this->setName('circles:manage:edit')
99
			 ->setDescription('edit displayName or description of a Circle')
100
			 ->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the circle')
101
			 ->addArgument('edit', InputArgument::REQUIRED, 'displayName or description')
102
			 ->addArgument('value', InputArgument::REQUIRED, 'new value')
103
			 ->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '')
104
			 ->addOption('status-code', '', InputOption::VALUE_NONE, 'display status code on exception');
105
106
	}
107
108
109
	/**
110
	 * @param InputInterface $input
111
	 * @param OutputInterface $output
112
	 *
113
	 * @return int
114
	 * @throws FederatedItemException
115
	 * @throws CircleNotFoundException
116
	 * @throws FederatedUserException
117
	 * @throws FederatedUserNotFoundException
118
	 * @throws InvalidIdException
119
	 * @throws MemberNotFoundException
120
	 * @throws OwnerNotFoundException
121
	 * @throws RemoteInstanceException
122
	 * @throws RemoteNotFoundException
123
	 * @throws RemoteResourceNotFoundException
124
	 * @throws SingleCircleNotFoundException
125
	 * @throws UnknownRemoteException
126
	 * @throws UserTypeNotFoundException
127
	 */
128
	protected function execute(InputInterface $input, OutputInterface $output): int {
129
		$circleId = (string)$input->getArgument('circle_id');
130
		$edit = strtolower((string)$input->getArgument('edit'));
131
		$newValue = (string)$input->getArgument('value');
132
133
		try {
134
			$this->federatedUserService->commandLineInitiator(
135
				$input->getOption('initiator'), $circleId, false
136
			);
137
138
			switch ($edit) {
139
				case 'displayname':
140
					$outcome = $this->circleService->updateDisplayName($circleId, $newValue);
141
					break;
142
143
				case 'description':
144
					$outcome = $this->circleService->updateDescription($circleId, $newValue);
145
					break;
146
147
				default:
148
					throw new InvalidArgumentException('edit can only be \'displayName\' or \'description\'');
149
			}
150
		} catch (FederatedItemException $e) {
151
			if ($input->getOption('status-code')) {
152
				throw new FederatedItemException(
153
					' [' . get_class($e) . ', ' . $e->getStatus() . ']' . "\n" . $e->getMessage()
154
				);
155
			}
156
157
			throw $e;
158
		}
159
160
		if (strtolower($input->getOption('output')) === 'json') {
161
			$output->writeln(json_encode($outcome, JSON_PRETTY_PRINT));
162
		}
163
164
		return 0;
165
	}
166
167
}
168
169