Completed
Push — master ( 6b6a4c...4ac435 )
by Maxence
02:56 queued 01:19
created

CirclesCreate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 4
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 OC\Core\Command\Base;
33
use OC\User\NoUserException;
34
use OCA\Circles\Db\CirclesRequest;
35
use OCA\Circles\Exceptions\CircleAlreadyExistsException;
36
use OCA\Circles\Exceptions\CircleDoesNotExistException;
37
use OCA\Circles\Exceptions\CircleTypeDisabledException;
38
use OCA\Circles\Exceptions\CircleTypeNotValidException;
39
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
40
use OCA\Circles\Model\Circle;
41
use OCA\Circles\Service\CirclesService;
42
use OCP\IL10N;
43
use OCP\IUserManager;
44
use Symfony\Component\Console\Input\InputArgument;
45
use Symfony\Component\Console\Input\InputInterface;
46
use Symfony\Component\Console\Output\OutputInterface;
47
48
49
/**
50
 * Class CirclesCreate
51
 *
52
 * @package OCA\Circles\Command
53
 */
54
class CirclesCreate extends Base {
55
56
57
	/** @var IL10N */
58
	private $l10n;
59
60
	/** @var IUserManager */
61
	private $userManager;
62
63
	/** @var CirclesRequest */
64
	private $circlesRequest;
65
66
	/** @var CirclesService */
67
	private $circlesService;
68
69
70
	/**
71
	 * CirclesCreate constructor.
72
	 *
73
	 * @param IL10N $l10n
74
	 * @param IUserManager $userManager
75
	 * @param CirclesRequest $circlesRequest
76
	 * @param CirclesService $circlesService
77
	 */
78
	public function __construct(
79
		IL10N $l10n, IUserManager $userManager, CirclesRequest $circlesRequest, CirclesService $circlesService
80
	) {
81
		parent::__construct();
82
		$this->l10n = $l10n;
83
		$this->userManager = $userManager;
84
		$this->circlesRequest = $circlesRequest;
85
		$this->circlesService = $circlesService;
86
	}
87
88
89
	protected function configure() {
90
		parent::configure();
91
		$this->setName('circles:manage:create')
92
			 ->setDescription('create a new circle')
93
			 ->addArgument('owner', InputArgument::REQUIRED, 'owner of the circle')
94
			 ->addArgument('type', InputArgument::REQUIRED, 'type of the circle')
95
			 ->addArgument('name', InputArgument::REQUIRED, 'name of the circle');
96
	}
97
98
99
	/**
100
	 * @param InputInterface $input
101
	 * @param OutputInterface $output
102
	 *
103
	 * @return int
104
	 * @throws CircleAlreadyExistsException
105
	 * @throws CircleTypeNotValidException
106
	 * @throws MemberAlreadyExistsException
107
	 * @throws NoUserException
108
	 * @throws CircleTypeDisabledException
109
	 * @throws CircleDoesNotExistException
110
	 */
111
	protected function execute(InputInterface $input, OutputInterface $output): int {
112
		$ownerId = $input->getArgument('owner');
113
		$type = $input->getArgument('type');
114
		$name = $input->getArgument('name');
115
116
		if ($this->userManager->get($ownerId) === null) {
117
			throw new NoUserException('user does not exist');
118
		}
119
120
		$types = [
121
			'personal' => Circle::CIRCLES_PERSONAL,
122
			'secret'   => Circle::CIRCLES_SECRET,
123
			'closed'   => Circle::CIRCLES_CLOSED,
124
			'public'   => Circle::CIRCLES_PUBLIC
125
		];
126
127 View Code Duplication
		if (!key_exists(strtolower($type), $types)) {
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...
128
			throw new CircleTypeNotValidException('unknown type: ' . json_encode(array_keys($types)));
129
		}
130
131
		$type = $types[strtolower($type)];
132
133
		$circle = $this->circlesService->createCircle($type, $name, $ownerId);
134
		$circle = $this->circlesRequest->forceGetCircle($circle->getUniqueId());
135
136
		echo json_encode($circle, JSON_PRETTY_PRINT) . "\n";
137
138
		return 0;
139
	}
140
141
}
142
143