Failed Conditions
Push — experimental/3.1 ( 47a5a1...b65b3f )
by Yangsin
141:46 queued 135:28
created

TwigLintValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Eccube\Form\Validator;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
use Twig\Error\Error;
8
use Twig\Loader\ArrayLoader;
9
use Twig\Source;
10
11
class TwigLintValidator extends ConstraintValidator
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
12
{
13
    /**
14
     * @var \Twig_Environment
15
     */
16
    protected $twig;
17
18
    /**
19
     * TwigLintValidator constructor.
20
     *
21
     * @param \Twig_Environment $twig
22
     */
23 17
    public function __construct(\Twig_Environment $twig)
24
    {
25 17
        $this->twig = $twig;
26
    }
27
28
    /**
29
     * @param mixed $value
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
30
     * @param Constraint $constraint
31
     */
32 17
    public function validate($value, Constraint $constraint)
33
    {
34
        // valueがnullの場合は "Template is not defined"のエラーが投げられるので, 空文字でチェックする.
35 17
        if (is_null($value)) {
36 1
            $value = '';
37
        }
38
39 17
        $realLoader = $this->twig->getLoader();
40
        try {
41 17
            $temporaryLoader = new ArrayLoader(['' => $value]);
42 17
            $this->twig->setLoader($temporaryLoader);
43 17
            $nodeTree = $this->twig->parse($this->twig->tokenize(new Source($value, '')));
44 16
            $this->twig->compile($nodeTree);
45
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
46 1
        } catch (Error $e) {
47 1
            $this->context->buildViolation($constraint->message)
48 1
                ->setParameter('{{ error }}', $e->getMessage())
49 1
                ->addViolation();
50
        }
51 17
        $this->twig->setLoader($realLoader);
52
    }
53
}
54