ValidatorFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.351

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 17
ccs 5
cts 9
cp 0.5556
crap 2.351
rs 9.4285
c 0
b 0
f 0
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
final class ValidatorFactory
12
{
13
    private const VALIDATOR_CACHE_DIRECTORY = 'validator';
14
15 1
    public function create(bool $enableCache, \SplFileInfo $cacheDir): ValidatorInterface
16
    {
17 1
        $validationBuilder = Validation::createValidatorBuilder();
18
19 1
        if (!$enableCache) {
20
            return $validationBuilder
21 1
                ->enableAnnotationMapping()
22 1
                ->getValidator();
23
        }
24
25
        $cache = new FilesystemCache($cacheDir->getPathname() . 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