Completed
Push — master ( 4207f0...333332 )
by Kamil
23:19 queued 01:42
created

BundleResourceLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 2
b 1
f 0
cc 1
eloc 3
nc 1
nop 2
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\Locator;
13
14
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\HttpKernel\KernelInterface;
17
18
/**
19
 * @author Kamil Kokot <[email protected]>
20
 */
21
final class BundleResourceLocator implements ResourceLocatorInterface
22
{
23
    /**
24
     * @var Filesystem
25
     */
26
    private $filesystem;
27
28
    /**
29
     * @var KernelInterface
30
     */
31
    private $kernel;
32
33
    /**
34
     * @param Filesystem $filesystem
35
     * @param KernelInterface $kernel
36
     */
37
    public function __construct(Filesystem $filesystem, KernelInterface $kernel)
38
    {
39
        $this->filesystem = $filesystem;
40
        $this->kernel = $kernel;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @param string $resourcePath Eg. "@AcmeBundle/Resources/views/template.html.twig"
47
     */
48
    public function locateResource($resourcePath, ThemeInterface $theme)
49
    {
50
        $this->assertResourcePathIsValid($resourcePath);
51
52
        $bundleName = $this->getBundleNameFromResourcePath($resourcePath);
53
        $resourceName = $this->getResourceNameFromResourcePath($resourcePath);
54
55
        $bundles = $this->kernel->getBundle($bundleName, false);
56
        foreach ($bundles as $bundle) {
0 ignored issues
show
Bug introduced by
The expression $bundles of type object<Symfony\Component...undle\BundleInterface>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
57
            $path = sprintf('%s/%s/%s', $theme->getPath(), $bundle->getName(), $resourceName);
58
59
            if ($this->filesystem->exists($path)) {
60
                return $path;
61
            }
62
        }
63
64
        throw new ResourceNotFoundException($resourcePath, $theme);
65
    }
66
67
    /**
68
     * @param string $resourcePath
69
     */
70
    private function assertResourcePathIsValid($resourcePath)
71
    {
72
        if ('@' !== substr($resourcePath, 0, 1)) {
73
            throw new \InvalidArgumentException(sprintf('Bundle resource path (given "%s") should start with an "@".', $resourcePath));
74
        }
75
76
        if (false !== strpos($resourcePath, '..')) {
77
            throw new \InvalidArgumentException(sprintf('File name "%s" contains invalid characters (..).', $resourcePath));
78
        }
79
80
        if (false === strpos($resourcePath, 'Resources/')) {
81
            throw new \InvalidArgumentException(sprintf('Resource path "%s" should be in bundles\' "Resources/" directory.', $resourcePath));
82
        }
83
    }
84
85
    /**
86
     * @param string $resourcePath
87
     *
88
     * @return string
89
     */
90
    private function getBundleNameFromResourcePath($resourcePath)
91
    {
92
        return substr($resourcePath, 1, strpos($resourcePath, '/') - 1);
93
    }
94
95
    /**
96
     * @param string $resourcePath
97
     *
98
     * @return string
99
     */
100
    private function getResourceNameFromResourcePath($resourcePath)
101
    {
102
        return substr($resourcePath, strpos($resourcePath, 'Resources/') + strlen('Resources/'));
103
    }
104
}
105