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

ConstraintValidatorBuilder::buildAlias()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
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()
45
    {
46
        $definitions = [];
47
48
        $validators = $this->finder->findClasses(
49
            $this->kernel->getBundles(),
50
            ['Validator', 'Validation'],
51
            'Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface'
52
        );
53
54
        foreach ($validators as $validator) {
55
            if (false === $this->analyzer->canBeConstructed($validator)) {
56
                continue;
57
            }
58
59
            $definitions[$validator] = (new Definition())
60
                ->setClass($validator)
61
                ->setPublic(false)
62
                ->addTag('validator.constraint_validator', ['alias_name' => $this->buildAlias($validator)])
63
            ;
64
        }
65
66
        return $definitions;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getName()
73
    {
74
        return 'constraint_validator';
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function isActive()
81
    {
82
        return true;
83
    }
84
85
    /**
86
     * @param string $class
87
     *
88
     * @return string
89
     */
90
    private function buildAlias($class)
91
    {
92
        $parts     = explode('\\', $class);
93
        $shortName = end($parts);
94
95
        if (false === strpos($shortName, 'Validator')) {
96
            return Inflector::tableize($shortName);
97
        }
98
99
        if ('Validator' !== substr($shortName, -9)) {
100
            return Inflector::tableize($shortName);
101
        }
102
103
        return Inflector::tableize(substr($shortName, 0, -9));
104
    }
105
}
106