Completed
Push — master ( 3c7667...799620 )
by Kamil
94:52 queued 57:43
created

Bundle/ThemeBundle/Asset/Package/UrlPackage.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ThemeBundle\Asset\Package;
15
16
use Sylius\Bundle\ThemeBundle\Asset\PathResolverInterface;
17
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
18
use Symfony\Component\Asset\Context\ContextInterface;
19
use Symfony\Component\Asset\Exception\InvalidArgumentException;
20
use Symfony\Component\Asset\UrlPackage as BaseUrlPackage;
21
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
22
23
/**
24
 * @see BaseUrlPackage
25
 */
26
class UrlPackage extends BaseUrlPackage
27
{
28
    /**
29
     * @var array
30
     */
31
    private $baseUrls = [];
32
33
    /**
34
     * @var UrlPackage
35
     */
36
    private $sslPackage;
37
38
    /**
39
     * @var ThemeContextInterface
40
     */
41
    protected $themeContext;
42
43
    /**
44
     * @var PathResolverInterface
45
     */
46
    protected $pathResolver;
47
48
    /**
49
     * @param string|array $baseUrls Base asset URLs
50
     * @param VersionStrategyInterface $versionStrategy The version strategy
51
     * @param ThemeContextInterface $themeContext
52
     * @param PathResolverInterface $pathResolver
53
     * @param ContextInterface|null $context Context
54
     */
55
    public function __construct(
56
        $baseUrls,
57
        VersionStrategyInterface $versionStrategy,
58
        ThemeContextInterface $themeContext,
59
        PathResolverInterface $pathResolver,
60
        ?ContextInterface $context = null
61
    ) {
62
        parent::__construct($baseUrls, $versionStrategy, $context);
0 ignored issues
show
It seems like $baseUrls defined by parameter $baseUrls on line 56 can also be of type array; however, Symfony\Component\Asset\UrlPackage::__construct() does only seem to accept string|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
63
64
        if (!is_array($baseUrls)) {
65
            $baseUrls = (array) $baseUrls;
66
        }
67
68
        foreach ($baseUrls as $baseUrl) {
69
            $this->baseUrls[] = rtrim($baseUrl, '/');
70
        }
71
72
        $sslUrls = $this->getSslUrls($baseUrls);
73
74
        if ($sslUrls && $baseUrls !== $sslUrls) {
75
            $this->sslPackage = new self($sslUrls, $versionStrategy, $themeContext, $pathResolver);
76
        }
77
78
        $this->themeContext = $themeContext;
79
        $this->pathResolver = $pathResolver;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getUrl($path): string
86
    {
87
        if ($this->isAbsoluteUrl($path)) {
88
            return $path;
89
        }
90
91
        if (null !== $this->sslPackage && $this->getContext()->isSecure()) {
92
            return $this->sslPackage->getUrl($path);
93
        }
94
95
        $theme = $this->themeContext->getTheme();
96
        if (null !== $theme) {
97
            $path = $this->pathResolver->resolve($path, $theme);
98
        }
99
100
        $url = $this->getVersionStrategy()->applyVersion($path);
101
102
        if ($this->isAbsoluteUrl($url)) {
103
            return $url;
104
        }
105
106
        if ($url && '/' != $url[0]) {
107
            $url = '/' . $url;
108
        }
109
110
        return $this->getBaseUrl($path) . $url;
111
    }
112
113
    /**
114
     * @param array $urls
115
     *
116
     * @return array
117
     */
118
    private function getSslUrls(array $urls): array
119
    {
120
        $sslUrls = [];
121
122
        foreach ($urls as $url) {
123
            if ('https://' === substr($url, 0, 8) || '//' === substr($url, 0, 2)) {
124
                $sslUrls[] = $url;
125
            } elseif ('http://' !== substr($url, 0, 7)) {
126
                throw new InvalidArgumentException(sprintf('"%s" is not a valid URL', $url));
127
            }
128
        }
129
130
        return $sslUrls;
131
    }
132
}
133