Completed
Push — master ( 98f65f...94fc25 )
by Paweł
21:33 queued 10:44
created

locateResourceBasedOnTwigNamespace()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19

Duplication

Lines 6
Ratio 31.58 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 6
loc 19
ccs 4
cts 4
cp 1
rs 9.6333
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 4
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\Locator\ResourceLocatorInterface;
19
use Sylius\Bundle\ThemeBundle\Locator\ResourceNotFoundException;
20
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
21
use Symfony\Component\Filesystem\Filesystem;
22
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
23
use Symfony\Component\HttpKernel\KernelInterface;
24
25
class BundleResourceLocator implements ResourceLocatorInterface
26
{
27
    /**
28
     * @var Filesystem
29
     */
30
    private $filesystem;
31
32
    /**
33
     * @var KernelInterface
34
     */
35
    private $kernel;
36
37
    /**
38
     * @var DeviceDetectionInterface
39
     */
40
    private $deviceDetection;
41
42
    public function __construct(Filesystem $filesystem, KernelInterface $kernel, DeviceDetectionInterface $deviceDetection)
43
    {
44
        $this->filesystem = $filesystem;
45
        $this->kernel = $kernel;
46 101
        $this->deviceDetection = $deviceDetection;
47
    }
48 101
49 101
    /**
50 101
     * {@inheritdoc}
51 101
     */
52
    public function locateResource(string $resourcePath, ThemeInterface $theme): string
53
    {
54
        $this->assertResourcePathIsValid($resourcePath);
55
56
        if (false !== strpos($resourcePath, 'Bundle/Resources/views/')) {
57
            // When using bundle notation, we get a path like @AcmeBundle/Resources/views/template.html.twig
58 19
            return $this->locateResourceBasedOnBundleNotation($resourcePath, $theme);
59
        }
60 19
61 19
        // When using namespaced Twig paths, we get a path like @Acme/template.html.twig
62 19
        return $this->locateResourceBasedOnTwigNamespace($resourcePath, $theme);
63 19
    }
64
65
    private function assertResourcePathIsValid(string $resourcePath): void
66
    {
67 19
        if (0 !== strpos($resourcePath, '@')) {
68
            throw new \InvalidArgumentException(sprintf('Bundle resource path (given "%s") should start with an "@".', $resourcePath));
69
        }
70
71
        if (false !== strpos($resourcePath, '..')) {
72
            throw new \InvalidArgumentException(sprintf('File name "%s" contains invalid characters (..).', $resourcePath));
73
        }
74 19
    }
75
76 19
    private function locateResourceBasedOnBundleNotation(string $resourcePath, ThemeInterface $theme): string
77 19
    {
78 19
        $bundleName = substr($resourcePath, 1, strpos($resourcePath, '/') - 1);
79 19
        $resourceName = substr($resourcePath, strpos($resourcePath, 'Resources/') + strlen('Resources/'));
80 19
81 19
        // Symfony 4.0+ always returns a single bundle
82 19
        /** @var BundleInterface|BundleInterface[] $bundles */
83 14
        $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...
84
85 19
        // So we need to hack it to support both Symfony 3.4 and Symfony 4.0+
86
        if (!is_array($bundles)) {
87
            $bundles = [$bundles];
88
        }
89 19
90
        foreach ($bundles as $bundle) {
91 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...
92
                $path = sprintf('%s/%s/%s/%s', $theme->getPath(), $this->deviceDetection->getType(), $bundle->getName(), $resourceName);
93
                if ($this->filesystem->exists($path)) {
94
                    return $path;
95 19
                }
96
            }
97 19
98
            $path = sprintf('%s/%s/%s', $theme->getPath(), $bundle->getName(), $resourceName);
99
            if ($this->filesystem->exists($path)) {
100
                return $path;
101 19
            }
102
        }
103
104
        throw new ResourceNotFoundException($resourcePath, $theme);
105 19
    }
106
107
    private function locateResourceBasedOnTwigNamespace(string $resourcePath, ThemeInterface $theme): string
108 19
    {
109
        $twigNamespace = substr($resourcePath, 1, strpos($resourcePath, '/') - 1);
110
        $resourceName = substr($resourcePath, strpos($resourcePath, '/') + 1);
111 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...
112
            $path = sprintf('%s/%s/%s/%s', $theme->getPath(), $this->deviceDetection->getType(), $this->getBundleOrPluginName($twigNamespace), $resourceName);
113
            if ($this->filesystem->exists($path)) {
114
                return $path;
115 19
            }
116
        }
117 19
118
        $path = sprintf('%s/%s/views/%s', $theme->getPath(), $this->getBundleOrPluginName($twigNamespace), $resourceName);
119
120
        if ($this->filesystem->exists($path)) {
121
            return $path;
122
        }
123
124
        throw new ResourceNotFoundException($resourcePath, $theme);
125 19
    }
126
127 19
    private function getBundleOrPluginName(string $twigNamespace): string
128
    {
129
        if ('Plugin' === substr($twigNamespace, -6)) {
130
            return $twigNamespace;
131
        }
132
133
        return $twigNamespace.'Bundle';
134
    }
135
}
136