Passed
Push — master ( a65c00...940a31 )
by Roeland
11:18 queued 02:26
created

UpdateLookupServer::sendToLookupServer()   F

Complexity

Conditions 14
Paths 4100

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 30
nc 4100
nop 2
dl 0
loc 45
rs 2.1
c 0
b 0
f 0

How to fix   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
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
5
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\LookupServerConnector;
25
26
use OCA\LookupServerConnector\BackgroundJobs\RetryJob;
27
use OCP\BackgroundJob\IJobList;
28
use OCP\IConfig;
29
use OCP\IUser;
30
31
/**
32
 * Class UpdateLookupServer
33
 *
34
 * @package OCA\LookupServerConnector
35
 */
36
class UpdateLookupServer {
37
	/** @var IConfig */
38
	private $config;
39
	/** @var IJobList */
40
	private $jobList;
41
42
	/**
43
	 * @param IJobList $jobList
44
	 * @param IConfig $config
45
	 */
46
	public function __construct(IJobList $jobList,
47
								IConfig $config) {
48
		$this->config = $config;
49
		$this->jobList = $jobList;
50
	}
51
52
	/**
53
	 * @param IUser $user
54
	 */
55
	public function userUpdated(IUser $user): void {
56
		if (!$this->shouldUpdateLookupServer()) {
57
			return;
58
		}
59
60
		// Reset retry counter
61
		$this->config->deleteUserValue(
62
			$user->getUID(),
63
			'lookup_server_connector',
64
			'update_retries'
65
		);
66
		$this->jobList->add(RetryJob::class, ['userId' => $user->getUID()]);
67
	}
68
69
	/**
70
	 * check if we should update the lookup server, we only do it if
71
	 *
72
	 * + we have an internet connection
73
	 * + the lookup server update was not disabled by the admin
74
	 * + we have a valid lookup server URL
75
	 *
76
	 * @return bool
77
	 */
78
	private function shouldUpdateLookupServer(): bool {
79
		return $this->config->getSystemValueBool('has_internet_connection', true) === true &&
80
			$this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') === 'yes' &&
81
			$this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') !== '';
82
	}
83
84
}
85