Completed
Pull Request — master (#1194)
by Greg
15:51
created

BundleResourceLocator::locate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Locator;
16
17
use SWP\Bundle\CoreBundle\Detection\DeviceDetectionInterface;
18
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
19
use Symfony\Component\Filesystem\Filesystem;
20
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
21
use Symfony\Component\HttpKernel\KernelInterface;
22
23
class BundleResourceLocator
24
{
25
    /**
26
     * @var Filesystem
27
     */
28
    private $filesystem;
29
30
    /**
31
     * @var KernelInterface
32
     */
33
    private $kernel;
34
35
    /**
36
     * @var DeviceDetectionInterface
37
     */
38
    private $deviceDetection;
39
40
    public function __construct(Filesystem $filesystem, KernelInterface $kernel, DeviceDetectionInterface $deviceDetection)
41
    {
42
        $this->filesystem = $filesystem;
43
        $this->kernel = $kernel;
44
        $this->deviceDetection = $deviceDetection;
45
    }
46 101
47
    /**
48 101
     * {@inheritdoc}
49 101
     */
50 101
    public function locate(string $resourcePath, ThemeInterface $theme): string
51 101
    {
52
        $this->assertResourcePathIsValid($resourcePath);
53
54
        if (false !== strpos($resourcePath, 'Bundle/Resources/views/')) {
55
            // When using bundle notation, we get a path like @AcmeBundle/Resources/views/template.html.twig
56
            return $this->locateResourceBasedOnBundleNotation($resourcePath, $theme);
57
        }
58 19
59
        // When using namespaced Twig paths, we get a path like @Acme/template.html.twig
60 19
        return $this->locateResourceBasedOnTwigNamespace($resourcePath, $theme);
61 19
    }
62 19
63 19
    private function assertResourcePathIsValid(string $resourcePath): void
64
    {
65
        if (0 !== strpos($resourcePath, '@')) {
66
            throw new \InvalidArgumentException(sprintf('Bundle resource path (given "%s") should start with an "@".', $resourcePath));
67 19
        }
68
69
        if (false !== strpos($resourcePath, '..')) {
70
            throw new \InvalidArgumentException(sprintf('File name "%s" contains invalid characters (..).', $resourcePath));
71
        }
72
    }
73
74 19
    private function locateResourceBasedOnBundleNotation(string $resourcePath, ThemeInterface $theme): string
75
    {
76 19
        $bundleName = substr($resourcePath, 1, strpos($resourcePath, '/') - 1);
77 19
        $resourceName = substr($resourcePath, strpos($resourcePath, 'Resources/') + strlen('Resources/'));
78 19
79 19
        // Symfony 4.0+ always returns a single bundle
80 19
        /** @var BundleInterface|BundleInterface[] $bundles */
81 19
        $bundles = $this->kernel->getBundle($bundleName, false);
0 ignored issues
show
Unused Code introduced by
The call to KernelInterface::getBundle() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
82 19
83 14
        // So we need to hack it to support both Symfony 3.4 and Symfony 4.0+
84
        if (!is_array($bundles)) {
85 19
            $bundles = [$bundles];
86
        }
87
88
        foreach ($bundles as $bundle) {
89 19 View Code Duplication
            if (null !== $this->deviceDetection->getType()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
                $path = sprintf('%s/%s/%s/%s', $theme->getPath(), $this->deviceDetection->getType(), $bundle->getName(), $resourceName);
91
                if ($this->filesystem->exists($path)) {
92
                    return $path;
93
                }
94
            }
95 19
96
            $path = sprintf('%s/%s/%s', $theme->getPath(), $bundle->getName(), $resourceName);
97 19
            if ($this->filesystem->exists($path)) {
98
                return $path;
99
            }
100
        }
101 19
102
//        throw new ResourceNotFoundException($resourcePath, $theme);
103
    }
104
105 19
    private function locateResourceBasedOnTwigNamespace(string $resourcePath, ThemeInterface $theme): string
106
    {
107
        $twigNamespace = substr($resourcePath, 1, strpos($resourcePath, '/') - 1);
108 19
        $resourceName = substr($resourcePath, strpos($resourcePath, '/') + 1);
109 View Code Duplication
        if (null !== $this->deviceDetection->getType()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
            $path = sprintf('%s/%s/%s/%s', $theme->getPath(), $this->deviceDetection->getType(), $this->getBundleOrPluginName($twigNamespace), $resourceName);
111
            if ($this->filesystem->exists($path)) {
112
                return $path;
113
            }
114
        }
115 19
116
        $path = sprintf('%s/%s/views/%s', $theme->getPath(), $this->getBundleOrPluginName($twigNamespace), $resourceName);
117 19
118
        if ($this->filesystem->exists($path)) {
119
            return $path;
120
        }
121
122
//        throw new ResourceNotFoundException($resourcePath, $theme);
123
    }
124
125 19
    private function getBundleOrPluginName(string $twigNamespace): string
126
    {
127 19
        if ('Plugin' === substr($twigNamespace, -6)) {
128
            return $twigNamespace;
129
        }
130
131
        return $twigNamespace.'Bundle';
132
    }
133
134
    public function supports(string $template): bool
135
    {
136
        return strpos($template, '@') !== 0;
137
    }
138
}
139