DoctrineLoader::findTemplate()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 8.6737
cc 5
eloc 13
nc 5
nop 1
crap 5
1
<?php
2
namespace Boekkooi\Bundle\TwigJackBundle\Twig\Loader;
3
4
use Boekkooi\Bundle\TwigJackBundle\Model\TemplateInterface;
5
use Boekkooi\Bundle\TwigJackBundle\Model\TranslatableTemplateInterface;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
8
/**
9
 * @author Warnar Boekkooi <[email protected]>
10
 */
11
class DoctrineLoader implements \Twig_LoaderInterface
12
{
13
    const KEY_SEPARATOR = '|';
14
15
    /**
16
     * @var string
17
     */
18
    protected $prefix;
19
20
    /**
21
     * @var ObjectRepository
22
     */
23
    protected $repository;
24
25
    /**
26
     * @var callable|null
27
     */
28
    protected $localeCallable;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param ObjectRepository $repository     The template repository
34
     * @param string           $prefix         The template prefix to identify database templates
35
     * @param callable|null    $localeCallable
36
     */
37 19
    public function __construct(ObjectRepository $repository, $prefix = 'database::', $localeCallable = null)
38
    {
39 19
        if ($localeCallable !== null && !is_callable($localeCallable)) {
40 1
            throw new \InvalidArgumentException('Given `localeCallable` must be a callable or NULL.');
41
        }
42
43 18
        $this->repository = $repository;
44 18
        $this->prefix = $prefix;
45 18
        $this->localeCallable = $localeCallable;
46 18
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 4
    public function getSource($name)
52
    {
53 4
        return $this->findTemplate($name)->getTemplate();
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function exists($name)
60
    {
61
        if ($this->isLoadableTemplate($name)) {
62
            $templateIdentifier = substr($name, strlen($this->prefix));
63
            $template = $this->repository->find($templateIdentifier);
64
65
            return $template !== null && $template instanceof TemplateInterface;
66
        }
67
        return false;
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 8
    public function getCacheKey($name)
74
    {
75 8
        $template = $this->findTemplate($name);
76
77 3
        if ($template instanceof TranslatableTemplateInterface) {
78
            /** @var TranslatableTemplateInterface $template */
79
80 2
            return $this->prefix . self::KEY_SEPARATOR . $template->getCurrentLocale() . self::KEY_SEPARATOR . $template->getIdentifier();
81
        }
82
83 1
        return $this->prefix . self::KEY_SEPARATOR . $template->getIdentifier();
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 6
    public function isFresh($name, $time)
90
    {
91 6
        return $this->findTemplate($name)->getLastModified()->getTimestamp() <= $time;
92
    }
93
94
    /**
95
     * Check if the given name has the correct prefix for loading.
96
     *
97
     * @param  string $name The name of the template to check
98
     * @return bool
99
     */
100 18
    protected function isLoadableTemplate($name)
101
    {
102 18
        return !empty($name) && (empty($this->prefix) || strlen($name) > strlen($this->prefix) && strpos($name, $this->prefix) === 0);
103
    }
104
105
    /**
106
     * Find a template by it's name
107
     *
108
     * @param  string             $name The name of the template to find
109
     * @return TemplateInterface
110
     * @throws \Twig_Error_Loader
111
     */
112 18
    protected function findTemplate($name)
113
    {
114 18
        if (!$this->isLoadableTemplate($name)) {
115 5
            throw new \Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "%stemplate_name").', $name, $this->prefix));
116
        }
117
118 13
        $templateIdentifier = substr($name, strlen($this->prefix));
119 13
        $template = $this->repository->find($templateIdentifier);
120 13
        if ($template === null) {
121 3
            throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $name));
122
        }
123 10
        if (!($template instanceof TemplateInterface)) {
124 3
            throw new \Twig_Error_Loader(sprintf('Unexpected template type "%s" found for template "%s".', get_class($template), $name));
125
        }
126 7
        if ($template instanceof TranslatableTemplateInterface) {
127
            /** @var TranslatableTemplateInterface $template */
128 2
            $locale = $this->getCurrentLocale();
129 2
            $template->setCurrentLocale($locale);
130 2
        }
131
132 7
        return $template;
133
    }
134
135 2
    protected function getCurrentLocale()
136
    {
137 2
        if ($currentLocaleCallable = $this->localeCallable) {
138 2
            $locale = call_user_func($currentLocaleCallable);
139 2
            if ($locale) {
140 1
                return $locale;
141
            }
142 1
        }
143
144 1
        return 'en';
145
    }
146
}
147