ExistingServiceValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 14 3
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