Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RelationshipService
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 3
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getRelationshipIdByContactId
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getEligibleRelationshipTypesForContact
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
240
1<?php
2
3namespace Drupal\crm\Service;
4
5use Drupal\Core\Entity\EntityTypeManagerInterface;
6use Drupal\crm\Entity\ContactInterface;
7
8/**
9 * Service description.
10 */
11class RelationshipService {
12
13  /**
14   * The entity type manager.
15   *
16   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
17   */
18  protected $entityTypeManager;
19
20
21  /**
22   * The relationship storage.
23   *
24   * @var \Drupal\Core\Entity\EntityStorageInterface
25   */
26  protected $relationshipStorage;
27
28  /**
29   * Constructs a Relationship object.
30   *
31   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
32   *   The entity type manager.
33   */
34  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
35    $this->entityTypeManager = $entity_type_manager;
36    $this->relationshipStorage = $entity_type_manager
37      ->getStorage('crm_relationship');
38  }
39
40  /**
41   * Method description.
42   */
43  public function getRelationshipIdByContactId(int $contact_id) {
44    $query = $this->relationshipStorage->getQuery();
45    $ids = $query
46      ->condition('status', 1)
47      ->condition('contacts', $contact_id)
48      ->accessCheck(TRUE)
49      ->execute();
50
51    return $ids;
52  }
53
54  /**
55   * Gets eligible relationship types for a contact.
56   *
57   * Filters relationship types based on the contact's bundle matching
58   * the allowed contact types for each position (A or B), and optionally
59   * the valid_contacts restrictions if configured.
60   *
61   * @param \Drupal\crm\ContactInterface $contact
62   *   The contact entity to check eligibility for.
63   *
64   * @return array
65   *   An array of eligible relationship types, keyed by type ID, containing:
66   *   - type: The relationship type entity.
67   *   - positions: Array of positions ('a', 'b') the contact can occupy.
68   */
69  public function getEligibleRelationshipTypesForContact(ContactInterface $contact): array {
70    $contact_type = $contact->bundle();
71    $contact_id = (int) $contact->id();
72    $eligible = [];
73
74    $relationship_types = $this->entityTypeManager
75      ->getStorage('crm_relationship_type')
76      ->loadMultiple();
77
78    foreach ($relationship_types as $type) {
79      $positions = [];
80
81      // Check position A eligibility.
82      $contact_type_a = $type->get('contact_type_a') ?? [];
83      if (!is_array($contact_type_a)) {
84        $contact_type_a = $contact_type_a !== NULL && $contact_type_a !== '' ? [$contact_type_a] : [];
85      }
86      if (in_array($contact_type, $contact_type_a, TRUE)) {
87        // Check valid_contacts_a restriction if configured.
88        $valid_contacts_a = $type->getValidContactsA();
89        if (empty($valid_contacts_a) || in_array($contact_id, array_map('intval', $valid_contacts_a), TRUE)) {
90          $positions[] = 'a';
91        }
92      }
93
94      // Check position B eligibility.
95      $contact_type_b = $type->get('contact_type_b') ?? [];
96      if (!is_array($contact_type_b)) {
97        $contact_type_b = $contact_type_b !== NULL && $contact_type_b !== '' ? [$contact_type_b] : [];
98      }
99      if (in_array($contact_type, $contact_type_b, TRUE)) {
100        // Check valid_contacts_b restriction if configured.
101        $valid_contacts_b = $type->getValidContactsB();
102        if (empty($valid_contacts_b) || in_array($contact_id, array_map('intval', $valid_contacts_b), TRUE)) {
103          $positions[] = 'b';
104        }
105      }
106
107      if (!empty($positions)) {
108        $eligible[$type->id()] = [
109          'type' => $type,
110          'positions' => $positions,
111        ];
112      }
113    }
114
115    return $eligible;
116  }
117
118}