Passed
Push — master ( 374698...e16807 )
by Milan
01:50
created

DocumentRoot   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setDestination() 0 8 2
A createAbsolutePath() 0 10 2
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