Kohana_Upload_Temp::directory()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 *  Utility class for managing the temporary upload directory
4
 *
5
 * @package    Jam
6
 * @author     Ivan Kerin
7
 * @copyright  (c) 2011-2012 Despark Ltd.
8
 * @license    http://creativecommons.org/licenses/by-sa/3.0/legalcode
9
 */
10
class Kohana_Upload_Temp
11
{
12 22
	public static function factory()
13
	{
14 22
		return new Upload_Temp;
15
	}
16
17 18
	public static function config($name = NULL)
18
	{
19 18
		if ($name !== NULL)
20
		{
21 18
			return Kohana::$config->load("jam.upload.temp.{$name}");
22
		}
23
		else
24
		{
25
			return Kohana::$config->load("jam.upload.temp");
26
		}
27
	}
28
29
	public static function valid($file)
30
	{
31
		return (substr_count($file, DIRECTORY_SEPARATOR) === 1 AND is_file(Upload_Util::combine(Upload_Temp::config('path'), $file)));
32
	}
33
34
	protected $_directory;
35
36 21
	public function directory($directory = NULL)
37
	{
38 21
		if ($directory !== NULL)
39
		{
40 5
			$this->_directory = $directory;
41 5
			return $this;
42
		}
43
44 21
		if ( ! $this->_directory)
45
		{
46 16
			$this->_directory = uniqid();
47
		}
48
49 21
		return $this->_directory;
50
	}
51
52 14
	public function directory_path($thumbnail = NULL)
53
	{
54 14
		$directory = $this->realpath($this->directory(), $thumbnail);
55
56 14
		if ( ! is_dir($directory))
57
		{
58 14
			mkdir($directory, 0777);
59
		}
60
61 14
		return $directory;
62
	}
63
64
	public function directory_url($thumbnail = NULL)
65
	{
66
		return $this->webpath($this->directory(), $thumbnail);
0 ignored issues
show
Bug introduced by
The method webpath() does not seem to exist on object<Kohana_Upload_Temp>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
	}
68
69 17
	public function clear()
70
	{
71 17
		if ($this->_directory)
72
		{
73 16
			Upload_Util::rmdir($this->realpath($this->directory()));
74 16
			$this->_directory = NULL;
75
		}
76 17
	}
77
78 18
	public function realpath($path, $thumbnail = NULL)
79
	{
80 18
		return Upload_Util::combine(Upload_Temp::config('path'), $path, $thumbnail);
81
	}
82
83
	public function url($path, $thumbnail = NULL)
84
	{
85
		return Upload_Util::combine(Upload_Temp::config('web'), $path, $thumbnail);
86
	}
87
88
}
89