Completed
Push — master ( 253917...6bb17d )
by Antoine
08:29
created

CanRenderValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @copyright Interactive Solutions
4
 */
5
6
declare(strict_types=1);
7
8
namespace Roave\EmailTemplates\Validator;
9
10
use Roave\EmailTemplates\Service\Template\Engine\EngineInterface;
11
use Zend\Validator\AbstractValidator;
12
13
final class CanRenderValidator extends AbstractValidator
14
{
15
    const RENDERING_FAILED = 'roave:email-templates:cannot-render-template';
16
17
    /**
18
     * @var array
19
     */
20
    private $messageTemplates = [
21
        self::RENDERING_FAILED => 'Rendering failed: with %value%s',
22
    ];
23
24
    /**
25
     * @var EngineInterface
26
     */
27
    private $engine;
28
29
    public function __construct(EngineInterface $engine)
30
    {
31
        parent::__construct();
32
33
        $this->engine = $engine;
34
    }
35
36
    public function isValid($value, $context = null)
37
    {
38
        try {
39
            $this->engine->render($value, $context['parameters'] ?? []);
40
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
41
42
            $this->error(self::RENDERING_FAILED, $e->getMessage());
43
44
            return false;
45
        }
46
47
        return true;
48
    }
49
}
50