Completed
Push — count-subquery-clone ( d5815f...6cc328 )
by Haralan
03:48
created

Kohana_Upload_File::move_to_server()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 32
ccs 21
cts 21
cp 1
rs 8.439
cc 5
eloc 18
nc 6
nop 1
crap 5
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
/**
4
 * This class is what the upload field accually returns
5
 * and has all the nesessary info and manipulation abilities to save / delete / validate itself
6
 *
7
 * @package    Jam
8
 * @author     Ivan Kerin
9
 * @copyright  (c) 2011-2012 Despark Ltd.
10
 * @license    http://creativecommons.org/licenses/by-sa/3.0/legalcode
11
*/
12
class Kohana_Upload_File {
13
14
	protected $_source;
15
16
	protected $_server;
17
18
	protected $_path;
19
20
	protected $_temp;
21
22
	protected $_filename;
23
24
	protected $_transformations = array();
25
26
	protected $_thumbnails = array();
27
28
	protected $_extracted_from_source = FALSE;
29
30
31 29
	public function __construct($server, $path, $filename = NULL)
32
	{
33 29
		$this->server($server);
34 29
		$this->_path = $path;
35
36 29
		if ($filename !== NULL)
37 29
		{
38 1
			$this->_filename = $filename;
39 1
		}
40 29
	}
41
42
	/**
43
	 * Get / Set the path for the image on the server
44
	 * @param  string $path
45
	 * @return string|Upload_File
46
	 */
47 22
	public function path($path = NULL)
48
	{
49 22
		if ($path !== NULL)
50 22
		{
51 19
			$this->_path = $path;
52
53 19
			return $this;
54
		}
55
56 5
		return $this->_path;
57
	}
58
59 2
	public function move_to_server($new_server)
60
	{
61 1
		$file = Upload_Util::combine($this->temp()->directory_path(), $this->filename());
62 2
		$old_file = $this->full_path();
63
64 1
		if ( ! $this->server()->is_file($old_file))
65 1
			throw new Kohana_Exception('File '.$old_file.' does not exist');
66
67 1
		$this->server()->download_move($old_file, $file);
68
69 1
		foreach ($this->thumbnails() as $thumbnail => $thumbnail_params)
0 ignored issues
show
Bug introduced by
The expression $this->thumbnails() of type this<Kohana_Upload_File>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
70
		{
71 1
			$thumbnail_file = Upload_Util::combine($this->temp()->directory_path($thumbnail), $this->filename());
72 1
			$old_thumbnail_file = $this->full_path($thumbnail);
73
74 1
			if ( ! $this->server()->is_file($old_thumbnail_file))
75 1
				throw new Kohana_Exception('File '.$old_thumbnail_file.' does not exist');
76
77 1
			$this->server()->download_move($old_thumbnail_file, $thumbnail_file);
78 1
		}
79
80 1
		$this->server($new_server);
81 1
		$this->server()->upload_move($this->full_path(), $file);
82
83 1
		foreach ($this->thumbnails() as $thumbnail => $thumbnail_params)
0 ignored issues
show
Bug introduced by
The expression $this->thumbnails() of type this<Kohana_Upload_File>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
84
		{
85 1
			$thumbnail_file = Upload_Util::combine($this->temp()->directory_path($thumbnail), $this->filename());
86 1
			$this->server()->upload_move($this->full_path($thumbnail), $thumbnail_file);
87 1
		}
88
89 1
		$this->temp()->clear();
90 1
	}
91
92
	/**
93
	 * Get / Set the source. Automatically set the source_type
94
	 *
95
	 * @param  mixed $source
96
	 * @return mixed
97
	 */
98 24
	public function source($source = NULL)
99
	{
100 24
		if ($source !== NULL)
101 24
		{
102 22
			$this->_source = Upload_Source::factory($source);
103
104 22
			if ($this->_source->type() === Upload_Source::TYPE_TEMP)
105 22
			{
106 5
				$this->temp()->directory(dirname($source));
107 5
				$this->filename(basename($source));
108 5
			}
109
110 22
			return $this;
111
		}
112
113 20
		return $this->_source;
114
	}
115
116
	/**
117
	 * Get / Set transformations
118
	 * @param  array $transformations
119
	 * @return array|Upload_File
120
	 */
121 1
	public function transformations(array $transformations = NULL)
122
	{
123 1
		if ($transformations !== NULL)
124 1
		{
125 1
			$this->_transformations = $transformations;
126
127 1
			return $this;
128
		}
129
130 1
		return $this->_transformations;
131
	}
132
133
134
	/**
135
	 * Get / Set thumbnails
136
	 * @param  array $thumbnails
137
	 * @return array|Upload_File
138
	 */
139 5
	public function thumbnails(array $thumbnails = NULL)
140
	{
141 5
		if ($thumbnails !== NULL)
142 5
		{
143 3
			$this->_thumbnails = $thumbnails;
144
145 3
			return $this;
146
		}
147
148 5
		return $this->_thumbnails;
149
	}
150
151
	/**
152
	 * Get the Upload_Temp object. Create it if it's not already created
153
	 * @return Upload_Temp
154
	 */
155 24
	public function temp()
156
	{
157 24
		if ( ! $this->_temp)
158 24
		{
159 24
			$this->_temp = Upload_Temp::factory();
160 24
		}
161
162 24
		return $this->_temp;
163
	}
164
165
	/**
166
	 * Get the upload server
167
	 * @return Upload_Server
168
	 */
169 29
	public function server($server = NULL)
170
	{
171 29
		if ($server !== NULL)
172 29
		{
173 29
			$this->_server = $server;
174 29
			return $this;
175
		}
176
177 5
		return Upload_Server::instance($this->_server);
178
	}
179
180
	/**
181
	 * Get / Set the current filename
182
	 * @param  string $filename
183
	 * @return string|Upload_File
184
	 */
185 28
	public function filename($filename = NULL)
186
	{
187 28
		if ($filename !== NULL)
188 28
		{
189 27
			if (is_array($filename))
190 27
			{
191 1
				if (isset($filename['name']))
192 1
				{
193 1
					$this->_filename = $filename['name'];
194 1
				}
195 1
			}
196
			else
197
			{
198 27
				$this->_filename = $filename;
199
			}
200
201 27
			return $this;
202
		}
203
204 25
		return $this->_filename;
205
	}
206
207 2
	public function transform()
208
	{
209 2
		if (($this->_transformations OR $this->_thumbnails) AND @ getimagesize($this->file()))
210 2
		{
211 1
			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...
212 1
			{
213 1
				Upload_Util::transform_image($this->file(), $this->file(), $this->transformations());
0 ignored issues
show
Bug introduced by
It seems like $this->transformations() targeting Kohana_Upload_File::transformations() can also be of type this<Kohana_Upload_File>; however, Kohana_Upload_Util::transform_image() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
214 1
			}
215
216 1
			$this->generate_thumbnails();
217 1
		}
218 2
	}
219
220
	/**
221
	 * Save the current source to the temp folder
222
	 */
223 15
	public function save_to_temp()
224
	{
225 14
		if ( ! $this->source())
226 15
			throw new Kohana_Exception("Cannot move file to temp directory, source does not exist, path :path, filename :filename", array(':path' => $this->path(), ':filename' => $this->filename()));
227
228 14
		if ( ! $this->source()->copied())
229 14
		{
230 14
			$this->source()->copy_to($this->temp()->directory_path());
231 14
			$this->filename($this->source()->filename());
232 14
		}
233
234 14
		return $this;
235
	}
236
237
	/**
238
	 * Generate the thumbnails if they are not generated
239
	 *
240
	 * @return Upload_File $this
241
	 */
242 3
	public function generate_thumbnails()
243
	{
244 3
		foreach ($this->thumbnails() as $thumbnail => $thumbnail_params)
0 ignored issues
show
Bug introduced by
The expression $this->thumbnails() of type this<Kohana_Upload_File>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
245
		{
246 2
			if ( ! is_file($this->file($thumbnail)))
247 2
			{
248 2
				Upload_Util::transform_image($this->file(), $this->file($thumbnail), $thumbnail_params['transformations']);
249 2
			}
250 3
		}
251
252 3
		return $this;
253
	}
254
255
	/**
256
	 * Save the file by moving it from temporary to the upload server
257
	 * Generate the thumbnails if nesessary
258
	 * @return Upload_File $this
259
	 */
260 2
	public function save()
261
	{
262 2
		if ($this->_thumbnails AND @ getimagesize($this->file()))
263 2
		{
264 1
			$this->generate_thumbnails();
265 1
		}
266
267 2
		$this->server()->upload_move($this->full_path(), $this->file());
268
269 2
		foreach ($this->thumbnails() as $thumbnail => $thumbnail_params)
0 ignored issues
show
Bug introduced by
The expression $this->thumbnails() of type this<Kohana_Upload_File>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
270
		{
271 1
			$this->server()->upload_move($this->full_path($thumbnail), $this->file($thumbnail));
272 2
		}
273
274 2
		$this->server()->unlink(dirname($this->file()));
275
276 2
		$this->_source = NULL;
277
278 2
		return $this;
279
	}
280
281
	/**
282
	 * Clear temporary files
283
	 * @return Upload_File $this
284
	 */
285 18
	public function clear()
286
	{
287 18
		$this->temp()->clear();
288
289 18
		return $this;
290
	}
291
292
	/**
293
	 * Delete the current file on the server and clear temporary files
294
	 * @return Upload_File $this
295
	 */
296 2
	public function delete()
297
	{
298 2
		$this->server()->unlink($this->full_path());
299
300 2
		foreach ($this->thumbnails() as $thumbnail => $transformations)
0 ignored issues
show
Bug introduced by
The expression $this->thumbnails() of type this<Kohana_Upload_File>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
301
		{
302 1
			$this->server()->unlink($this->full_path($thumbnail));
303 2
		}
304
305 2
		$this->clear();
306
307 2
		return $this;
308
	}
309
310
	/**
311
	 * Get the current filename (temp or server)
312
	 * @param  string $thumbnail
313
	 * @return string
314
	 */
315 20
	public function file($thumbnail = NULL)
316
	{
317 20
		return $this->location('realpath', $thumbnail);
318
	}
319
320
	/**
321
	 * Get the current url (temp or server)
322
	 * @param  string $thumbnail
323
	 * @param  mixed $protocol
0 ignored issues
show
Bug introduced by
There is no parameter named $protocol. 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...
324
	 * @return string
325
	 */
326 1
	public function url($thumbnail = NULL)
327
	{
328 1
		return $this->location('url', $thumbnail);
329
	}
330
331
	/**
332
	 * Get the full path with the filename
333
	 * @param  string $thumbnail
334
	 * @return string
335
	 */
336 4
	public function full_path($thumbnail = NULL)
337
	{
338 4
		return Upload_Util::combine($this->path(), $thumbnail, $this->filename());
339
	}
340
341 1
	public function temp_source()
342
	{
343 1
		if ( ! $this->_source OR ! $this->filename())
344 1
			return NULL;
345
346 1
		return $this->temp()->directory().'/'.$this->filename();
347
	}
348
349 20
	protected function location($method, $thumbnail = NULL)
350
	{
351 20
		if ( ! $this->filename())
352 20
			return NULL;
353
354
		try
355
		{
356 20
			if ($this->_source)
357 20
			{
358 18
				return $this->temp()->$method(Upload_Util::combine($this->temp()->directory(), $thumbnail, $this->filename()));
359
			}
360
			else
361
			{
362 4
				return $this->server()->$method($this->full_path($thumbnail));
363
			}
364
		}
365
		catch (Flex\Storage\Exception_Notsupported $exception)
366
		{
367
			return NULL;
368
		}
369
	}
370
371
	/**
372
	 * Check if its empty (no filename or source)
373
	 * @return boolean
374
	 */
375 14
	public function is_empty()
376
	{
377 14
		return ! $this->filename();
378
	}
379
380 2
	public function __toString()
381
	{
382 2
		return (string) $this->filename();
383
	}
384
}
385