Completed
Push — theme/path-package ( f6cab5...1424db )
by Kamil
59:20 queued 35:47
created

PathPackage::getUrl()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 6
eloc 10
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ThemeBundle\Asset\Package;
13
14
use Sylius\Bundle\ThemeBundle\Asset\PathResolverInterface;
15
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
16
use Symfony\Component\Asset\Context\ContextInterface;
17
use Symfony\Component\Asset\PathPackage as BasePathPackage;
18
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
19
20
/**
21
 * @see BasePathPackage
22
 *
23
 * @author Kamil Kokot <[email protected]>
24
 */
25
class PathPackage extends BasePathPackage
26
{
27
    /**
28
     * @var ThemeContextInterface
29
     */
30
    protected $themeContext;
31
32
    /**
33
     * @var PathResolverInterface
34
     */
35
    protected $pathResolver;
36
37
    /**
38
     * @param string $basePath
39
     * @param VersionStrategyInterface $versionStrategy
40
     * @param ThemeContextInterface $themeContext
41
     * @param PathResolverInterface $pathResolver
42
     * @param ContextInterface|null $context
43
     */
44
    public function __construct(
45
        $basePath,
46
        VersionStrategyInterface $versionStrategy,
47
        ThemeContextInterface $themeContext,
48
        PathResolverInterface $pathResolver,
49
        ContextInterface $context = null
50
    ) {
51
        parent::__construct($basePath, $versionStrategy, $context);
52
53
        $this->themeContext = $themeContext;
54
        $this->pathResolver = $pathResolver;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getUrl($path)
61
    {
62
        if ($this->isAbsoluteUrl($path)) {
63
            return $path;
64
        }
65
66
        $theme = $this->themeContext->getTheme();
67
        if (null !== $theme) {
68
            $path = $this->pathResolver->resolve($path, $theme);
69
        }
70
71
        $versionedPath = $this->getVersionStrategy()->applyVersion($path);
72
73
        // if absolute or begins with /, we're done
74
        if ($this->isAbsoluteUrl($versionedPath) || ($versionedPath && '/' === $versionedPath[0])) {
75
            return $versionedPath;
76
        }
77
78
        return $this->getBasePath() . ltrim($versionedPath, '/');
79
    }
80
}
81