Completed
Push — master ( 331ac1...00209f )
by Milan
02:04
created

DocumentRoot::setDestination()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace h4kuna\Upload;
4
5
class DocumentRoot
6
{
7
8
	/** @var array */
9
	private $destinationDirs;
10
11
	/** @var string */
12
	private $alias;
13
14
	/** @var string */
15
	private $default;
16
17
	public function __construct(array $destinationDirs)
18
	{
19
		$this->destinationDirs = $destinationDirs;
20
		reset($destinationDirs);
21
		$this->default = $this->alias = key($destinationDirs);
22
	}
23
24
	/**
25
	 * @param string $alias
26
	 * @return self
27
	 * @throws AliasDoesNotExistsException
28
	 */
29
	public function setDestination($alias)
30
	{
31
		if (!isset($this->destinationDirs[$alias])) {
32
			throw new AliasDoesNotExistsException($alias);
33
		}
34
		$this->alias = $alias;
35
		return $this;
36
	}
37
38
	/**
39
	 * @param string|IStoreFile $relativePath
40
	 * @return string
41
	 */
42
	public function createAbsolutePath($relativePath)
43
	{
44
		if ($relativePath instanceof IStoreFile) {
45
			$relativePath = $relativePath->getRelativePath();
46
		}
47
48
		$path = $this->destinationDirs[$this->alias] . DIRECTORY_SEPARATOR . $relativePath;
49
		$this->alias = $this->default;
50
		return $path;
51
	}
52
53
}
54