File::setDir()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Class for file
4
 *
5
 * @file      File.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      2013-07-27 16:15
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Tools;
17
18
/**
19
 * Class File
20
 * @author  Yancharuk Alexander <alex at itvault dot info>
21
 */
22
class File
23
{
24
	protected $name;
25
	protected $path;
26
	protected $dir;
27
	protected $mime;
28
29
	/**
30
	 * Get absolute directory path
31
	 *
32
	 * @return string
33
	 */
34 1
	public function getDir()
35
	{
36 1
		return $this->dir;
37
	}
38
39
	/**
40
	 * Set absolute directory path
41
	 *
42
	 * @param string $dir Absolute directory path
43
	 *
44
	 * @return $this
45
	 */
46 1
	public function setDir($dir)
47
	{
48 1
		$this->dir = $dir;
49
50 1
		return $this;
51
	}
52
53
	/**
54
	 * Get MIME type
55
	 *
56
	 * @return string
57
	 */
58 1
	public function getMime()
59
	{
60 1
		return $this->mime;
61
	}
62
63
	/**
64
	 * Set MIME type
65
	 *
66
	 * @param string $mime
67
	 * @return $this
68
	 */
69 1
	public function setMime($mime)
70
	{
71 1
		$this->mime = $mime;
72
73 1
		return $this;
74
	}
75
76
	/**
77
	 * Get file name
78
	 *
79
	 * @return string
80
	 */
81 1
	public function getName()
82
	{
83 1
		return $this->name;
84
	}
85
86
	/**
87
	 * Set file name
88
	 *
89
	 * @param string $name File name
90
	 *
91
	 * @return $this
92
	 */
93 1
	public function setName($name)
94
	{
95 1
		$this->name = $name;
96
97 1
		return $this;
98
	}
99
100
	/**
101
	 * Get absolute file path
102
	 *
103
	 * @return string
104
	 */
105 1
	public function getPath()
106
	{
107 1
		return $this->path;
108
	}
109
110
	/**
111
	 * Set absolute file path
112
	 *
113
	 * @param string $path Absolute path to file
114
	 *
115
	 * @return $this
116
	 */
117 1
	public function setPath($path)
118
	{
119 1
		$this->path = $path;
120
121 1
		$name = basename($path);
122
123 1
		$this->setName($name);
124
125 1
		$dir = rtrim(strstr($path, $name, true), DIRECTORY_SEPARATOR);
126
127 1
		$this->setDir($dir);
128
129 1
		return $this;
130
	}
131
132
	/**
133
	 * Delete file
134
	 *
135
	 * @return bool
136
	 */
137 3
	public function delete()
138
	{
139 3
		if (!file_exists($this->getPath()) || !is_writable($this->getPath())) {
140 2
			return false;
141
		}
142
143 1
		return unlink($this->getPath());
144
	}
145
146
	/**
147
	 * Delete file dir if empty
148
	 *
149
	 * @return bool
150
	 */
151 3
	public function deleteDir()
152
	{
153 3
		$files = glob($this->getDir() . '/*');
154
155 3
		foreach ($files as $file) {
156 2
			if ($file !== $this->getPath()) {
157 1
				return false;
158
			}
159
		}
160
161 3
		if (!$this->delete()) {
162 1
			return false;
163
		}
164
165 2
		return rmdir($this->getDir());
166
	}
167
}
168