Kohana_Model_Auth_User_Token   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 36.67%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 6
dl 0
loc 63
ccs 11
cts 30
cp 0.3667
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 28 1
A convert_expires() 0 9 3
A expired() 0 4 1
A generate_token() 0 4 1
A generate_unique_token() 0 11 2
1
<?php defined('SYSPATH') OR die('No direct access allowed.');
2
/**
3
 * Default auth user toke
4
 *
5
 * @package	   Kohana/Auth
6
 * @author     Ivan Kerin
7
 * @copyright  (c) 2011-2012 Despark Ltd.
8
 * @author	   creatoro
9
 * @copyright  (c) 2011 creatoro
10
 * @license	   http://creativecommons.org/licenses/by-sa/3.0/legalcode
11
 */
12
class Kohana_Model_Auth_User_Token extends Jam_Model {
13
14
	public static function initialize(Jam_Meta $meta)
15
	{
16
		$meta
17
			->name_key('token')
18
19
			->behaviors(array(
20
				'auth_user_token' => Jam::behavior('auth_user_token'),
21
			))
22
23
			->fields(array(
24
				'id' => Jam::field('primary'),
25
				'user_agent' => Jam::field('string'),
26
				'token' => Jam::field('string'),
27
				'type' => Jam::field('string'),
28
				'created' => Jam::field('timestamp', array(
29
					'auto_now_create' => TRUE,
30
				)),
31
				'expires' => Jam::field('timestamp', array(
32
					'filters' => array('Model_Auth_User_Token::convert_expires')
33
				)),
34
			))
35
36
			->associations(array(
37
				'user' => Jam::association('belongsto'),
38
			))
39
40
			->validator('token', array("unique" => TRUE));
41
	}
42
43 6
	public static function convert_expires($value)
44
	{
45 6
		if ( ! is_numeric($value) AND $time = strtotime($value))
46
		{
47 1
			return $time;
48
		}
49
50 5
		return $value;
51
	}
52
53
	public function expired()
54
	{
55
		return $this->expires < time();
0 ignored issues
show
Documentation introduced by
The property expires does not exist on object<Kohana_Model_Auth_User_Token>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
	}
57
58 2
	public static function generate_token()
59
	{
60 2
		return sha1(uniqid(Text::random('alnum', 32), TRUE));
61
	}
62
63 2
	public function generate_unique_token()
64
	{
65
		do 
66
		{
67 2
			$this->token = Model_Auth_User_Token::generate_token();
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Kohana_Model_Auth_User_Token>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68 2
			$collection = Jam::all($this->meta()->model())->where_key($this->token)->limit(1);
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Kohana_Model_Auth_User_Token>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Unused Code introduced by
$collection is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
		} 
70 2
		while (Jam::all($this)->where('token', '=', $this->token)->limit(1)->count_all() > 0);
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Kohana_Model_Auth_User_Token>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
72 2
		return $this;	
73
	}
74
}
75