ExistingServiceValidator::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Storeman\Validation\Constraints;
4
5
use Psr\Container\ContainerInterface;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
class ExistingServiceValidator extends ConstraintValidator
10
{
11
    /**
12
     * @var ContainerInterface
13
     */
14
    protected $container;
15
16
    public function __construct(ContainerInterface $container)
17
    {
18
        $this->container = $container;
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function validate($value, Constraint $constraint)
25
    {
26
        if (!($constraint instanceof ServiceExists))
27
        {
28
            throw new \LogicException();
29
        }
30
31
        if (!$this->container->has("{$constraint->getPrefix()}{$value}"))
32
        {
33
            $this->context->buildViolation($constraint->getMessage())
34
                ->setParameter('{{ value }}', $value)
35
                ->addViolation();
36
        }
37
    }
38
}
39