Completed
Push — add/sync-rest-2 ( ada61b...a7dbc1 )
by
unknown
09:34
created

SAL_Token::for_anonymous_user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
/**
4
 * So that we have a real class instead of just passing around an array
5
 */
6
class SAL_Token {
7
8
	public $blog_id;
9
	public $user_id;
10
	public $scope;
11
	public $client_id;
12
	public $external_user_id;
13
	public $external_user_code;
14
	public $auth_type;
15
16
	function __construct( $blog_id, $user_id, $scope, $client_id, $external_user_id, $external_user_code, $auth_type ) {
17
		$this->blog_id = $blog_id; // if blog_id is set and scope is not global, limit to that blog
18
		$this->user_id = $user_id;
19
		$this->client_id = $client_id;
20
		$this->scope = $scope; 
21
		$this->external_user_id = $external_user_id;
22
		$this->external_user_code = $external_user_code;
23
		$this->auth_type = $auth_type;
24
	}
25
26
	public function is_global() {
27
		return $this->scope === 'global';
28
	}
29
30
	static function for_anonymous_user() {
31
		return new SAL_Token( 
32
			null, 
33
			get_current_user_id(), 
34
			null, // there's only ever one scope in our current API implementation, auth or global
35
			null,
36
			null, 
37
			null, 
38
			null
39
		);
40
	}
41
42
	static function from_rest_token( $token ) {
43
		return new SAL_Token( 
44
			$token['blog_id'], 
45
			$token['user_id'], 
46
			$token['scope'][0], // there's only ever one scope in our current API implementation, auth or global
47
			$token['client_id'],
48
			$token['external_user_id'], 
49
			$token['external_user_code'], 
50
			$token['auth']
51
		);
52
	}
53
}
54