Passed
Pull Request — master (#8)
by Martin
06:34 queued 01:38
created

DatabaseTwigLoader::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Alpha\TwigBundle\Loader;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\NoResultException;
9
use Twig\Error\LoaderError;
10
use Twig\Loader\LoaderInterface;
11
use Twig\Source;
12
13
class DatabaseTwigLoader implements LoaderInterface
14
{
15
    protected $entityManager;
16
    protected $entity;
17
18
    public function __construct(EntityManagerInterface $entityManager, string $entity)
19
    {
20
        $this->entityManager = $entityManager;
21
        $this->entity = $entity;
22
    }
23
24
    public function getCacheKey($name): string
25
    {
26
        return $name;
27
    }
28
29
    public function exists($templateName): bool
30
    {
31
        try {
32
            $template = $this->getTemplate($templateName);
33
34
            return $template instanceof $this->entity;
35
        } catch (NoResultException $e) {
36
            return false;
37
        }
38
    }
39
40
    private function getTemplate(string $templateName): object
41
    {
42
        return $this->entityManager
43
            ->getRepository($this->entity)
44
            ->createQueryBuilder('t')
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Doctrine\ORM\EntityRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            ->/** @scrutinizer ignore-call */ createQueryBuilder('t')
Loading history...
45
            ->select('t')
46
            ->where('t.name = :name')
47
            ->setMaxResults(1)
48
            ->getQuery()
49
            ->setParameter('name', $templateName)
50
            ->getSingleResult();
51
    }
52
53
    public function getSourceContext($templateName): Source
54
    {
55
        $templateSource = $this->getValue('source', $templateName);
56
        if (!is_string($templateSource) || mb_strlen($templateSource) < 1) {
57
            throw new LoaderError(sprintf('Template "%s" does not exist.', $templateName));
58
        }
59
60
        return new Source($templateSource, $templateName);
61
    }
62
63
    public function isFresh($templateName, $time): bool
64
    {
65
        try {
66
            $lastModified = $this->getValue('lastModified', $templateName);
67
            if (null === $lastModified) {
68
                return false;
69
            }
70
71
            return strtotime($lastModified) <= $time;
72
        } catch (NoResultException $e) {
73
            return false;
74
        }
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    private function getValue(string $column, string $templateName)
81
    {
82
        return $this->entityManager
83
            ->getRepository($this->entity)
84
            ->createQueryBuilder('t')
85
            ->select(sprintf('t.%s', $column))
86
            ->where('t.name = :name')
87
            ->setMaxResults(1)
88
            ->getQuery()
89
            ->setParameter('name', $templateName)
90
            ->getSingleScalarResult();
91
    }
92
}
93