Completed
Pull Request — master (#362)
by Maxence
02:45
created

GlobalScaleUsers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 74
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B search() 0 45 5
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Search;
28
29
use daita\MySmallPhpTools\Exceptions\RequestContentException;
30
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
31
use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException;
32
use daita\MySmallPhpTools\Exceptions\RequestResultSizeException;
33
use daita\MySmallPhpTools\Exceptions\RequestServerException;
34
use daita\MySmallPhpTools\Model\Request;
35
use daita\MySmallPhpTools\Traits\TArrayTools;
36
use daita\MySmallPhpTools\Traits\TRequest;
37
use OCA\Circles\Exceptions\GSStatusException;
38
use OCA\Circles\ISearch;
39
use OCA\Circles\Model\Member;
40
use OCA\Circles\Model\SearchResult;
41
use OCA\Circles\Service\ConfigService;
42
use OCA\Circles\Service\MiscService;
43
44
45
/**
46
 * Class GlobalScaleUsers
47
 *
48
 * @package OCA\Circles\Search
49
 */
50
class GlobalScaleUsers implements ISearch {
51
52
53
	use TRequest;
54
	use TArrayTools;
55
56
57
	/** @var ConfigService */
58
	private $configService;
59
60
	/** @var MiscService */
61
	private $miscService;
62
63
64
	/**
65
	 * GlobalScaleUsers constructor.
66
	 *
67
	 * @param ConfigService $configService
68
	 * @param MiscService $miscService
69
	 */
70
	public function __construct(ConfigService $configService, MiscService $miscService) {
71
		$this->configService = $configService;
72
		$this->miscService = $miscService;
73
	}
74
75
	/**
76
	 * {@inheritdoc}
77
	 */
78
	public function search($search) {
79
80
		/** @var string $lookup */
81
		try {
82
			$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
83
		} catch (GSStatusException $e) {
84
			return;
85
		}
86
87
		$request = new Request('/users', Request::TYPE_GET);
88
		$request->setProtocol($_SERVER['REQUEST_SCHEME']);
89
		$request->addData('search', $search);
90
		$request->setAddressFromUrl($lookup);
91
92
		try {
93
			$users = $this->retrieveJson($request);
94
		} catch (
95
		RequestContentException |
96
		RequestNetworkException |
97
		RequestResultSizeException |
98
		RequestServerException |
99
		RequestResultNotJsonException $e
100
		) {
101
			$this->miscService->log('Issue while retrieving instances from lookup: ' . $e->getMessage());
102
103
			return [];
104
		}
105
106
		$result = [];
107
//		{\"federationId\":\"[email protected]\",\"name\":{\"value\":\"gs3test1\",\"verified\":\"0\"},\"userid\":{\"value\":\"gs3test1\",\"verified\":\"0\"}}
108
		foreach ($users as $user) {
109
			list(, $instance) = explode('@', $this->get('federationId', $user), 2);
110
			if ($instance === $this->configService->getLocalCloudId()) {
111
				continue;
112
			}
113
114
			$result[] =
115
				new SearchResult(
116
					$this->get('userid.value', $user), Member::TYPE_USER, $instance,
117
					['display' => $this->get('name.value', $user)]
118
				);
119
		}
120
121
		return $result;
122
	}
123
}
124
125
126