LastModifyStrategy::applyVersion()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 8
nc 6
nop 1
crap 4
1
<?php
2
namespace PTS\SymfonyAsset;
3
4
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
5
6
class LastModifyStrategy implements VersionStrategyInterface
7
{
8
    /** @var string */
9
    protected $staticDir;
10
    /** @var null|string */
11
    protected $cdnHost;
12
13
    /**
14
     * @param string $staticDir
15
     * @param string|null $cdnHost
16
     */
17 6
    public function __construct(string $staticDir, string $cdnHost = null)
18
    {
19 6
        $this->staticDir = $staticDir;
20 6
        $this->cdnHost = $cdnHost;
21 6
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26 5
    public function getVersion($path): string
27
    {
28 5
        $path = $this->staticDir . '/' . $path;
29 5
        return file_exists($path) ? (string)filemtime($path) : '';
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 5
    public function applyVersion($relPath): string
36
    {
37 5
        $version = $this->getVersion($relPath);
38 5
        $path = $version ? sprintf('%s?v=%s', $relPath, $version) : $relPath;
39
40 5
        if ($this->cdnHost) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->cdnHost of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
41 1
            if (mb_substr($path, 0, 1, "UTF-8") !== '/') {
42 1
                $path = '/' . $path;
43
            }
44
45 1
            $path = '//' . $this->cdnHost . $path;
46
        }
47
48 5
        return $path;
49
    }
50
}
51