Completed
Push — master ( df8ec4...96358d )
by Nazar
04:25
created

profiles   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 44
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B profiles_get() 0 42 6
1
<?php
2
/**
3
 * @package    CleverStyle CMS
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @copyright  Copyright (c) 2015, Nazar Mokrynskyi
8
 * @license    MIT License, see license.txt
9
 */
10
namespace cs\modules\System\api\Controller;
11
use
12
	cs\ExitException,
13
	cs\Page,
14
	cs\Route,
15
	cs\User;
16
trait profiles {
17
	static function profiles_get () {
18
		$User = User::instance();
19
		if ($User->guest()) {
20
			throw new ExitException(403);
21
		}
22
		$fields = [
23
			'id',
24
			'login',
25
			'username',
26
			'language',
27
			'timezone',
28
			'avatar'
29
		];
30
		$Page   = Page::instance();
31
		$Route  = Route::instance();
32
		if (isset($Route->route[1])) {
33
			$id     = _int(explode(',', $Route->route[1]));
34
			$single = count($id) == 1;
35
			if (
36
				!$User->admin() &&
37
				!(
38
				$id = array_intersect($id, $User->get_contacts())
39
				)
40
			) {
41
				throw new ExitException('User is not in your contacts', 403);
42
			}
43
			if ($single) {
44
				$Page->json($User->get($fields, $id[0]));
45
			} else {
46
				$Page->json(
47
					array_map(
48
						function ($id) use ($fields, $User) {
49
							return $User->get($fields, $id);
50
						},
51
						$id
52
					)
53
				);
54
			}
55
		} else {
56
			throw new ExitException('Specified ids are expected', 400);
57
		}
58
	}
59
}
60