FallbackPackage::getUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
ccs 4
cts 4
cp 1
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Rj\FrontendBundle\Package;
4
5
use Symfony\Component\Asset\PackageInterface;
6
7
class FallbackPackage implements PackageInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    private $patterns;
13
14
    /**
15
     * @var PackageInterface
16
     */
17
    private $package;
18
19
    /**
20
     * @var PackageInterface
21
     */
22
    private $fallback;
23
24
    /**
25
     * @param array            $patterns
26
     * @param PackageInterface $package
27
     */
28 11
    public function __construct(array $patterns, PackageInterface $package)
29
    {
30 11
        $this->patterns = $patterns;
31 11
        $this->package = $package;
32 11
    }
33
34
    /**
35
     * @param PackageInterface $fallback
36
     *
37
     * @return $this
38
     */
39 11
    public function setFallback(PackageInterface $fallback)
40
    {
41 11
        $this->fallback = $fallback;
42
43 11
        return $this;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function getVersion($path)
50
    {
51 1
        if ($this->mustFallback($path)) {
52 1
            return $this->fallback->getVersion($path);
53
        }
54
55 1
        return $this->package->getVersion($path);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 5
    public function getUrl($path, $version = null)
62
    {
63 5
        if ($this->mustFallback($path)) {
64 2
            return $this->fallback->getUrl($path);
65
        }
66
67 4
        return $this->package->getUrl($path);
68
    }
69
70
    /**
71
     * @param string $path
72
     *
73
     * @return bool
74
     */
75 6
    protected function mustFallback($path)
76
    {
77 6
        foreach ($this->patterns as $pattern) {
78 6
            if (1 === preg_match("/$pattern/", $path)) {
79 6
                return true;
80
            }
81
        }
82
83 5
        return false;
84
    }
85
}
86