Completed
Pull Request — master (#634)
by Maxence
02:50
created

MaintenanceService::runMaintenance()   C

Complexity

Conditions 11
Paths 216

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 5.8993
c 0
b 0
f 0
cc 11
nc 216
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Service;
33
34
35
use ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore;
36
use Exception;
37
use OCA\Circles\Db\CircleRequest;
38
use OCA\Circles\Db\MemberRequest;
39
use OCA\Circles\Exceptions\InitiatorNotFoundException;
40
use OCA\Circles\Exceptions\RequestBuilderException;
41
use OCA\Circles\Model\Circle;
42
use OCA\Circles\Model\Member;
43
use OCP\IUserManager;
44
use Symfony\Component\Console\Output\OutputInterface;
45
46
47
/**
48
 * Class MaintenanceService
49
 *
50
 * @package OCA\Circles\Service
51
 */
52
class MaintenanceService {
53
54
55
	/** @var IUserManager */
56
	private $userManager;
57
58
	/** @var CircleRequest */
59
	private $circleRequest;
60
61
	/** @var MemberRequest */
62
	private $memberRequest;
63
64
	/** @var FederatedUserService */
65
	private $federatedUserService;
66
67
	/** @var EventWrapperService */
68
	private $eventWrapperService;
69
70
	/** @var CircleService */
71
	private $circleService;
72
73
74
	/** @var OutputInterface */
75
	private $output;
76
77
78
	/**
79
	 * MaintenanceService constructor.
80
	 *
81
	 * @param IUserManager $userManager
82
	 * @param CircleRequest $circleRequest
83
	 * @param MemberRequest $memberRequest
84
	 * @param FederatedUserService $federatedUserService
85
	 * @param EventWrapperService $eventWrapperService
86
	 * @param CircleService $circleService
87
	 */
88
	public function __construct(
89
		IUserManager $userManager,
90
		CircleRequest $circleRequest,
91
		MemberRequest $memberRequest,
92
		FederatedUserService $federatedUserService,
93
		EventWrapperService $eventWrapperService,
94
		CircleService $circleService
95
	) {
96
		$this->userManager = $userManager;
97
		$this->circleRequest = $circleRequest;
98
		$this->memberRequest = $memberRequest;
99
		$this->federatedUserService = $federatedUserService;
100
		$this->eventWrapperService = $eventWrapperService;
101
		$this->circleService = $circleService;
102
	}
103
104
105
	/**
106
	 * @param OutputInterface $output
107
	 */
108
	public function setOccOutput(OutputInterface $output): void {
109
		$this->output = $output;
110
	}
111
112
113
	/**
114
	 *
115
	 */
116
	public function runMaintenance(int $level = 0): void {
117
		$this->federatedUserService->bypassCurrentUserCondition(true);
118
119
		try {
120
			$this->output('remove circles with no owner');
121
			$this->removeCirclesWithNoOwner();
122
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
123
		}
124
125
		try {
126
			$this->output('remove members with no circles');
127
			$this->removeMembersWithNoCircles();
0 ignored issues
show
Unused Code introduced by
The call to the method OCA\Circles\Service\Main...eMembersWithNoCircles() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
128
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
129
		}
130
131
132
		try {
133
			// TODO: waiting for confirmation of a good migration before cleaning orphan shares
134
//			$this->output('remove deprecated shares');
135
//		$this->removeDeprecatedShares();
136
		} catch (Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
137
		}
138
139
		try {
140
			$this->output('retry failed FederatedEvents');
141
			$this->eventWrapperService->retry();
142
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
143
		}
144
145
		if ($level < 1) {
146
			return;
147
		}
148
149
		if ($level < 2) {
150
			return;
151
		}
152
153
		if ($level < 3) {
154
			return;
155
		}
156
157
//		if ($level < 5) {
158
//			$this->output('refresh displayNames older than 7d');
159
//			//	$this->refreshOldDisplayNames();
160
//		}
161
//
162
		if ($level < 4) {
163
			return;
164
		}
165
166
		if ($level < 5) {
167
			return;
168
		}
169
170
		try {
171
			$this->output('refresh DisplayNames');
172
			$this->refreshDisplayName();
173
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
174
		}
175
	}
176
177
178
	/**
179
	 * @throws InitiatorNotFoundException
180
	 * @throws RequestBuilderException
181
	 */
182
	private function removeCirclesWithNoOwner(): void {
183
		$circles = $this->circleService->getCircles();
184
		foreach ($circles as $circle) {
185
			if (!$circle->hasOwner()) {
186
				$this->circleRequest->delete($circle);
187
			}
188
		}
189
	}
190
191
192
	/**
193
	 *
194
	 */
195
	private function removeMembersWithNoCircles(): void {
196
//		$members = $this->membersRequest->forceGetAllMembers();
197
//
198
//		foreach ($members as $member) {
199
//			try {
200
//				$this->circlesRequest->forceGetCircle($member->getCircleId());
201
//			} catch (CircleDoesNotExistException $e) {
202
//				$this->membersRequest->removeMember($member);
203
//			}
204
//		}
205
	}
206
207
208
	private function removeDeprecatedShares(): void {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
209
//		$circles = array_map(
210
//			function(DeprecatedCircle $circle) {
211
//				return $circle->getUniqueId();
212
//			}, $this->circlesRequest->forceGetCircles()
213
//		);
214
//
215
//		$shares = array_unique(
216
//			array_map(
217
//				function($share) {
218
//					return $share['share_with'];
219
//				}, $this->fileSharesRequest->getShares()
220
//			)
221
//		);
222
//
223
//		foreach ($shares as $share) {
224
//			if (!in_array($share, $circles)) {
225
//				$this->fileSharesRequest->removeSharesToCircleId($share);
226
//			}
227
//		}
228
	}
229
230
231
	/**
232
	 * @throws RequestBuilderException
233
	 * @throws InitiatorNotFoundException
234
	 */
235
	private function refreshDisplayName(): void {
236
		$params = new SimpleDataStore(['includeSystemCircles' => true]);
237
		$circleFilter = new Circle();
238
		$circleFilter->setConfig(Circle::CFG_SINGLE);
239
		$circles = $this->circleService->getCircles($circleFilter, null, $params);
240
241
		foreach ($circles as $circle) {
242
			$owner = $circle->getOwner();
243
			if ($owner->getUserType() === Member::TYPE_USER) {
244
				$user = $this->userManager->get($owner->getUserId());
245
				$this->memberRequest->updateDisplayName($owner->getSingleId(), $user->getDisplayName());
246
				$this->circleRequest->updateDisplayName($owner->getSingleId(), $user->getDisplayName());
247
			}
248
		}
249
	}
250
251
	/**
252
	 * @param string $message
253
	 */
254
	private function output(string $message): void {
255
		if (!is_null($this->output)) {
256
			$this->output->writeln('- ' . $message);
257
		}
258
	}
259
260
}
261
262