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

CanRenderValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 37
ccs 0
cts 10
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isValid() 0 13 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