Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       21 / 21  | 
               | 
       100.00%  | 
       1 / 1  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| RelationshipContactsConstraintValidator |         | 
       100.00%  | 
       21 / 21  | 
               | 
       100.00%  | 
       1 / 1  | 
       10 |         | 
       100.00%  | 
       1 / 1  | 
      
| validate |         | 
       100.00%  | 
       21 / 21  | 
               | 
       100.00%  | 
       1 / 1  | 
       10 | |||
| 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') : NULL; | 
| 34 | $expected_contact_b_type = $relationship_type ? $relationship_type->get('contact_type_b') : NULL; | 
| 35 | |
| 36 | $contact_a_type = $contact_a ? $contact_a->bundle() : NULL; | 
| 37 | $contact_b_type = $contact_b ? $contact_b->bundle() : NULL; | 
| 38 | |
| 39 | if ($expected_contact_a_type !== $contact_a_type) { | 
| 40 | $this->context->buildViolation($constraint->wrongTypeMessage) | 
| 41 | ->atPath('contact_a') | 
| 42 | ->setParameter('@type', $expected_contact_a_type) | 
| 43 | ->addViolation(); | 
| 44 | } | 
| 45 | |
| 46 | if ($expected_contact_b_type !== $contact_b_type) { | 
| 47 | $this->context->buildViolation($constraint->wrongTypeMessage) | 
| 48 | ->atPath('contact_b') | 
| 49 | ->setParameter('@type', $expected_contact_b_type) | 
| 50 | ->addViolation(); | 
| 51 | } | 
| 52 | } | 
| 53 | |
| 54 | } |