Completed
Pull Request — master (#26)
by Paulo Rodrigues
04:47
created

FallbackPackage::getVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Rj\FrontendBundle\Package;
4
5
class FallbackPackage
6
{
7
    private $patterns;
8
    private $package;
9
    private $fallback;
10
11 9
    public function __construct($patterns, $package)
12
    {
13 9
        $this->patterns = $patterns;
14 9
        $this->package = $package;
15 9
    }
16
17 9
    public function setFallback($fallback)
18
    {
19 9
        $this->fallback = $fallback;
20
21 9
        return $this;
22
    }
23
24
    public function getVersion($path)
25
    {
26
        if ($this->mustFallback($path)) {
27
            return $this->fallback->getVersion($path);
28
        }
29
30
        return $this->package->getVersion($path);
31
    }
32
33 4
    public function getUrl($path, $version = null)
34
    {
35 4
        if ($this->mustFallback($path)) {
36 1
            return $this->fallback->getUrl($path, $version);
37
        }
38
39 3
        return $this->package->getUrl($path);
40
    }
41
42 4
    protected function mustFallback($path)
43
    {
44 4
        foreach ($this->patterns as $pattern) {
45 4
            if (1 === preg_match("/$pattern/", $path)) {
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $pattern instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
46 1
                return true;
47
            }
48
        }
49
50 3
        return false;
51
    }
52
}
53