Kohana_Jam_Validator_Rule_Range::validate()   C
last analyzed

Complexity

Conditions 17
Paths 66

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 17.8433

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 18
cts 21
cp 0.8571
rs 5.2166
c 0
b 0
f 0
cc 17
nc 66
nop 3
crap 17.8433

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Jam Validatior Rule
4
 *
5
 * @package    Jam
6
 * @category   Validation
7
 * @author     Ivan Kerin
8
 * @copyright  (c) 2011-2012 Despark Ltd.
9
 * @license    http://www.opensource.org/licenses/isc-license.txt
10
 */
11
class Kohana_Jam_Validator_Rule_Range extends Jam_Validator_Rule {
12
13
	public $consecutive;
14
15
	public $minimum;
16
17
	public $minimum_or_equal_to;
18
19
	public $maximum;
20
21
	public $maximum_or_equal_to;
22
23
	public $between;
24
25 30
	public function validate(Jam_Validated $model, $attribute, $value)
26
	{
27 30
		if ( ! ($value instanceof Jam_Range))
28
			throw new Kohana_Exception('Range validation rule can only be applied to range');
29
30 30
		if ( ! is_numeric($value->min()) OR ! is_numeric($value->max()))
31
		{
32
			$model->errors()->add($attribute, 'range_numeric');
33
34
			return;
35
		}
36
37 30
		$min = min($value->min(), $value->max());
38 30
		$max = max($value->min(), $value->max());
39
40 30
		if ($this->minimum !== NULL AND ($min <= $this->minimum))
41
		{
42 4
			$model->errors()->add($attribute, 'range_minimum', array(':minimum' => $this->minimum));
43
		}
44
45 30
		if ($this->minimum_or_equal_to !== NULL AND ($min < $this->minimum_or_equal_to))
46
		{
47 2
			$model->errors()->add($attribute, 'range_minimum_or_equal_to', array(':minimum_or_equal_to' => $this->minimum_or_equal_to));
48
		}
49
50 30
		if ($this->maximum !== NULL AND ($max >= $this->maximum))
51
		{
52 4
			$model->errors()->add($attribute, 'range_maximum', array(':maximum' => $this->maximum));
53
		}
54
55 30
		if ($this->maximum_or_equal_to !== NULL AND ($max > $this->maximum_or_equal_to))
56
		{
57 2
			$model->errors()->add($attribute, 'range_maximum_or_equal_to', array(':maximum_or_equal_to' => $this->maximum_or_equal_to));
58
		}
59
60 30
		if ($this->between !== NULL AND ! ($min >= $this->between[0] AND $max <= $this->between[1]))
61
		{
62 3
			$model->errors()->add($attribute, 'range_between', array(':minimum' => $this->between[0], ':maximum' => $this->between[1]));
63
		}
64
65 30
		if ($this->consecutive !== NULL AND ($value->min() > $value->max()))
66
		{
67 1
			$model->errors()->add($attribute, 'range_consecutive');
68
		}
69 30
	}
70
}
71