Completed
Push — master ( 9c1f83...0c1135 )
by Paulo Rodrigues
10:00
created

FallbackPackage::mustFallback()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Rj\FrontendBundle\Package;
4
5
class FallbackPackage
6
{
7
    private $patterns;
8
    private $package;
9
    private $fallback;
10
11 23
    public function __construct($patterns, $package)
12
    {
13 23
        $this->patterns = $patterns;
14 23
        $this->package = $package;
15 23
    }
16
17 23
    public function setFallback($fallback)
18
    {
19 23
        $this->fallback = $fallback;
20
21 23
        return $this;
22
    }
23
24 2
    public function getVersion($path)
25
    {
26 2
        if ($this->mustFallback($path)) {
27 2
            return $this->fallback->getVersion($path);
28
        }
29
30 2
        return $this->package->getVersion($path);
31
    }
32
33 11
    public function getUrl($path, $version = null)
34
    {
35 11
        if ($this->mustFallback($path)) {
36 4
            return $this->fallback->getUrl($path, $version);
37
        }
38
39 9
        return $this->package->getUrl($path);
40
    }
41
42 13
    protected function mustFallback($path)
43
    {
44 13
        foreach ($this->patterns as $pattern) {
45 13
            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 13
                return true;
47
            }
48
        }
49
50 11
        return false;
51
    }
52
}
53