Completed
Pull Request — master (#641)
by Maxence
02:32
created

AdminController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Controller;
33
34
35
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Deserialize;
36
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
37
use Exception;
38
use OCA\Circles\Exceptions\ContactAddressBookNotFoundException;
39
use OCA\Circles\Exceptions\ContactFormatException;
40
use OCA\Circles\Exceptions\ContactNotFoundException;
41
use OCA\Circles\Exceptions\FederatedUserException;
42
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
43
use OCA\Circles\Exceptions\InvalidIdException;
44
use OCA\Circles\Exceptions\RequestBuilderException;
45
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
46
use OCA\Circles\Service\CircleService;
47
use OCA\Circles\Service\ConfigService;
48
use OCA\Circles\Service\FederatedUserService;
49
use OCA\Circles\Service\MemberService;
50
use OCA\Circles\Service\MembershipService;
51
use OCA\Circles\Service\SearchService;
52
use OCP\AppFramework\Http\DataResponse;
53
use OCP\AppFramework\OCS\OCSException;
54
use OCP\AppFramework\OCSController;
55
use OCP\IRequest;
56
use OCP\IUserSession;
57
58
59
/**
60
 * Class AdminController
61
 *
62
 * @package OCA\Circles\Controller
63
 */
64
class AdminController extends OcsController {
65
66
67
	use TNC22Deserialize;
68
	use TNC22Logger;
69
70
71
	/** @var IUserSession */
72
	private $userSession;
73
74
	/** @var FederatedUserService */
75
	private $federatedUserService;
76
77
	/** @var CircleService */
78
	private $circleService;
79
80
	/** @var MemberService */
81
	private $memberService;
82
83
	/** @var MembershipService */
84
	private $membershipService;
85
86
	/** @var SearchService */
87
	private $searchService;
88
89
	/** @var ConfigService */
90
	protected $configService;
91
92
93
	/**
94
	 * LocalController constructor.
95
	 *
96
	 * @param string $appName
97
	 * @param IRequest $request
98
	 * @param IUserSession $userSession
99
	 * @param FederatedUserService $federatedUserService
100
	 * @param CircleService $circleService
101
	 * @param MemberService $memberService
102
	 * @param MembershipService $membershipService
103
	 * @param SearchService $searchService
104
	 * @param ConfigService $configService
105
	 */
106 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...
107
		string $appName,
108
		IRequest $request,
109
		IUserSession $userSession,
110
		FederatedUserService $federatedUserService,
111
		CircleService $circleService,
112
		MemberService $memberService,
113
		MembershipService $membershipService,
114
		SearchService $searchService,
115
		ConfigService $configService
116
	) {
117
		parent::__construct($appName, $request);
118
		$this->userSession = $userSession;
119
		$this->federatedUserService = $federatedUserService;
120
		$this->circleService = $circleService;
121
		$this->memberService = $memberService;
122
		$this->membershipService = $membershipService;
123
		$this->searchService = $searchService;
124
		$this->configService = $configService;
125
126
		$this->setup('app', 'circles');
127
	}
128
129
130
	/**
131
	 * @param string $userId
132
	 * @param string $name
133
	 * @param bool $personal
134
	 * @param bool $local
135
	 *
136
	 * @return DataResponse
137
	 * @throws OCSException
138
	 */
139 View Code Duplication
	public function create(
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...
140
		string $userId,
141
		string $name,
142
		bool $personal = false,
143
		bool $local = false
144
	): DataResponse {
145
		try {
146
			$this->setLocalFederatedUser($userId);
147
			$circle = $this->circleService->create($name, null, $personal, $local);
148
149
			return new DataResponse($this->serializeArray($circle));
150
		} catch (Exception $e) {
151
			throw new OcsException($e->getMessage(), $e->getCode());
152
		}
153
	}
154
155
	/**
156
	 * @param string $userId
157
	 *
158
	 * @return DataResponse
159
	 * @throws OCSException
160
	 */
161 View Code Duplication
	public function circles(string $userId): DataResponse {
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...
162
		try {
163
			$this->setLocalFederatedUser($userId);
164
165
			return new DataResponse($this->serializeArray($this->circleService->getCircles()));
166
		} catch (Exception $e) {
167
			throw new OCSException($e->getMessage(), $e->getCode());
168
		}
169
	}
170
171
172
	/**
173
	 * @param string $userId
174
	 *
175
	 * @throws FederatedUserException
176
	 * @throws FederatedUserNotFoundException
177
	 * @throws InvalidIdException
178
	 * @throws RequestBuilderException
179
	 * @throws SingleCircleNotFoundException
180
	 * @throws ContactAddressBookNotFoundException
181
	 * @throws ContactFormatException
182
	 * @throws ContactNotFoundException
183
	 */
184
	private function setLocalFederatedUser(string $userId): void {
185
		$user = $this->userSession->getUser();
186
		$this->federatedUserService->setCurrentPatron($user->getUID());
187
		$this->federatedUserService->setLocalCurrentUserId($userId);
188
	}
189
190
}
191
192