Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
21 / 24
80.00% covered (warning)
80.00%
8 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
RelationshipType
87.50% covered (warning)
87.50%
21 / 24
80.00% covered (warning)
80.00%
8 / 10
15.44
0.00% covered (danger)
0.00%
0 / 1
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDescription
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 isLocked
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 calculateDependencies
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 getLimitA
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLimitB
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isLimitActiveOnly
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValidContactsA
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValidContactsB
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Drupal\crm\Entity;
4
5use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
6use Drupal\Core\Entity\Attribute\ConfigEntityType;
7use Drupal\Core\Entity\EntityDeleteForm;
8use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
9use Drupal\Core\StringTranslation\TranslatableMarkup;
10use Drupal\crm\RelationshipTypeListBuilder;
11use Drupal\crm\Form\RelationshipTypeForm;
12
13use Drupal\crm\CrmRelationshipTypeInterface;
14
15/**
16 * Defines the CRM Relationship type configuration entity.
17 */
18#[ConfigEntityType(
19  id: 'crm_relationship_type',
20  label: new TranslatableMarkup('CRM Relationship type'),
21  label_collection: new TranslatableMarkup('CRM Relationship types'),
22  label_singular: new TranslatableMarkup('crm relationship type'),
23  label_plural: new TranslatableMarkup('crm relationships types'),
24  label_count: [
25    'singular' => '@count crm relationships type',
26    'plural' => '@count crm relationships types',
27  ],
28  handlers: [
29    'form' => [
30      'add' => RelationshipTypeForm::class,
31      'edit' => RelationshipTypeForm::class,
32      'delete' => EntityDeleteForm::class,
33    ],
34    'list_builder' => RelationshipTypeListBuilder::class,
35    'route_provider' => [
36      'html' => AdminHtmlRouteProvider::class,
37    ],
38  ],
39  admin_permission: 'administer crm',
40  bundle_of: 'crm_relationship',
41  config_prefix: 'crm_relationship_type',
42  entity_keys: [
43    'id' => 'id',
44    'label' => 'label',
45    'uuid' => 'uuid',
46  ],
47  links: [
48    'add-form' => '/admin/structure/crm/relationship_types/add',
49    'edit-form' => '/admin/structure/crm/relationship_types/manage/{crm_relationship_type}',
50    'delete-form' => '/admin/structure/crm/relationship_types/manage/{crm_relationship_type}/delete',
51    'collection' => '/admin/structure/crm/relationship_types',
52  ],
53  config_export: [
54    'id',
55    'label',
56    'description',
57    'asymmetric',
58    'uuid',
59    'contact_type_a',
60    'contact_type_b',
61    'label_a',
62    'label_b',
63    'limit_a',
64    'limit_b',
65    'limit_active_only',
66    'valid_contacts_a',
67    'valid_contacts_b',
68  ],
69)]
70class RelationshipType extends ConfigEntityBundleBase implements CrmRelationshipTypeInterface {
71
72  /**
73   * The machine name of this crm relationship type.
74   *
75   * @var string
76   */
77  protected $id;
78
79  /**
80   * The human-readable name of the crm relationship type.
81   *
82   * @var string
83   */
84  protected $label;
85
86  /**
87   * The description of the crm relationship type.
88   *
89   * @var string
90   */
91  protected $description;
92
93  /**
94   * Is the bundle and label the same for both sides of the relationship?
95   *
96   * @var bool
97   */
98  protected $asymmetric;
99
100  /**
101   * The human-readable name of the crm relationship type.
102   *
103   * @var string
104   */
105  protected $label_a;
106
107  /**
108   * The human-readable name of the crm relationship type.
109   *
110   * @var string
111   */
112  protected $label_b;
113
114  /**
115   * The contact types for the first contact in the relationship.
116   *
117   * @var array
118   */
119  protected $contact_type_a;
120
121  /**
122   * The contact types for the second contact in the relationship.
123   *
124   * @var array
125   */
126  protected $contact_type_b;
127
128  /**
129   * Maximum number of relationships where a contact can be in position A.
130   *
131   * @var int|null
132   */
133  protected $limit_a;
134
135  /**
136   * Maximum number of relationships where a contact can be in position B.
137   *
138   * @var int|null
139   */
140  protected $limit_b;
141
142  /**
143   * Whether to only count active relationships toward the limit.
144   *
145   * @var bool
146   */
147  protected $limit_active_only = FALSE;
148
149  /**
150   * Valid contact IDs for the first contact in the relationship.
151   *
152   * @var array
153   */
154  protected $valid_contacts_a = [];
155
156  /**
157   * Valid contact IDs for the second contact in the relationship.
158   *
159   * @var array
160   */
161  protected $valid_contacts_b = [];
162
163  /**
164   * {@inheritdoc}
165   */
166  public function getDescription() {
167    return $this->description ?? '';
168  }
169
170  /**
171   * {@inheritdoc}
172   */
173  public function setDescription($description) {
174    $this->description = $description;
175
176    return $this;
177  }
178
179  /**
180   * Is the relationship type locked?
181   */
182  public function isLocked() {
183    $locked = \Drupal::state()->get('crm.relationship_type.locked');
184    return $locked[$this->id()] ?? FALSE;
185  }
186
187  /**
188   * {@inheritdoc}
189   */
190  public function calculateDependencies() {
191    parent::calculateDependencies();
192
193    if ($type_a = $this->get('contact_type_a')) {
194      foreach ($type_a as $type) {
195        $this->addDependency('config', 'crm.crm_contact_type.' . $type);
196      }
197    }
198    if ($type_b = $this->get('contact_type_b')) {
199      foreach ($type_b as $type) {
200        $this->addDependency('config', 'crm.crm_contact_type.' . $type);
201      }
202    }
203
204    return $this;
205  }
206
207  /**
208   * {@inheritdoc}
209   */
210  public function getLimitA(): ?int {
211    return $this->limit_a;
212  }
213
214  /**
215   * {@inheritdoc}
216   */
217  public function getLimitB(): ?int {
218    return $this->limit_b;
219  }
220
221  /**
222   * {@inheritdoc}
223   */
224  public function isLimitActiveOnly(): bool {
225    return (bool) $this->limit_active_only;
226  }
227
228  /**
229   * {@inheritdoc}
230   */
231  public function getValidContactsA(): array {
232    return $this->valid_contacts_a ?? [];
233  }
234
235  /**
236   * {@inheritdoc}
237   */
238  public function getValidContactsB(): array {
239    return $this->valid_contacts_b ?? [];
240  }
241
242  /**
243   * {@inheritdoc}
244   */
245  public function save() {
246    if (!$this->get('asymmetric')) {
247      $this->set('label_b', $this->get('label_a'));
248      $this->set('contact_type_b', $this->get('contact_type_a'));
249      $this->set('limit_b', $this->get('limit_a'));
250      $this->set('valid_contacts_b', $this->get('valid_contacts_a'));
251    }
252
253    return parent::save();
254  }
255
256}