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

HasContactInformationValidator::validate()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 0
loc 34
rs 6.7272
cc 7
eloc 20
nc 4
nop 2
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