Kohana_Jam_Field_Upload::convert()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 3
crap 12
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * @package    Despark/jam-upload
4
 * @author     Ivan Kerin
5
 * @copyright  (c) 2011-2012 Despark Ltd.
6
 * @license    http://creativecommons.org/licenses/by-sa/3.0/legalcode
7
 */
8
abstract class Kohana_Jam_Field_Upload extends Jam_Field {
9
10
	/**
11
	 * @var  boolean  whether or not to delete the old file when a new file is added
12
	 */
13
	public $delete_file = TRUE;
14
15
	/**
16
	 * @var boolean save the sizes of the image when saving the field
17
	 */
18
	public $save_size = FALSE;
19
20
	/**
21
	 * @var  string  the server used to store the file
22
	 */
23
	public $server = 'default';
24
25
	/**
26
	 * @var string the path to the file
27
	 */
28
	public $path = ":model/:id";
29
30
	/**
31
	 * @var  array  an array of transformation to apply to the image
32
	 */
33
	public $transformations = array();
34
35
	/**
36
	 * @var bool|string the field that has the data which server we are using at the moment
37
	 */
38
	public $dynamic_server = NULL;
39
40
	/**
41
	 * @var  array  specifications for all of the thumbnails that should be automatically generated when a new image is uploaded
42
	 */
43
	public $thumbnails = array();
44
45 16
	public function get(Jam_Validated $model, $upload_file, $is_changed)
46
	{
47 16
		if ( ! ($upload_file instanceof Upload_File))
48
		{
49 2
			$upload_file = $this->upload_file($model);
0 ignored issues
show
Compatibility introduced by
$model of type object<Jam_Validated> is not a sub-type of object<Jam_Model>. It seems like you assume a child class of the class Jam_Validated to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
50
		}
51
52
		return $upload_file
0 ignored issues
show
Bug introduced by
The method path does only exist in Upload_File, but not in Database.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53 16
			->server($this->dynamic_server ? $model->{$this->dynamic_server} : $this->server)
54 16
			->path($this->path($model));
0 ignored issues
show
Compatibility introduced by
$model of type object<Jam_Validated> is not a sub-type of object<Jam_Model>. It seems like you assume a child class of the class Jam_Validated to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
55
	}
56
57 17
	public function set(Jam_Validated $model, $value, $is_changed)
58
	{
59 17
		if ( ! $is_changed)
60 17
			return $this->upload_file($model)->filename($value);
0 ignored issues
show
Compatibility introduced by
$model of type object<Jam_Validated> is not a sub-type of object<Jam_Model>. It seems like you assume a child class of the class Jam_Validated to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
61
62 2
		if ($value instanceof Upload_File)
63
		{
64 1
			$upload_file = $value;
65
		}
66
		else
67
		{
68 1
			$upload_file = $model->{$this->name};
69
70 1
			if ($value === NULL)
71
			{
72
				$upload_file->filename('');
73
			}
74 1
			elseif ($value)
75
			{
76 1
				if (Upload_Source::valid($value))
77
				{
78 1
					$upload_file->source($value);
79
				}
80
81 1
				$upload_file->filename($value);
82
			}
83
84
		}
85
86 2
		return $upload_file->path($this->path($model));
0 ignored issues
show
Compatibility introduced by
$model of type object<Jam_Validated> is not a sub-type of object<Jam_Model>. It seems like you assume a child class of the class Jam_Validated to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
87
	}
88
89
	/**
90
	 * Move the file to the temp directory even before it validates, so validaitons can be properly performed
91
	 * @param  Jam_Model $model
92
	 */
93 1
	public function model_before_check(Jam_Model $model)
94
	{
95 1
		if ($model->changed($this->name) AND $upload_file = $model->{$this->name} AND $upload_file->source())
96
		{
97
			try {
98 1
				$upload_file->save_to_temp();
99
			} catch (Kohana_Exception $e) {
100
				$model->errors()->add($this->name, 'uploaded_native', array(':message' => $e->getMessage()));
101
			}
102
		}
103 1
	}
104
105
	/**
106
	 * After the validation for this field passes without any errors, fire up transformations and thumbnail generation
107
	 * @param  Jam_Model $model
108
	 */
109
	public function model_after_check(Jam_Model $model)
110
	{
111
		if ($model->changed($this->name) AND ! $model->errors($this->name) AND $upload_file = $model->{$this->name} AND $upload_file->source())
112
		{
113
			$upload_file->transform();
114
		}
115
	}
116
117
	/**
118
	 * Cleanup temporary file directories when the files are successfully saved
119
	 * @param  Jam_Model $model
120
	 * @param  boolean $is_changed
0 ignored issues
show
Bug introduced by
There is no parameter named $is_changed. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
121
	 */
122
	public function model_after_save(Jam_Model $model)
123
	{
124
		if ($model->changed($this->name) AND $upload_file = $model->{$this->name} AND $upload_file->source())
125
		{
126
			$upload_file
127
				->server($this->dynamic_server ? $model->{$this->dynamic_server} : $this->server)
128
				->path($this->path($model));
129
130
			// Delete the old file if there was one
131
			if ($this->delete_file AND $original = $model->original($this->name))
132
			{
133
				$this->upload_file($model)->filename($original)->delete();
134
			}
135
136
			$upload_file->save();
137
		}
138
139
		if ($model->changed($this->name) AND $upload_file = $model->{$this->name})
140
		{
141
			$upload_file->clear();
142
		}
143
	}
144
145
	/**
146
	 * Get the filename to store in the database
147
	 * @param  Jam_Model $model
148
	 * @param  Upload_File $upload_file
149
	 * @param  boolean $is_loaded
150
	 */
151
	public function convert(Jam_Validated $model, $upload_file, $is_loaded)
152
	{
153
		return ($upload_file AND $upload_file !== $this->default) ? $upload_file->filename() : $upload_file;
154
	}
155
156
	/**
157
	 * Remove files on model deletion
158
	 *
159
	 * @param  Jam_Model $model
160
	 * @param  boolean $is_changed
0 ignored issues
show
Bug introduced by
There is no parameter named $is_changed. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
161
	 */
162
	public function model_after_delete(Jam_Validated $model, Jam_Event_Data $data, $delete_finishd)
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...
163
	{
164
		if ($this->delete_file AND $delete_finishd AND $upload_file = $model->{$this->name})
165
		{
166
			$upload_file->delete();
167
		}
168
	}
169
170
	/**
171
	 * Get upload file object for a given model
172
	 *
173
	 * @param  Jam_Model $model
174
	 * @return Upload_File
175
	 */
176 17
	public function upload_file(Jam_Model $model)
177
	{
178 17
		$upload_file = new Upload_File($this->server, $this->path($model));
179
180 17
		if ($this->transformations)
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->transformations of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
181
		{
182
			$upload_file->transformations($this->transformations);
183
		}
184
185 17
		if ($this->thumbnails)
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->thumbnails of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
186
		{
187
			$upload_file->thumbnails($this->thumbnails);
188
		}
189
190 17
		return $upload_file;
191
	}
192
193
	/**
194
	 * Get the localized path for a given model, so that there are less filename conflicts and files are easily located,
195
	 * for example the default path is model/id so that Model_Image(1) images will be stored as images/1/file.jpg
196
	 *
197
	 * @param  Jam_Model $model the model for the context
198
	 * @return string
199
	 */
200 17
	protected function path(Jam_Model $model)
201
	{
202 17
		$converted_params = array();
203 17
		preg_match_all('#\:[a-zA-z]*#', $this->path, $params);
204 17
		foreach ($params[0] as $param)
205
		{
206
			switch ($param) {
207
208 17
				case ':column':
209
					$converted_params[$param] = $this->column;
210
				break;
211
212 17
				case ':model':
213 17
					$converted_params[$param] = $this->model;
214 17
				break;
215
216 17
				case ':name':
217
					$converted_params[$param] = $this->name;
218
				break;
219
220 17
				case ':id':
221 17
					$converted_params[$param] = ($model->loaded()) ? $model->id() : 'new';
222 17
				break;
223
224
				case ':id_group':
225
					$converted_params[$param] = ($model->loaded()) ? ceil($model->id() / 10000) : 'new';
226
				break;
227
228
				default:
229 17
					$converted_params[$param] = $model->{str_replace(':','', $param)};
230
			}
231
		}
232 17
		return rtrim(strtr($this->path, $converted_params), '/').'/';
233
	}
234
235
} // End Jam_Core_Field_File
236