Kohana_Auth_Service   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 55.41%

Importance

Changes 0
Metric Value
wmc 32
lcom 2
cbo 7
dl 0
loc 185
ccs 41
cts 74
cp 0.5541
rs 9.84
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
initialize() 0 1 ?
logged_in() 0 1 ?
service_login_complete() 0 1 ?
login_url() 0 1 ?
logout_service() 0 1 ?
service_user_info() 0 1 ?
service_uid() 0 1 ?
A api() 0 8 3
A __construct() 0 5 1
A type() 0 4 1
A auto_login_enabled() 0 4 1
A enabled() 0 9 2
A build_user() 0 16 4
C get_user() 0 56 11
A logout() 0 7 2
A login() 0 17 3
A complete_login() 0 16 4
1
<?php defined('SYSPATH') OR die('No direct access allowed.');
2
/**
3
 * Jam Auth driver.
4
 *
5
 * @package    Kohana/Auth
6
 * @author     Ivan Kerin
7
 * @copyright  (c) 2011-2012 Despark Ltd.
8
 * @license    http://creativecommons.org/licenses/by-sa/3.0/legalcode
9
 */
10
abstract class Kohana_Auth_Service {
11
12
	protected $_service_field;
13
14
	protected $_type;
15
16
	protected $_api;
17
18
	protected $_enabled;
19
20
	protected $_config = array();
21
22
	protected $_login_role;
23
24
	protected $_user_model = 'user';
25
26
	protected $_role_model = 'role';
27
28 3
	public function api($api = NULL)
29
	{
30 3
		if ( ! $this->_api)
31
		{
32 3
			$this->_api = $api ?: $this->initialize();
33
		}
34 3
		return $this->_api;
35
	}
36
37 37
	public function __construct($config = NULL)
38
	{
39 37
		$this->_config = (array) $config;
40 37
		$this->_enabled = Arr::get($this->_config, 'enabled', FALSE);
41 37
	}
42
43 1
	public function type()
44
	{
45 1
		return $this->_type;
46
	}
47
48 12
	public function auto_login_enabled()
49
	{
50 12
		return Arr::get($this->_config, 'auto_login', FALSE);
51
	}
52
53 11
	public function enabled($enabled = NULL)
54
	{
55 11
		if ($enabled !== NULL)
56
		{
57
			$this->_enabled = $enabled;
58
			return $this;
59
		}
60 11
		return $this->_enabled;
61
	}
62
63 1
	public function build_user($data, $create = TRUE)
64
	{
65 1
		if ($this->logged_in() AND ! empty($data))
66
		{
67 1
			$user = Jam::build($this->_user_model);
68
69 1
			if ($user->load_service_values($this, $data, $create) === FALSE)
70
				return FALSE;
71
72 1
			$user->roles->add(Jam::find($this->_role_model, 'login'));
73
74 1
			$user->set($this->_service_field, $this->service_uid());
75
76 1
			return $user;
77
		}
78
	}
79
80 10
	public function get_user()
81
	{
82 10
		if ($this->enabled() AND $this->logged_in())
83
		{
84 1
			$user = Jam::find_or_build($this->_user_model, array($this->_service_field => $this->service_uid()));
85 1
			$user->_is_new = TRUE;
86 1
			$data = $this->service_user_info();
87
88 1
			if ( ! $user->loaded())
89
			{
90 1
				if (isset($data['email']))
91
				{
92 1
					$user = Jam::find_or_build($this->_user_model, array('email' => $data['email']));
93
94 1
					if ($user->loaded())
95
					{
96 1
						$user->_is_new = FALSE;
97
98 1
                        if (Arr::get($this->_config, 'update_user_on_link'))
99
                        {
100
                            $user->load_service_values($this, $data, FALSE);
101
                        }
102
					}
103
				}
104
105 1
				if ( ! $user->loaded() AND Arr::get($this->_config, 'create_user'))
106
				{
107
					$user = $this->build_user($data, TRUE);
108
					$user->_is_new = TRUE;
109
				}
110
111 1
				if ( ! $user)
112
				{
113
					throw new Auth_Exception_Service('Service :service user with service uid :id does not exist and failed to create', array(
114
						':service' => $this->type(),
115
						':id' => $this->service_uid()
116
					));
117
				}
118
119 1
				$user->set($this->_service_field, $this->service_uid());
120 1
				$user->save();
121
			}
122 1
			elseif (Arr::get($this->_config, 'update_user'))
123
			{
124
				$user->_is_new = FALSE;
125
				$user->load_service_values($this, $data, FALSE);
126
				$user->save();
127
			}
128
			else
129
			{
130 1
				$user->_is_new = FALSE;
131
			}
132 1
			return $user;
133
		}
134 9
		return FALSE;
135
	}
136
137
	public function logout()
138
	{
139
		if ( ! $this->enabled())
140
			return FALSE;
141
142
		return $this->logout_service(Request::initial(), URL::site(Request::current()->uri(), TRUE));
0 ignored issues
show
Bug introduced by
It seems like \Request::current()->uri() targeting Kohana_Request::uri() can also be of type object<Request>; however, Kohana_URL::site() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
143
	}
144
145
	public function login()
146
	{
147
		if ( ! $this->enabled())
148
			return FALSE;
149
150
		if (($user = $this->get_user()))
151
		{
152
			return $user;
153
		}
154
		else
155
		{
156
			$login_url = $this->login_url(URL::site(Arr::get($this->_config, 'back_url', Request::current()->uri()), TRUE));
157
158
			HTTP::redirect($login_url);
159
			return FALSE;
160
		}
161
	}
162
163
	public function complete_login()
164
	{
165
		if ( ! $this->enabled())
166
			return FALSE;
167
168
		if ( ! $this->logged_in()) {
169
			$this->service_login_complete();
170
		}
171
172
		if (($user = $this->get_user()))
173
		{
174
			return $user;
175
		}
176
177
		return FALSE;
178
	}
179
180
	abstract public function initialize();
181
182
	abstract public function logged_in();
183
184
	abstract public function service_login_complete();
185
186
	abstract public function login_url($back_url);
187
188
	abstract public function logout_service($request, $back_url);
189
190
	abstract public function service_user_info();
191
192
	abstract public function service_uid();
193
194
}
195