Kohana_Jam_Validator_Rule_Uploaded::validate()   F
last analyzed

Complexity

Conditions 39
Paths 3565

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 39.0423

Importance

Changes 0
Metric Value
dl 0
loc 68
ccs 32
cts 33
cp 0.9697
rs 0
c 0
b 0
f 0
cc 39
nc 3565
nop 3
crap 39.0423

How to fix   Long Method    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_Uploaded extends Jam_Validator_Rule {
12
13
	/**
14
	 * array of allowed extensions
15
	 * @var array
16
	 */
17
	public $only = array();
18
19
	public $minimum_width;
20
	public $minimum_height;
21
	public $minimum_size;
22
23
	public $maximum_width;
24
	public $maximum_height;
25
	public $maximum_size;
26
27
	public $exact_width;
28
	public $exact_height;
29
	public $exact_size;
30
31 4
	public function valid_extensions()
32
	{
33 4
		$extensions = $this->only;
34
35 4
		if ($extensions == 'image')
36
		{
37 3
			$extensions = array('jpg', 'jpeg', 'png', 'gif');
38
		}
39
40 4
		return array_map('strtolower', $extensions);
41
	}
42
43 14
	public function validate(Jam_Validated $model, $attribute, $value)
44
	{
45 14
		if ($value AND ! $value->is_empty() AND $value->source())
46
		{
47 13
			if (($error = $value->source()->error()))
48
			{
49
				$model->errors()->add($attribute, 'uploaded_native', array(':message' => $error));
50
			}
51 13
			elseif ( ! is_file($value->file()))
52
			{
53 1
				$model->errors()->add($attribute, 'uploaded_is_file');
54
			}
55 13
			if ($this->only AND ! in_array(strtolower(pathinfo($value->filename(), PATHINFO_EXTENSION)), $this->valid_extensions()))
56
			{
57 2
				$model->errors()->add($attribute, 'uploaded_extension', array(':extension' => join(', ', $this->valid_extensions())));
58
			}
59 13
			if ($this->minimum_size OR $this->maximum_size OR $this->exact_size)
60
			{
61 4
				$size = @ filesize($value->file());
62
63 4
				if ($this->minimum_size AND $minimum_size = Num::bytes($this->minimum_size) AND (int) $size < (int) $minimum_size)
64
				{
65 1
					$model->errors()->add($attribute, 'uploaded_minimum_size', array(':minimum_size' => $this->minimum_size));
66
				}
67 4
				if ($this->maximum_size AND $maximum_size = Num::bytes($this->maximum_size) AND (int) $size > (int) $maximum_size)
68
				{
69 2
					$model->errors()->add($attribute, 'uploaded_maximum_size', array(':maximum_size' => $this->maximum_size));
70
				}
71 4
				if ($this->exact_size AND $exact_size = Num::bytes($this->exact_size) AND (int) $size !== (int) $exact_size)
72
				{
73 1
					$model->errors()->add($attribute, 'uploaded_exact_size', array(':exact_size' => $this->exact_size));
74
				}
75
			}
76 13
			if ($this->minimum_width OR $this->minimum_height OR $this->maximum_width OR $this->maximum_height OR $this->exact_width OR $this->exact_height)
77
			{
78 7
				$dims = @ getimagesize($value->file());
79 7
				if ($dims)
80
				{
81 7
					list($width, $height) = $dims;
82
83 7
					if ($this->exact_width AND (int) $width !== (int) $this->exact_width)
84
					{
85 1
						$model->errors()->add($attribute, 'uploaded_exact_width', array(':exact_width' => $this->exact_width));
86
					}
87 7
					if ($this->exact_height AND (int) $height !== (int) $this->exact_height)
88
					{
89 2
						$model->errors()->add($attribute, 'uploaded_exact_height', array(':exact_height' => $this->exact_height));
90
					}
91 7
					if ($this->minimum_width AND (int) $width < (int) $this->minimum_width)
92
					{
93 1
						$model->errors()->add($attribute, 'uploaded_minimum_width', array(':minimum_width' => $this->minimum_width));
94
					}
95 7
					if ($this->minimum_height AND (int) $height < (int) $this->minimum_height)
96
					{
97 1
						$model->errors()->add($attribute, 'uploaded_minimum_height', array(':minimum_height' => $this->minimum_height));
98
					}
99 7
					if ($this->maximum_width AND (int) $width > (int) $this->maximum_width)
100
					{
101 1
						$model->errors()->add($attribute, 'uploaded_maximum_width', array(':maximum_width' => $this->maximum_width));
102
					}
103 7
					if ($this->maximum_height AND (int) $height > (int) $this->maximum_height)
104
					{
105 1
						$model->errors()->add($attribute, 'uploaded_maximum_height', array(':maximum_height' => $this->maximum_height));
106
					}
107
				}
108
			}
109
		}
110 14
	}
111
}
112