Completed
Push — master ( d4ce93...77c8bc )
by Maxence
02:50
created

MiscService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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\Service;
28
29
use Exception;
30
use OC\User\NoUserException;
31
use OCA\Circles\Exceptions\MissingKeyInArrayException;
32
use OCA\Circles\Model\Member;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
use OCP\ILogger;
36
use OCP\IUserManager;
37
38
class MiscService {
39
40
	/** @var ILogger */
41
	private $logger;
42
43
	/** @var string */
44
	private $appName;
45
46
	/** @var IUserManager */
47
	private $userManager;
48
49
	public function __construct(ILogger $logger, $appName, IUserManager $userManager) {
50
		$this->logger = $logger;
51
		$this->appName = $appName;
52
		$this->userManager = $userManager;
53
	}
54
55
	public function log($message, $level = 2) {
56
		$data = array(
57
			'app'   => $this->appName,
58
			'level' => $level
59
		);
60
61
		$this->logger->log($level, $message, $data);
62
	}
63
64
65
	/**
66
	 * @param $arr
67
	 * @param $k
68
	 *
69
	 * @return string|array
70
	 */
71
	public static function get($arr, $k) {
72
		if (!key_exists($k, $arr)) {
73
			return '';
74
		}
75
76
		return $arr[$k];
77
	}
78
79
80
	public static function mustContains($data, $arr) {
81
		if (!is_array($arr)) {
82
			$arr = [$arr];
83
		}
84
85
		foreach ($arr as $k) {
86
			if (!key_exists($k, $data)) {
87
				throw new MissingKeyInArrayException('missing_key_in_array');
88
			}
89
		}
90
	}
91
92
93
	/**
94
	 * @param $data
95
	 *
96
	 * @return DataResponse
97
	 */
98
	public function fail($data) {
99
		$this->log(json_encode($data));
100
101
		return new DataResponse(
102
			array_merge($data, array('status' => 0)),
103
			Http::STATUS_NON_AUTHORATIVE_INFORMATION
104
		);
105
	}
106
107
108
	/**
109
	 * @param $data
110
	 *
111
	 * @return DataResponse
112
	 */
113
	public function success($data) {
114
		return new DataResponse(
115
			array_merge($data, array('status' => 1)),
116
			Http::STATUS_CREATED
117
		);
118
	}
119
120
121
	/**
122
	 * return the real userId, with its real case
123
	 *
124
	 * @param $userId
125
	 *
126
	 * @return string
127
	 * @throws NoUserException
128
	 */
129
	public function getRealUserId($userId) {
130
		if (!$this->userManager->userExists($userId)) {
131
			throw new NoUserException();
132
		}
133
134
		return $this->userManager->get($userId)
135
								 ->getUID();
136
	}
137
138
139
	/**
140
	 * @param string $ident
141
	 * @param int $type
142
	 *
143
	 * @return string
144
	 */
145
	public static function getDisplay($ident, $type) {
146
		$display = $ident;
147
148
		self::getDisplayMember($display, $ident, $type);
149
		self::getDisplayContact($display, $ident, $type);
150
151
		return $display;
152
	}
153
154
155
	/**
156
	 * @param string $display
157
	 * @param string $ident
158
	 * @param int $type
159
	 */
160
	private static function getDisplayMember(&$display, $ident, $type) {
161
		if ($type !== Member::TYPE_USER) {
162
			return;
163
		}
164
165
		$user = \OC::$server->getUserManager()
166
							->get($ident);
167
		if ($user !== null) {
168
			$display = $user->getDisplayName();
169
		}
170
	}
171
172
173
	/**
174
	 * @param string $display
175
	 * @param string $ident
176
	 * @param int $type
177
	 */
178
	private static function getDisplayContact(&$display, $ident, $type) {
179
		if ($type !== Member::TYPE_CONTACT) {
180
			return;
181
		}
182
183
		$contact = self::getContactData($ident);
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Service\MiscService::getContactData() has been deprecated with message: - move this somewhere else, no static if possible

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
184
		self::getDisplayContactFromArray($display, $contact);
185
	}
186
187
188
	/**
189
	 * @param $ident
190
	 *
191
	 * @deprecated - move this somewhere else, no static if possible
192
	 * @return mixed|string
193
	 */
194
	public static function getContactData($ident) {
195
		list($userId, $contactId) = explode(':', $ident);
196
197
		try {
198
			/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
199
			$contactApp = new \OCA\DAV\AppInfo\Application();
200
			$cm = \OC::$server->getContactsManager();
201
			$contactApp->setupContactsProvider($cm, $userId);
202
			$contact = $cm->search($contactId, ['UID']);
203
204
			return array_shift($contact);
205
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
206
		}
207
208
		return null;
209
	}
210
211
212
	/**
213
	 * @param string $display
214
	 * @param array $contact
215
	 */
216
	private static function getDisplayContactFromArray(&$display, $contact) {
217
		if (key_exists('FN', $contact) && $contact['FN'] !== '') {
218
			$display = $contact['FN'];
219
220
			return;
221
		}
222
223 View Code Duplication
		if (key_exists('EMAIL', $contact) && $contact['EMAIL'] !== '') {
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...
224
			$display = $contact['EMAIL'];
225
226
			return;
227
		}
228
	}
229
230
	/**
231
	 * return Display Name if user exists and display name exists.
232
	 * returns Exception if user does not exist.
233
	 *
234
	 * However, with noException set to true, will return userId even if user does not exist
235
	 *
236
	 * @param $userId
237
	 * @param bool $noException
238
	 *
239
	 * @return string
240
	 * @throws NoUserException
241
	 */
242
	public function getDisplayName($userId, $noException = false) {
243
		$user = $this->userManager->get($userId);
244
		if ($user === null) {
245
			if ($noException) {
246
				return $userId;
247
			} else {
248
				throw new NoUserException();
249
			}
250
		}
251
252
		return $user->getDisplayName();
253
	}
254
255
256
	/**
257
	 * Hacky way to async the rest of the process without keeping client on hold.
258
	 *
259
	 * @param string $result
260
	 */
261
	public function asyncAndLeaveClientOutOfThis($result = '') {
262
		if (ob_get_contents() !== false) {
263
			ob_end_clean();
264
		}
265
266
		header('Connection: close');
267
		ignore_user_abort();
268
		ob_start();
269
		echo($result);
270
		$size = ob_get_length();
271
		header('Content-Length: ' . $size);
272
		ob_end_flush();
273
		flush();
274
	}
275
276
}
277
278