Completed
Pull Request — master (#34)
by
unknown
02:45
created

ConstraintValidatorBuilder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 96
wmc 9
lcom 1
cbo 5
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B buildDefinitions() 0 26 3
A getName() 0 4 1
A isActive() 0 4 1
A buildAlias() 0 15 3
1
<?php
2
3
namespace Knp\Rad\AutoRegistration\DefinitionBuilder;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use Knp\Rad\AutoRegistration\DefinitionBuilder;
7
use Knp\Rad\AutoRegistration\Finder\BundleFinder;
8
use Knp\Rad\AutoRegistration\Kernel\KernelWrapper;
9
use Knp\Rad\AutoRegistration\Reflection\ClassAnalyzer;
10
use Symfony\Component\DependencyInjection\Definition;
11
12
class ConstraintValidatorBuilder implements DefinitionBuilder
13
{
14
    /**
15
     * @var KernelWrapper
16
     */
17
    private $kernel;
18
19
    /**
20
     * @var BundleFinder
21
     */
22
    private $finder;
23
24
    /**
25
     * @var ClassAnalyzer
26
     */
27
    private $analyzer;
28
29
    /**
30
     * @param KernelWrapper $kernel
31
     * @param BundleFinder  $finder
32
     * @param ClassAnalyzer $analyzer
33
     */
34
    public function __construct(KernelWrapper $kernel, BundleFinder $finder, ClassAnalyzer $analyzer)
35
    {
36
        $this->kernel   = $kernel;
37
        $this->finder   = $finder;
38
        $this->analyzer = $analyzer;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function buildDefinitions(array $config)
45
    {
46
        $config = array_merge(['public' => false], $config);
47
48
        $definitions = [];
49
50
        $validators = $this->finder->findClasses(
51
            $this->kernel->getBundles(),
52
            ['Validator', 'Validation'],
53
            'Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface'
54
        );
55
56
        foreach ($validators as $validator) {
57
            if (false === $this->analyzer->canBeConstructed($validator)) {
58
                continue;
59
            }
60
61
            $definitions[$validator] = (new Definition())
62
                ->setClass($validator)
63
                ->setPublic($config['public'])
64
                ->addTag('validator.constraint_validator', ['alias_name' => $this->buildAlias($validator)])
65
            ;
66
        }
67
68
        return $definitions;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getName()
75
    {
76
        return 'constraint_validator';
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function isActive()
83
    {
84
        return true;
85
    }
86
87
    /**
88
     * @param string $class
89
     *
90
     * @return string
91
     */
92
    private function buildAlias($class)
93
    {
94
        $parts     = explode('\\', $class);
95
        $shortName = end($parts);
96
97
        if (false === strpos($shortName, 'Validator')) {
98
            return Inflector::tableize($shortName);
99
        }
100
101
        if ('Validator' !== substr($shortName, -9)) {
102
            return Inflector::tableize($shortName);
103
        }
104
105
        return Inflector::tableize(substr($shortName, 0, -9));
106
    }
107
}
108