Completed
Push — theme-bundle ( aad440...af4cdd )
by Kamil
15:45
created

TemplateFileLocator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 53
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A locate() 0 17 4
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\Templating\Locator;
13
14
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
15
use Sylius\Bundle\ThemeBundle\Locator\ResourceNotFoundException;
16
use Symfony\Component\Config\FileLocatorInterface;
17
use Symfony\Component\Templating\TemplateReferenceInterface;
18
19
/**
20
 * {@inheritdoc}
21
 *
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
final class TemplateFileLocator implements FileLocatorInterface
25
{
26
    /**
27
     * @var FileLocatorInterface
28
     */
29
    private $decoratedFileLocator;
30
31
    /**
32
     * @var ThemeContextInterface
33
     */
34
    private $themeContext;
35
36
    /**
37
     * @var TemplateLocatorInterface
38
     */
39
    private $templateLocator;
40
41
    /**
42
     * @param FileLocatorInterface $decoratedFileLocator
43
     * @param ThemeContextInterface $themeContext
44
     * @param TemplateLocatorInterface $templateLocator
45
     */
46
    public function __construct(
47
        FileLocatorInterface $decoratedFileLocator,
48
        ThemeContextInterface $themeContext,
49
        TemplateLocatorInterface $templateLocator
50
    ) {
51
        $this->decoratedFileLocator = $decoratedFileLocator;
52
        $this->themeContext = $themeContext;
53
        $this->templateLocator = $templateLocator;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function locate($template, $currentPath = null, $first = true)
60
    {
61
        if (!$template instanceof TemplateReferenceInterface) {
62
            throw new \InvalidArgumentException('The template must be an instance of TemplateReferenceInterface.');
63
        }
64
65
        $themes = $this->themeContext->getThemeHierarchy();
66
        foreach ($themes as $theme) {
67
            try {
68
                return $this->templateLocator->locateTemplate($template, $theme);
69
            } catch (ResourceNotFoundException $exception) {
70
                // Ignore if resource cannot be found in given theme.
71
            }
72
        }
73
74
        return $this->decoratedFileLocator->locate($template, $currentPath, $first);
75
    }
76
}
77