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

MembersLevel   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 83
Duplicated Lines 14.46 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 12
loc 83
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A configure() 0 7 1
A execute() 3 29 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 MembersLevel
47
 *
48
 * @package OCA\Circles\Command
49
 */
50
class MembersLevel extends Base {
51
52
53
	/** @var IL10N */
54
	private $l10n;
55
56
	/** @var IUserManager */
57
	private $userManager;
58
59
	/** @var MembersRequest */
60
	private $membersRequest;
61
62
	/** @var MembersService */
63
	private $membersService;
64
65
66
	/**
67
	 * MembersLevel constructor.
68
	 *
69
	 * @param IL10N $l10n
70
	 * @param IUserManager $userManager
71
	 * @param MembersRequest $membersRequest
72
	 * @param MembersService $membersService
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, MembersRequest $membersRequest, MembersService $membersService
76
	) {
77
		parent::__construct();
78
		$this->l10n = $l10n;
79
		$this->userManager = $userManager;
80
		$this->membersRequest = $membersRequest;
81
		$this->membersService = $membersService;
82
	}
83
84
85
	protected function configure() {
86
		parent::configure();
87
		$this->setName('circles:members:level')
88
			 ->setDescription('Change level of a member')
89
			 ->addArgument('member_id', InputArgument::REQUIRED, 'ID of the member')
90
			 ->addArgument('level', InputArgument::REQUIRED, 'new level');
91
	}
92
93
94
	/**
95
	 * @param InputInterface $input
96
	 * @param OutputInterface $output
97
	 *
98
	 * @return int
99
	 * @throws NoUserException
100
	 * @throws Exception
101
	 */
102
	protected function execute(InputInterface $input, OutputInterface $output): int {
103
		$memberId = $input->getArgument('member_id');
104
		$level = $input->getArgument('level');
105
106
		$levels = [
107
			'member'    => Member::LEVEL_MEMBER,
108
			'moderator' => Member::LEVEL_MODERATOR,
109
			'admin'     => Member::LEVEL_ADMIN,
110
			'owner'     => Member::LEVEL_OWNER
111
		];
112
113 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...
114
			throw new Exception('unknown level: ' . json_encode(array_keys($levels)));
115
		}
116
117
		$level = $levels[strtolower($level)];
118
119
		$member = $this->membersService->getMemberById($memberId);
120
		$this->membersService->levelMember(
121
			$member->getCircleId(), $member->getUserId(), Member::TYPE_USER, $level, true
122
		);
123
124
		$member = $this->membersRequest->forceGetMember(
125
			$member->getCircleId(), $member->getUserId(), Member::TYPE_USER
126
		);
127
		echo json_encode($member, JSON_PRETTY_PRINT) . "\n";
128
129
		return 0;
130
	}
131
132
}
133
134