Completed
Push — master ( dccb89...31024b )
by Morris
12:39
created

SyncFederationAddressBooks   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
C syncThemAll() 0 34 8
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
namespace OCA\Federation;
25
26
use OC\OCS\DiscoveryService;
27
use OCA\DAV\CardDAV\SyncService;
28
use OCP\AppFramework\Http;
29
use OCP\OCS\IDiscoveryService;
30
31
class SyncFederationAddressBooks {
32
33
	/** @var DbHandler */
34
	protected $dbHandler;
35
36
	/** @var SyncService */
37
	private $syncService;
38
39
	/** @var  DiscoveryService */
40
	private $ocsDiscoveryService;
41
42
	/**
43
	 * @param DbHandler $dbHandler
44
	 * @param SyncService $syncService
45
	 * @param IDiscoveryService $ocsDiscoveryService
46
	 */
47
	public function __construct(DbHandler $dbHandler,
48
								SyncService $syncService,
49
								IDiscoveryService $ocsDiscoveryService
50
	) {
51
		$this->syncService = $syncService;
52
		$this->dbHandler = $dbHandler;
53
		$this->ocsDiscoveryService = $ocsDiscoveryService;
0 ignored issues
show
Documentation Bug introduced by
$ocsDiscoveryService is of type object<OCP\OCS\IDiscoveryService>, but the property $ocsDiscoveryService was declared to be of type object<OC\OCS\DiscoveryService>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
54
	}
55
56
	/**
57
	 * @param \Closure $callback
58
	 */
59
	public function syncThemAll(\Closure $callback) {
60
61
		$trustedServers = $this->dbHandler->getAllServer();
62
		foreach ($trustedServers as $trustedServer) {
63
			$url = $trustedServer['url'];
64
			$callback($url, null);
65
			$sharedSecret = $trustedServer['shared_secret'];
66
			$syncToken = $trustedServer['sync_token'];
67
68
			$endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING');
69
			$cardDavUser = isset($endPoints['carddav-user']) ? $endPoints['carddav-user'] : 'system';
70
			$addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system';
71
72
			if (is_null($sharedSecret)) {
73
				continue;
74
			}
75
			$targetBookId = $trustedServer['url_hash'];
76
			$targetPrincipal = "principals/system/system";
77
			$targetBookProperties = [
78
					'{DAV:}displayname' => $url
79
			];
80
			try {
81
				$newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
82
				if ($newToken !== $syncToken) {
83
					$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
84
				}
85
			} catch (\Exception $ex) {
86
				if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
87
					$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
88
				}
89
				$callback($url, $ex);
90
			}
91
		}
92
	}
93
}
94