Completed
Push — master ( 3aeedd...be0cda )
by Maxence
03:35 queued 11s
created

MaintenanceService::runMaintenance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 OCA\Circles\Db\CircleRequest;
36
use OCA\Circles\Exceptions\InitiatorNotFoundException;
37
use OCA\Circles\Exceptions\RequestBuilderException;
38
use Symfony\Component\Console\Output\OutputInterface;
39
40
41
/**
42
 * Class MaintenanceService
43
 *
44
 * @package OCA\Circles\Service
45
 */
46
class MaintenanceService {
47
48
49
	/** @var CircleRequest */
50
	private $circleRequest;
51
52
	/** @var FederatedUserService */
53
	private $federatedUserService;
54
55
	/** @var EventWrapperService */
56
	private $eventWrapperService;
57
58
	/** @var CircleService */
59
	private $circleService;
60
61
62
	/** @var OutputInterface */
63
	private $output;
64
65
66
	/**
67
	 * MaintenanceService constructor.
68
	 *
69
	 * @param FederatedUserService $federatedUserService
70
	 * @param EventWrapperService $eventWrapperService
71
	 * @param CircleService $circleService
72
	 */
73
	public function __construct(
74
		CircleRequest $circleRequest,
75
		FederatedUserService $federatedUserService,
76
		EventWrapperService $eventWrapperService,
77
		CircleService $circleService
78
	) {
79
		$this->circleRequest = $circleRequest;
80
		$this->federatedUserService = $federatedUserService;
81
		$this->eventWrapperService = $eventWrapperService;
82
		$this->circleService = $circleService;
83
	}
84
85
86
	/**
87
	 * @param OutputInterface $output
88
	 */
89
	public function setOccOutput(OutputInterface $output): void {
90
		$this->output = $output;
91
	}
92
93
94
	/**
95
	 *
96
	 */
97
	public function runMaintenance(): void {
98
		$this->federatedUserService->bypassCurrentUserCondition(true);
99
100
		$this->output('remove circles with no owner');
101
		$this->removeCirclesWithNoOwner();
102
103
		$this->output('remove members with no circles');
104
		$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...
105
106
		$this->output('remove deprecated shares');
107
//		$this->removeDeprecatedShares();
108
109
		$this->output('retry failed FederatedEvents');
110
		$this->eventWrapperService->retry();
111
	}
112
113
114
	/**
115
	 * @throws InitiatorNotFoundException
116
	 * @throws RequestBuilderException
117
	 */
118
	private function removeCirclesWithNoOwner(): void {
119
		$circles = $this->circleService->getCircles();
120
		foreach ($circles as $circle) {
121
			if (!$circle->hasOwner()) {
122
				$this->circleRequest->delete($circle);
123
			}
124
		}
125
	}
126
127
128
	/**
129
	 *
130
	 */
131
	private function removeMembersWithNoCircles(): void {
132
//		$members = $this->membersRequest->forceGetAllMembers();
133
//
134
//		foreach ($members as $member) {
135
//			try {
136
//				$this->circlesRequest->forceGetCircle($member->getCircleId());
137
//			} catch (CircleDoesNotExistException $e) {
138
//				$this->membersRequest->removeMember($member);
139
//			}
140
//		}
141
	}
142
143
144
	private function removeDeprecatedShares(): void {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
145
//		$circles = array_map(
146
//			function(DeprecatedCircle $circle) {
147
//				return $circle->getUniqueId();
148
//			}, $this->circlesRequest->forceGetCircles()
149
//		);
150
//
151
//		$shares = array_unique(
152
//			array_map(
153
//				function($share) {
154
//					return $share['share_with'];
155
//				}, $this->fileSharesRequest->getShares()
156
//			)
157
//		);
158
//
159
//		foreach ($shares as $share) {
160
//			if (!in_array($share, $circles)) {
161
//				$this->fileSharesRequest->removeSharesToCircleId($share);
162
//			}
163
//		}
164
	}
165
166
167
	/**
168
	 * @param string $message
169
	 */
170
	private function output(string $message): void {
171
		if (!is_null($this->output)) {
172
			$this->output->writeln('- ' . $message);
173
		}
174
	}
175
176
}
177
178