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

CachedTemplateLocator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A locateTemplate() 0 15 3
A getCacheKey() 0 4 1
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 Doctrine\Common\Cache\Cache;
15
use Sylius\Bundle\ThemeBundle\Locator\ResourceNotFoundException;
16
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
17
use Symfony\Component\Templating\TemplateReferenceInterface;
18
19
/**
20
 * @author Kamil Kokot <[email protected]>
21
 */
22
final class CachedTemplateLocator implements TemplateLocatorInterface
23
{
24
    /**
25
     * @var TemplateLocatorInterface
26
     */
27
    private $decoratedTemplateLocator;
28
29
    /**
30
     * @var Cache
31
     */
32
    private $cache;
33
34
    /**
35
     * @param TemplateLocatorInterface $decoratedTemplateLocator
36
     * @param Cache $cache
37
     */
38
    public function __construct(TemplateLocatorInterface $decoratedTemplateLocator, Cache $cache)
39
    {
40
        $this->decoratedTemplateLocator = $decoratedTemplateLocator;
41
        $this->cache = $cache;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function locateTemplate(TemplateReferenceInterface $template, ThemeInterface $theme)
48
    {
49
        $cacheKey = $this->getCacheKey($template, $theme);
50
        if ($this->cache->contains($cacheKey)) {
51
            $location = $this->cache->fetch($cacheKey);
52
53
            if (null === $location) {
54
                throw new ResourceNotFoundException($template->getPath(), $theme);
55
            }
56
57
            return $location;
58
        }
59
60
        return $this->decoratedTemplateLocator->locateTemplate($template, $theme);
61
    }
62
63
    /**
64
     * @param TemplateReferenceInterface $template
65
     * @param ThemeInterface $theme
66
     *
67
     * @return string
68
     */
69
    private function getCacheKey(TemplateReferenceInterface $template, ThemeInterface $theme)
70
    {
71
        return $template->getLogicalName() . '|' . $theme->getSlug();
72
    }
73
}
74