Completed
Push — master ( cc0315...505aa7 )
by Vitaly
02:04
created

Resource::getRelativePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 8
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 17
rs 9.4285
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 15.05.16 at 10:52
5
 */
6
namespace samsonphp\resource;
7
8
use samsonphp\resource\exception\ResourceNotFound;
9
10
/**
11
 * Static resource entity class.
12
 *
13
 * @package samsonphp\resource
14
 */
15
class Resource
16
{
17
    /**
18
     * Build correct relative path to static resource using relative path and parent path.
19
     *
20
     * @param string $relativePath Relative path to static resource
21
     * @param string $parentPath Path to parent entity
22
     *
23
     * @return string Validated relative path to static resource
24
     * @throws ResourceNotFound
25
     */
26
    public static function getRelativePath($relativePath, $parentPath = '')
27
    {
28
        // Build full path to resource from given relative path
29
        $fullPath = rtrim($parentPath, DIRECTORY_SEPARATOR)
30
            .DIRECTORY_SEPARATOR
31
            .ltrim($relativePath, DIRECTORY_SEPARATOR);
32
33
        // Make real path with out possible "../"
34
        $realPath = realpath($fullPath);
35
36
        // Output link to static resource handler with relative path to project root
37
        if ($realPath) {
38
            return str_replace(dirname(getcwd()), '', $realPath);
39
        }
40
41
        throw new ResourceNotFound($fullPath);
42
    }
43
}
44