ResourceValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
ccs 0
cts 13
cp 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getWebRelativePath() 0 4 1
A getProjectRelativePath() 0 4 1
A getRelativePath() 0 21 3
1
<?php declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 10.07.16 at 16:24
5
 */
6
namespace samsonphp\resource;
7
8
use samsonphp\resource\exception\ResourceNotFound;
9
10
/**
11
 * Class for assets path generation.
12
 * @deprecated Should be changed to avoid this rewritings
13
 * @package    samsonphp\resource
14
 */
15
class ResourceValidator
16
{
17
    /** @var string Full path to project web root directory */
18
    public static $webRoot;
19
20
    /** @var string Full path to project root directory */
21
    public static $projectRoot;
22
23
    /** @var string Full path to project cache root directory */
24
    public static $cacheRoot;
25
26
    /**
27
     * Build relative path to static resource relatively to web root path.
28
     *
29
     * @param string $relativePath Relative path to static resource
30
     * @param string $parentPath   Path to parent entity
31
     *
32
     * @return string Validated relative path to static resource relatively to web root path
33
     * @throws ResourceNotFound
34
     */
35
    public static function getWebRelativePath($relativePath, $parentPath = '')
36
    {
37
        return static::getRelativePath($relativePath, $parentPath, static::$webRoot);
38
    }
39
40
    /**
41
     * Build correct relative path to static resource using relative path and parent path.
42
     *
43
     * @param string $relativePath Relative path to static resource
44
     * @param string $parentPath   Path to parent entity
45
     * @param string $rootPath     Root path for relative path building
46
     *
47
     * @return string Validated relative path to static resource
48
     * @throws ResourceNotFound
49
     */
50
    public static function getRelativePath($relativePath, $parentPath = '', $rootPath = '')
51
    {
52
        // If parent path if not passed - use project root path
53
        $parentPath = $parentPath === '' ? static::$projectRoot : $parentPath;
54
55
        // Build full path to resource from given relative path
56
        $fullPath = rtrim($parentPath, DIRECTORY_SEPARATOR)
57
            . DIRECTORY_SEPARATOR
58
            . ltrim($relativePath, DIRECTORY_SEPARATOR);
59
60
        // Make real path with out possible "../"
61
        $realPath = realpath($fullPath);
62
63
        // Output link to static resource handler with relative path to project root
64
        if ($realPath) {
65
            // Build relative path to static resource
66
            return str_replace($rootPath, '', $realPath);
67
        }
68
69
        throw new ResourceNotFound($fullPath);
70
    }
71
72
    /**
73
     * Build relative path to static resource relatively to project root path.
74
     *
75
     * @param string $relativePath Relative path to static resource
76
     * @param string $parentPath   Path to parent entity
77
     *
78
     * @return string Validated relative path to static resource relatively to project root path
79
     * @throws ResourceNotFound
80
     */
81
    public static function getProjectRelativePath($relativePath, $parentPath = '')
82
    {
83
        return static::getRelativePath($relativePath, $parentPath, static::$projectRoot);
84
    }
85
}