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

Resource   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 10
wmc 2
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A getRelativePath() 0 17 2
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