Completed
Push — master ( 4f3689...17b456 )
by Maxence
02:12 queued 11s
created

GlobalScaleUsers::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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->setProtocols(['https', 'http']);
0 ignored issues
show
Bug introduced by
The method setProtocols() does not exist on daita\MySmallPhpTools\Model\Request. Did you maybe mean setProtocol()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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
		foreach ($users as $user) {
108
			list(, $instance) = explode('@', $this->get('federationId', $user), 2);
109
			if ($instance === $this->configService->getLocalCloudId()) {
110
				continue;
111
			}
112
113
			$result[] =
114
				new SearchResult(
115
					$this->get('userid.value', $user), Member::TYPE_USER, $instance,
116
					['display' => $this->get('name.value', $user)]
117
				);
118
		}
119
120
		return $result;
121
	}
122
}
123
124
125