Completed
Push — master ( c57b95...1d3e11 )
by Maxence
30s queued 14s
created

CircleConfig::verify()   D

Complexity

Conditions 18
Paths 192

Size

Total Lines 61

Duplication

Lines 15
Ratio 24.59 %

Importance

Changes 0
Metric Value
dl 15
loc 61
rs 4.1
c 0
b 0
f 0
cc 18
nc 192
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\FederatedItems;
33
34
35
use OCA\Circles\Db\CircleRequest;
36
use OCA\Circles\Exceptions\FederatedItemBadRequestException;
37
use OCA\Circles\Exceptions\FederatedItemException;
38
use OCA\Circles\IFederatedItem;
39
use OCA\Circles\IFederatedItemAsyncProcess;
40
use OCA\Circles\Model\Circle;
41
use OCA\Circles\Model\Federated\FederatedEvent;
42
use OCA\Circles\Model\Helpers\MemberHelper;
43
44
45
/**
46
 * Class CircleConfig
47
 *
48
 * @package OCA\Circles\FederatedItems
49
 */
50
class CircleConfig implements
51
	IFederatedItem,
52
	IFederatedItemAsyncProcess {
53
54
55
	/** @var CircleRequest */
56
	private $circleRequest;
57
58
59
	/**
60
	 * CircleConfig constructor.
61
	 *
62
	 * @param CircleRequest $circleRequest
63
	 */
64
	public function __construct(CircleRequest $circleRequest) {
65
		$this->circleRequest = $circleRequest;
66
	}
67
68
69
	/**
70
	 * @param FederatedEvent $event
71
	 *
72
	 * @throws FederatedItemException
73
	 */
74
	public function verify(FederatedEvent $event): void {
75
		$circle = $event->getCircle();
76
		$config = $event->getData()->gInt('config');
77
78
		$initiatorHelper = new MemberHelper($circle->getInitiator());
79
		$initiatorHelper->mustBeAdmin();
80
81
		$listing = Circle::$DEF_CFG_CORE_FILTER;
82
		if (!$circle->isConfig(Circle::CFG_SYSTEM)) {
83
			$listing = array_merge($listing, Circle::$DEF_CFG_SYSTEM_FILTER);
84
		}
85
86
		$confirmed = true;
87
		foreach ($listing as $item) {
88
			if ($circle->isConfig($item, $config)) {
89
				$confirmed = false;
90
			}
91
		}
92
93
		if (!$circle->isConfig(Circle::CFG_OPEN, $config)
94
			&& $circle->isConfig(Circle::CFG_OPEN)
95
			&& $circle->isConfig(Circle::CFG_REQUEST, $config)
96
		) {
97
			$config -= Circle::CFG_REQUEST;
98
			$event->getData()->sInt('config', $config);
99
		}
100
101 View Code Duplication
		if ($circle->isConfig(Circle::CFG_REQUEST, $config)
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...
102
			&& !$circle->isConfig(Circle::CFG_REQUEST)
103
			&& !$circle->isConfig(Circle::CFG_OPEN, $config)) {
104
			$config += Circle::CFG_OPEN;
105
			$event->getData()->sInt('config', $config);
106
		}
107
108
		if (!$circle->isConfig(Circle::CFG_ROOT, $config)
109
			&& $circle->isConfig(Circle::CFG_ROOT)
110
			&& $circle->isConfig(Circle::CFG_FEDERATED, $config)) {
111
			$config -= Circle::CFG_FEDERATED;
112
			// TODO: Broadcast message to other instances about loosing federated tag.
113
			$event->getData()->sInt('config', $config);
114
		}
115
116 View Code Duplication
		if ($circle->isConfig(Circle::CFG_FEDERATED, $config)
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...
117
			&& !$circle->isConfig(Circle::CFG_FEDERATED)
118
			&& !$circle->isConfig(Circle::CFG_ROOT, $config)) {
119
			$config += Circle::CFG_ROOT;
120
			// TODO: Check locally that circle is not a member of another circle.
121
			// TODO  in that case, remove the membership (and update the memberships)
122
			$event->getData()->sInt('config', $config);
123
			$event->getData()->sBool('broadcastAsFederated', true);
124
		}
125
126
		if (!$confirmed || $config > Circle::$DEF_CFG_MAX) {
127
			throw new FederatedItemBadRequestException('Configuration value is not valid');
128
		}
129
130
		$new = clone $circle;
131
		$new->setConfig($config);
132
133
		$event->setOutcome($new->jsonSerialize());
134
	}
135
136
137
	/**
138
	 * @param FederatedEvent $event
139
	 */
140
	public function manage(FederatedEvent $event): void {
141
		$circle = clone $event->getCircle();
142
		$config = $event->getData()->gInt('config');
143
144
		$circle->setConfig($config);
145
		// TODO: Check locally that circle is not un-federated during the process
146
		// TODO: if the circle is managed remotely, remove the circle locally
147
		// TODO: if the circle is managed locally, remove non-local users
148
149
		// TODO: Check locally that circle is not federated during the process
150
		// TODO: sync if it is to broadcast to Trusted RemoteInstance
151
152
		$this->circleRequest->updateConfig($circle);
153
	}
154
155
156
	/**
157
	 * @param FederatedEvent $event
158
	 * @param array $results
159
	 */
160
	public function result(FederatedEvent $event, array $results): void {
161
	}
162
163
}
164
165