Completed
Push — master ( c383ac...a7fbb8 )
by Milan
01:52 queued 01:52
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 $default;
13
14
	public function __construct(array $destinationDirs)
15
	{
16
		$this->destinationDirs = $destinationDirs;
17
		reset($destinationDirs);
18
		$this->default = key($destinationDirs);
19
	}
20
21
	/**
22
	 * @param string|IStoreFile $relativePath
23
	 * @param string|NULL $destinationAlias
24
	 * @return string
25
	 */
26
	public function createAbsolutePath($relativePath, $destinationAlias = NULL)
27
	{
28
		if ($relativePath instanceof IStoreFile) {
29
			$relativePath = $relativePath->getRelativePath();
30
		}
31
32
		if ($destinationAlias === NULL) {
33
			$path = $this->destinationDirs[$this->default];
34
		} elseif (isset($this->destinationDirs[$destinationAlias])) {
35
			$path = $this->destinationDirs[$destinationAlias];
36
		} else {
37
			throw new InvalidArgumentException('Destination alias does not exists "' . $destinationAlias . '".');
38
		}
39
40
		return $path . DIRECTORY_SEPARATOR . $relativePath;
41
	}
42
43
}
44