Completed
Push — master ( a435b5...54b9b0 )
by Guillaume
14:56
created

LeadNotExistValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace Starkerxp\LeadBundle\Validator;
4
5
use Starkerxp\LeadBundle\Entity\Lead;
6
use Starkerxp\LeadBundle\Repository\LeadRepository;
7
use Symfony\Component\Validator\Constraint;
8
use Symfony\Component\Validator\ConstraintValidator;
9
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
10
11
/**
12
 * @Annotation
13
 */
14 View Code Duplication
class LeadNotExistValidator extends ConstraintValidator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var LeadRepository
18
     */
19
    protected $repositoryLead;
20
21
    /**
22
     * NotCreatedSameLeadValidator constructor.
23
     * @param LeadRepository $repositoryLead
24
     */
25
    public function __construct(LeadRepository $repositoryLead)
26
    {
27
        $this->repositoryLead = $repositoryLead;
28
    }
29
30
    public function validate($value, Constraint $constraint)
31
    {
32
        if (!$constraint instanceof LeadNotExist) {
33
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\LeadNotExist');
34
        }
35
        /** @var Lead $lead */
36
        $lead = $this->context->getRoot()->getData();
37
        if (!$this->repositoryLead->leadExist($lead->getOrigin(), $lead->getExternalReference())) {
38
            $this->context->buildViolation($constraint->message)
39
                ->setParameter('{{ origin }}', $lead->getOrigin())
40
                ->setParameter('{{ external_reference }}', $lead->getExternalReference())
41
                ->addViolation();
42
        }
43
    }
44
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
45