Completed
Push — master ( 8b0d56...e2f78c )
by Arne
01:57
created

ContainerConstraintValidatorFactory::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Storeman\Validation;
4
5
use Psr\Container\ContainerInterface;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidatorFactory;
8
9
/**
10
 * Allows dependency injection for constraint validators.
11
 */
12
class ContainerConstraintValidatorFactory extends ConstraintValidatorFactory
13
{
14
    /**
15
     * @var ContainerInterface
16
     */
17
    protected $container;
18
19
    public function __construct(ContainerInterface $container)
20
    {
21
        $this->container = $container;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getInstance(Constraint $constraint)
28
    {
29
        if ($this->container->has($constraint->validatedBy()))
30
        {
31
            return $this->container->get($constraint->validatedBy());
32
        }
33
34
        return parent::getInstance($constraint);
35
    }
36
}
37