Test Failed
Branch v0.2 (3f77aa)
by Freddie
07:42 queued 01:11
created

ValidatorFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 17 2
1
<?php declare(strict_types=1);
2
3
namespace Simplex\Quickstart\Shared\Factory;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\CachedReader;
7
use Doctrine\Common\Cache\FilesystemCache;
8
use Symfony\Component\Validator\Validation;
9
use Symfony\Component\Validator\Validator\ValidatorInterface;
10
11
class ValidatorFactory
12
{
13
    const VALIDATOR_CACHE_DIRECTORY = 'validator';
14
15
    public function create(bool $enableCache, string $cacheDir): ValidatorInterface
16
    {
17
        $validationBuilder = Validation::createValidatorBuilder();
18
19
        if (!$enableCache) {
20
            return $validationBuilder
21
                ->enableAnnotationMapping()
22
                ->getValidator();
23
        }
24
25
        $cache = new FilesystemCache($cacheDir . DIRECTORY_SEPARATOR . self::VALIDATOR_CACHE_DIRECTORY);
26
27
        $annotationReader = new CachedReader(new AnnotationReader(), $cache);
28
29
        return $validationBuilder
30
            ->enableAnnotationMapping($annotationReader)
31
            ->getValidator();
32
    }
33
}
34