Kohana_Jam_Behavior_Tokenable::new_token()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
/**
4
 * @package    Jam
5
 * @category   Behavior
6
 * @author     Ivan Kerin
7
 * @copyright  (c) 2011-2012 Despark Ltd.
8
 * @license    http://www.opensource.org/licenses/isc-license.txt
9
 */
10
class Kohana_Jam_Behavior_Tokenable extends Jam_Behavior {
11
12
	protected $_field = 'token';
13
14
	protected $_field_options = array();
15
16
	protected $_uppercase = FALSE;
17
18
	protected $_token_function = 'Jam_Behavior_Tokenable::generate_token';
19
20
	public function initialize(Jam_Meta $meta, $name)
21
	{
22
		parent::initialize($meta, $name);
23
24
		$meta->field($this->_field, Jam::field('string', $this->_field_options));
25
	}
26
27 10
	public function model_before_create(Jam_Model $model)
28
	{
29 10
		if ( ! $model->{$this->_field})
30
		{
31 10
			$model->update_token();
0 ignored issues
show
Documentation Bug introduced by
The method update_token does not exist on object<Jam_Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
32
		}
33 10
	}
34
35 11
	public static function generate_token()
36
	{
37 11
		return base_convert(rand(1, 1000000000), 10, 36);
38
	}
39
40 11
	public function new_token()
41
	{
42 11
		$token = call_user_func($this->_token_function);
43
44 11
		if ($this->_uppercase)
45
		{
46 11
			$token = strtoupper($token);
47
		}
48
49 11
		return $token;
50
	}
51
52 10
	public function model_call_update_token(Jam_Model $model, Jam_Event_Data $data)
53
	{
54
		do
55
		{
56 10
			$model->{$this->_field} = $this->new_token();
57
		}
58 10
		while (Jam::all($model->meta()->model())->where($this->_field, '=', $model->{$this->_field})->count_all() > 0);
59
60 10
		$data->return = $model;
61 10
	}
62
63
	/**
64
	 * Generate a where_token method for Jam_Query_Builder_Select
65
	 * @param  Jam_Query_Builder_Select $builder the builder object
66
	 * @param  Jam_Event_Data $data
67
	 * @param  string $token the token to search for
68
	 * @return void
69
	 */
70
	public function builder_call_where_token(Jam_Query_Builder_Select $builder, Jam_Event_Data $data, $token)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
	{
72
		$builder->where($this->_model.'.'.$this->_field, '=', $token);
73
	}
74
75
	/**
76
	 * Generate a find_by_token method for Jam_Query_Builder_Select
77
	 * @param  Jam_Query_Builder_Select $builder the builder object
78
	 * @param  Jam_Event_Data $data
79
	 * @param  string $token the token to search for
80
	 */
81
	public function builder_call_find_by_token(Jam_Query_Builder_Select $builder, Jam_Event_Data $data, $token)
82
	{
83
		$this->builder_call_where_token($builder, $data, $token);
84
		$data->return = $builder->first();
0 ignored issues
show
Documentation Bug introduced by
The method first does not exist on object<Jam_Query_Builder_Select>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
85
		$data->stop = TRUE;
86
	}
87
88
	/**
89
	 * Generate a find_by_token_insist method for Jam_Query_Builder_Select
90
	 * @param  Jam_Query_Builder_Select $builder the builder object
91
	 * @param  Jam_Event_Data $data
92
	 * @param  string $token the token to search for
93
	 */
94
	public function builder_call_find_by_token_insist(Jam_Query_Builder_Select $builder, Jam_Event_Data $data, $token)
95
	{
96
		$this->builder_call_where_token($builder, $data, $token);
97
		$data->return = $builder->first_insist();
0 ignored issues
show
Documentation Bug introduced by
The method first_insist does not exist on object<Jam_Query_Builder_Select>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
98
		$data->stop = TRUE;
99
	}
100
}
101