Completed
Push — master ( cd8605...3b7290 )
by
unknown
09:46
created

HasContactInformationValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 3 Features 0
Metric Value
wmc 8
c 3
b 3
f 0
lcom 1
cbo 5
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C validate() 0 34 7
1
<?php
2
3
namespace OroCRM\Bundle\ContactBundle\Validator\Constraints;
4
5
use Symfony\Component\Translation\TranslatorInterface;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
use OroCRM\Bundle\ContactBundle\Entity\Contact;
10
11
class HasContactInformationValidator extends ConstraintValidator
12
{
13
    /** @var TranslatorInterface */
14
    protected $translator;
15
16
    /**
17
     * @param TranslatorInterface $translator
18
     */
19
    public function __construct(TranslatorInterface $translator)
20
    {
21
        $this->translator = $translator;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function validate($value, Constraint $constraint)
28
    {
29
        if (!$value) {
30
            return;
31
        }
32
33
        if (!$value instanceof Contact) {
34
            throw new \InvalidArgumentException(sprintf(
35
                'Validator expects $value to be instance of "%s"',
36
                'OroCRM\Bundle\ContactBundle\Entity\Contact'
37
            ));
38
        }
39
40
        if ($value->getFirstName() ||
41
            $value->getLastName() ||
42
            $value->getEmails()->count() > 0 ||
43
            $value->getPhones()->count() > 0
44
        ) {
45
            return;
46
        }
47
48
        $this->context->addViolation(
49
            $constraint->message,
50
            [
51
                '%fields%' => sprintf(
52
                    '%s, %s, %s or %s',
53
                    $this->translator->trans('orocrm.contact.first_name.label'),
54
                    $this->translator->trans('orocrm.contact.last_name.label'),
55
                    $this->translator->trans('orocrm.contact.emails.label'),
56
                    $this->translator->trans('orocrm.contact.phones.label')
57
                ),
58
            ]
59
        );
60
    }
61
}
62