Completed
Push — master ( 6130d0...57338d )
by Dev
24:32 queued 11:23
created

PageRenderingValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Validator\Constraints;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
8
9
class PageRenderingValidator extends ConstraintValidator
10
{
11
    private $defaultPageTemplate;
12
    private $twig;
13
14
    public function __construct(string $defaultPageTemplate, $twig)
15
    {
16
        $this->twig = $twig;
17
        $this->defaultPageTemplate = $defaultPageTemplate;
18
    }
19
20
    public function validate($page, Constraint $constraint)
21
    {
22
        if (!$constraint instanceof PageRendering) {
23
            throw new UnexpectedTypeException($constraint, PageRendering::class);
24
        }
25
26
        if (false !== $page->getRedirection()) { // si c'est une redir, on check rien
27
            return;
28
        }
29
30
        $template = null !== $page->getTemplate() ? $page->getTemplate() : $this->defaultPageTemplate;
31
32
        try {
33
            $this->twig->render($template, ['page' => $page]);
34
        } catch (\Exception $exception) {
35
            $this->context->buildViolation($constraint->message)
36
                ->addViolation();
37
        }
38
    }
39
}
40