Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.47% |
26 / 34 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| RelationshipContactsConstraintValidator | |
76.47% |
26 / 34 |
|
0.00% |
0 / 1 |
20.76 | |
0.00% |
0 / 1 |
| validate | |
76.47% |
26 / 34 |
|
0.00% |
0 / 1 |
20.76 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Drupal\crm\Plugin\Validation\Constraint; |
| 4 | |
| 5 | use Symfony\Component\Validator\Constraint; |
| 6 | use Symfony\Component\Validator\ConstraintValidator; |
| 7 | |
| 8 | /** |
| 9 | * Validates that Contact A and Contact B are different. |
| 10 | */ |
| 11 | class RelationshipContactsConstraintValidator extends ConstraintValidator { |
| 12 | |
| 13 | /** |
| 14 | * Validates the entity. |
| 15 | * |
| 16 | * @param \Drupal\Core\Entity\EntityInterface $entity |
| 17 | * The entity being validated. |
| 18 | * @param \Drupal\crm\Plugin\Validation\Constraint\RelationshipContactsConstraint $constraint |
| 19 | * The constraint to validate against. |
| 20 | */ |
| 21 | public function validate($entity, Constraint $constraint) { |
| 22 | $contact_a = $entity->get('contacts')->referencedEntities()[0] ?? NULL; |
| 23 | $contact_b = $entity->get('contacts')->referencedEntities()[1] ?? NULL; |
| 24 | |
| 25 | $contact_a_target_id = $contact_a ? $contact_a->id() : NULL; |
| 26 | $contact_b_target_id = $contact_b ? $contact_b->id() : NULL; |
| 27 | |
| 28 | if ($contact_a_target_id === $contact_b_target_id) { |
| 29 | $this->context->addViolation($constraint->differentMessage); |
| 30 | } |
| 31 | |
| 32 | $relationship_type = $entity->get('bundle')->entity ?? NULL; |
| 33 | $expected_contact_a_type = $relationship_type ? $relationship_type->get('contact_type_a') : []; |
| 34 | $expected_contact_b_type = $relationship_type ? $relationship_type->get('contact_type_b') : []; |
| 35 | |
| 36 | $contact_a_type = $contact_a ? $contact_a->bundle() : NULL; |
| 37 | $contact_b_type = $contact_b ? $contact_b->bundle() : NULL; |
| 38 | |
| 39 | if (!in_array($contact_a_type, $expected_contact_a_type)) { |
| 40 | $this->context->buildViolation($constraint->wrongTypeMessage) |
| 41 | ->atPath('contact_a') |
| 42 | ->setParameter('@type', implode(', ', $expected_contact_a_type)) |
| 43 | ->addViolation(); |
| 44 | } |
| 45 | |
| 46 | if (!in_array($contact_b_type, $expected_contact_b_type)) { |
| 47 | $this->context->buildViolation($constraint->wrongTypeMessage) |
| 48 | ->atPath('contact_b') |
| 49 | ->setParameter('@type', implode(', ', $expected_contact_b_type)) |
| 50 | ->addViolation(); |
| 51 | } |
| 52 | |
| 53 | // Validate valid contacts constraints. |
| 54 | if ($relationship_type) { |
| 55 | $valid_contacts_a = $relationship_type->getValidContactsA(); |
| 56 | $valid_contacts_b = $relationship_type->getValidContactsB(); |
| 57 | |
| 58 | // Check Contact A is in valid contacts list (if configured). |
| 59 | if (!empty($valid_contacts_a) && $contact_a_target_id !== NULL) { |
| 60 | if (!in_array((int) $contact_a_target_id, array_map('intval', $valid_contacts_a))) { |
| 61 | $this->context->buildViolation($constraint->invalidContactMessage) |
| 62 | ->atPath('contact_a') |
| 63 | ->addViolation(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Check Contact B is in valid contacts list (if configured). |
| 68 | if (!empty($valid_contacts_b) && $contact_b_target_id !== NULL) { |
| 69 | if (!in_array((int) $contact_b_target_id, array_map('intval', $valid_contacts_b))) { |
| 70 | $this->context->buildViolation($constraint->invalidContactMessage) |
| 71 | ->atPath('contact_b') |
| 72 | ->addViolation(); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } |