Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
41 / 41 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
DefaultCrmUserContactSubscriber | |
100.00% |
41 / 41 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getSubscribedEvents | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
onCrmUserContactCreate | |
100.00% |
35 / 35 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Drupal\crm\EventSubscriber; |
4 | |
5 | use Drupal\Core\Config\ConfigFactoryInterface; |
6 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
7 | use Drupal\crm\Event\CrmUserContactEvent; |
8 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
9 | |
10 | /** |
11 | * Subscribe to crm user contact create events. |
12 | */ |
13 | class DefaultCrmUserContactSubscriber implements EventSubscriberInterface { |
14 | |
15 | /** |
16 | * The crm contact storage. |
17 | * |
18 | * @var \Drupal\Core\Entity\EntityStorageInterface |
19 | */ |
20 | protected $contactStorage; |
21 | |
22 | /** |
23 | * The crm contact detail storage. |
24 | * |
25 | * @var \Drupal\Core\Entity\EntityStorageInterface |
26 | */ |
27 | protected $contactDetailStorage; |
28 | |
29 | /** |
30 | * The crm user contact settings. |
31 | * |
32 | * @var \Drupal\Core\Config\ImmutableConfig |
33 | */ |
34 | protected $settings; |
35 | |
36 | /** |
37 | * Constructs a new DefaultCrmUserContactSubscriber object. |
38 | * |
39 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
40 | * The entity type manager. |
41 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
42 | * The config factory. |
43 | */ |
44 | public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) { |
45 | $this->settings = $config_factory->get('crm.crm_user_contact.settings'); |
46 | $this->contactStorage = $entity_type_manager->getStorage('crm_contact'); |
47 | $this->contactDetailStorage = $entity_type_manager->getStorage('crm_contact_detail'); |
48 | } |
49 | |
50 | /** |
51 | * {@inheritdoc} |
52 | */ |
53 | public static function getSubscribedEvents() { |
54 | $events = []; |
55 | $events[CrmUserContactEvent::EVENT_NAME] = ['onCrmUserContactCreate', 100]; |
56 | |
57 | return $events; |
58 | } |
59 | |
60 | /** |
61 | * React to a crm user contact create event. |
62 | * |
63 | * @param \Drupal\crm\Event\CrmUserContactEvent $event |
64 | * The crm user contact event. |
65 | */ |
66 | public function onCrmUserContactCreate(CrmUserContactEvent $event) { |
67 | |
68 | $crm_user_contact = $event->getCrmUserContact(); |
69 | $contact = $crm_user_contact->getContact(); |
70 | $lookup = $this->settings->get('auto_create_lookup_contact'); |
71 | $user = $crm_user_contact->getUser(); |
72 | $user_email = $user->getEmail(); |
73 | if ($lookup && $contact == NULL) { |
74 | $contact_detail_emails = $this->contactDetailStorage->getQuery() |
75 | ->condition('email', $user_email) |
76 | ->condition('bundle', 'email') |
77 | ->sort('status', 'DESC') |
78 | ->accessCheck(FALSE) |
79 | ->execute(); |
80 | |
81 | $contact_ids = $this->contactStorage->getQuery() |
82 | ->condition('emails', $contact_detail_emails, 'IN') |
83 | ->condition('bundle', 'person') |
84 | ->sort('status', 'DESC') |
85 | ->accessCheck(FALSE) |
86 | ->execute(); |
87 | |
88 | if ($contact == NULL && count($contact_ids) == 1) { |
89 | $contact = $this->contactStorage->load(reset($contact_ids)); |
90 | } |
91 | |
92 | } |
93 | if ($contact == NULL) { |
94 | $contact = $this->contactStorage |
95 | ->create(['bundle' => 'person']); |
96 | |
97 | $email = $this->contactDetailStorage |
98 | ->create([ |
99 | 'bundle' => 'email', |
100 | 'email' => $user_email, |
101 | 'type' => 'main', |
102 | ]); |
103 | |
104 | $email->save(); |
105 | $contact->set('emails', $email); |
106 | $contact->set('full_name', ['given' => $user->getDisplayName()]); |
107 | |
108 | $contact->save(); |
109 | } |
110 | $crm_user_contact->setContact($contact); |
111 | $event->setCrmUserContact($crm_user_contact); |
112 | } |
113 | |
114 | } |