Completed
Push — master ( 60e59a...334051 )
by Adam
06:35
created

PathResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B expandPath() 0 27 6
1
<?php
2
/**
3
 * PathResolver.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Packages!
9
 * @subpackage     Helpers
10
 * @since          1.0.0
11
 *
12
 * @date           19.06.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Packages\Helpers;
18
19
use Nette;
20
use Nette\Utils;
21
22
use IPub;
23
use IPub\Packages;
24
use IPub\Packages\Exceptions;
25
use IPub\Packages\Repository;
26
27
/**
28
 * Package path resolver
29
 *
30
 * @package        iPublikuj:Packages!
31
 * @subpackage     Helpers
32
 *
33
 * @author         Adam Kadlec <[email protected]>
34
 */
35
final class PathResolver extends Nette\Object
36
{
37
	/**
38
	 * Define class name
39
	 */
40
	const CLASS_NAME = __CLASS__;
41
42
	/**
43
	 * @var Repository\IRepository
44
	 */
45
	private $repository;
46
47
	/**
48
	 * @param Repository\IRepository $repository
49
	 */
50
	public function __construct(Repository\IRepository $repository)
51
	{
52
		$this->repository = $repository;
53
	}
54
55
	/**
56
	 * Expands @foo/path/....
57
	 *
58
	 * @param string $path
59
	 * @param string|NULL $localPrefix
60
	 *
61
	 * @return string
62
	 */
63
	public function expandPath(string $path, string $localPrefix = NULL) : string
64
	{
65
		$path = Utils\Strings::replace($path, '~\\\~', '/');
66
67
		if (substr($path, 0, 1) !== '@') {
68
			return $path;
69
		}
70
71
		$pos = strpos($path, '/');
72
73
		if ($pos) {
74
			$package = substr($path, 1, $pos - 1);
75
76
		} else {
77
			$package = substr($path, 1);
78
		}
79
80
		$package = str_replace('.', '/', $package);
81
82
		if (!$this->repository->findPackage($package)) {
83
			throw new Exceptions\InvalidArgumentException(sprintf('Package \'%s\' does not exist.', $package));
84
		}
85
86
		$path = $this->repository->findPackage($package)->getPath() . ($localPrefix ? '/' . $localPrefix : '') . ($pos ? substr($path, $pos) : '');
87
88
		return Utils\Strings::replace($path, '~\\\~', '/');
89
	}
90
}
91