DatabaseTwigLoader::getCacheKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Alpha\TwigBundle\Loader;
6
7
use Doctrine\DBAL\Exception\TableNotFoundException;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\NoResultException;
10
use Twig\Error\LoaderError;
11
use Twig\Loader\LoaderInterface;
12
use Twig\Source;
13
14
class DatabaseTwigLoader implements LoaderInterface
15
{
16
    protected $entityManager;
17
    protected $entity;
18
19
    public function __construct(EntityManagerInterface $entityManager, string $entity)
20
    {
21
        $this->entityManager = $entityManager;
22
        $this->entity = $entity;
23
    }
24
25
    public function getCacheKey($name): string
26
    {
27
        return $name;
28
    }
29
30
    public function exists($templateName): bool
31
    {
32
        try {
33
            $template = $this->getTemplate($templateName);
34
35
            return $template instanceof $this->entity;
36
        } catch (NoResultException | TableNotFoundException $exception) {
37
            return false;
38
        }
39
    }
40
41
    private function getTemplate(string $templateName): object
42
    {
43
        return $this->entityManager
44
            ->getRepository($this->entity)
45
            ->createQueryBuilder('t')
46
            ->select('t')
47
            ->where('t.name = :name')
48
            ->setMaxResults(1)
49
            ->getQuery()
50
            ->setParameter('name', $templateName)
51
            ->getSingleResult();
52
    }
53
54
    public function getSourceContext($templateName): Source
55
    {
56
        $templateSource = $this->getValue('source', $templateName);
57
        if (!is_string($templateSource) || mb_strlen($templateSource) < 1) {
58
            throw new LoaderError(sprintf('Template "%s" does not exist.', $templateName));
59
        }
60
61
        return new Source($templateSource, $templateName);
62
    }
63
64
    public function isFresh($templateName, $time): bool
65
    {
66
        try {
67
            $lastModified = $this->getValue('lastModified', $templateName);
68
            if (null === $lastModified) {
69
                return false;
70
            }
71
72
            return strtotime($lastModified) <= $time;
73
        } catch (NoResultException $exception) {
74
            return false;
75
        }
76
    }
77
78
    /**
79
     * @return mixed
80
     */
81
    private function getValue(string $column, string $templateName)
82
    {
83
        return $this->entityManager
84
            ->getRepository($this->entity)
85
            ->createQueryBuilder('t')
86
            ->select(sprintf('t.%s', $column))
87
            ->where('t.name = :name')
88
            ->setMaxResults(1)
89
            ->getQuery()
90
            ->setParameter('name', $templateName)
91
            ->getSingleScalarResult();
92
    }
93
}
94