Completed
Pull Request — master (#78)
by Philip
08:54 queued 07:17
created

Kohana_Jam_Field_Range::_default()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 39
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 12.1974

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 24
cts 27
cp 0.8889
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 19
nc 40
nop 2
crap 12.1974

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
 * Handles serialized data
4
 *
5
 * When set, the field attempts to unserialize the data into it's
6
 * actual PHP representation. When the model is saved, the value
7
 * is serialized back and saved as a string into the column.
8
 *
9
 * @package    Jam
10
 * @category   Fields
11
 * @author     Jonathan Geiger
12
 * @copyright  (c) 2010-2011 Jonathan Geiger
13
 * @license    http://www.opensource.org/licenses/isc-license.txt
14
 */
15
abstract class Kohana_Jam_Field_Range extends Jam_Field {
16
17
	public $format;
18
19 2
	public function get(Jam_Validated $model, $value, $is_loaded)
20
	{
21 2
		if ( ! $value)
22 2
			return NULL;
23
24 2
		return ($value instanceof Jam_Range) ? $value : new Jam_Range($value, $this->format);
25
	}
26
27 2
	public function set(Jam_Validated $model, $value, $is_changed)
28
	{
29 2
		list($value, $return) = $this->_default($model, $value);
30
31 2
		if ( ! $return AND ! ($value instanceof Jam_Range))
32 2
		{
33 1
			$value = new Jam_Range($value, $this->format);
34 1
		}
35
36 2
		return $value;
37
	}
38
39
	public function convert(Jam_Validated $model, $value, $is_loaded)
40
	{
41
		if ($value === NULL)
42
			return NULL;
43
44
		return $value->__toString();
45
	}
46
47 2
	protected function _default(Jam_Validated $model, $value)
48
	{
49 2
		$return = FALSE;
50
51 2
		$value = $this->run_filters($model, $value);
52
53 2
		$min = NULL;
54 2
		$max = NULL;
55
56 2
		if (is_array($value))
57 2
		{
58 1
			$min = $value[0];
59 1
			$max = $value[1];
60 1
		}
61
62 2
		if (is_string($value) AND strstr($value, '|'))
63 2
		{
64 1
			$values = explode('|', $value);
65
66 1
			$min = isset($values[0]) ? $values[0] : NULL;
67 1
			$max = isset($values[1]) ? $values[1] : NULL;
68 1
		}
69
70
		// Convert empty values to NULL, if needed
71 2
		if ($this->convert_empty AND (empty($min) OR empty($max)))
72 2
		{
73
			$value  = $this->empty_value;
74
			$return = TRUE;
75
		}
76
77
		// Allow NULL values to pass through untouched by the field
78 2
		if ($this->allow_null AND ($min === NULL OR $max === NULL))
79 2
		{
80 1
			$value  = NULL;
81 1
			$return = TRUE;
82 1
		}
83
84 2
		return array($value, $return);
85
	}
86
}
87