Passed
Push — master ( 4ad38e...2a3104 )
by Carsten
01:55
created

AssetTimestamper::buildTimestampedPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 9.3142
cc 1
eloc 13
nc 1
nop 2
crap 1
1
<?php
2
namespace Germania\AssetTimestamper;
3
4
class AssetTimestamper
5
{
6
7
    /**
8
     * @var string
9
     */
10
    public $base_path;
11
12
13
    /**
14
     * Timestamp format to use.
15
     *
16
     * @see http://php.net/manual/de/function.date.php
17
     * @var string
18
     */
19
    public $format = 'YmdHis';
20
21
    /**
22
     * Separator sign that preceeds the timestamp
23
     * @var string
24
     */
25
    public $separator = '.';
26
27
28
    /**
29
     * @param string $base_path Optional: Base path, default: Current work dir.
30
     * @uses   $base_path
31
     */
32 11
    public function __construct( $base_path = null )
33
    {
34 11
        $this->base_path = $base_path ?: getcwd();
35 11
    }
36
37
    /**
38
     * @return string
39
     * @uses   $base_path
40
     */
41 3
    public function getBasePath()
42
    {
43 3
        return $this->base_path;
44
    }
45
46
    /**
47
     * @param  string $asset Asset URL
48
     * @return string Timestamped Asset URL
49
     * @uses   $base_path
50
     */
51 8
    public function __invoke( $asset )
52
    {
53
        // Parse asset URL
54 8
        $asset_parts = parse_url( $asset );
55
56
        // Exclude if asset seems to come from different location,
57
        // i.e. if it has defined hostname
58 8
        if (is_array($asset_parts)
59 8
        && !empty($asset_parts['host'])) {
60 2
            return $asset;
61
        }
62
63
        // Prepend DIRECTORY_SEPARATOR if missing
64 6
        $work_asset = (substr($asset, 0, strlen(\DIRECTORY_SEPARATOR)) === \DIRECTORY_SEPARATOR)
65 6
        ? $asset
66 6
        : \DIRECTORY_SEPARATOR . $asset;
67
68
        // Glue base path and asset; throw if file not existant
69 6
        if (!$real_file = realpath($this->base_path . $work_asset)) {
70 2
            return $asset;
71
        }
72
73 4
        $result = $this->buildTimestampedPath($asset, $real_file);
74 4
        return $result;
75
    }
76
77
78
    /**
79
     * @param  string $asset     Asset path
80
     * @param  string $real_file Real file path
81
     * @return string            The timestamped asset file name
82
     */
83 4
    public function buildTimestampedPath($asset, $real_file)
84
    {
85
        // Build result
86 4
        $timestamp = date( $this->format, filemtime( $real_file ));
87
88 4
        $path_info = pathinfo( $asset );
89
90 4
        $result = str_replace(
91 4
            $path_info['basename'],
92 4
            join( "", [
93 4
                $path_info['filename'],
94 4
                $this->separator,
95 4
                $timestamp,
96 4
                ".",
97 4
                $path_info['extension']
98 4
            ]),
99
            $asset
100 4
        );
101
102 4
        return $result;
103
    }
104
}
105