/
ContactHubspotEventHandler.php
87 lines (77 loc) · 2.63 KB
/
ContactHubspotEventHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
declare(strict_types=1);
/*
* This file is part of the RunroomSamplesBundle.
*
* (c) Runroom <runroom@runroom.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Runroom\SamplesBundle\Forms\Form;
use Runroom\FormHandlerBundle\ViewModel\FormAwareInterface;
use SevenShores\Hubspot\Resources\Forms;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Contracts\Translation\TranslatorInterface;
final class ContactHubspotEventHandler implements EventSubscriberInterface
{
private TranslatorInterface $translator;
private Forms $hubspotForms;
private int $portalId;
private string $formId;
public function __construct(
TranslatorInterface $translator,
Forms $hubspotForms,
int $portalId,
string $formId
) {
$this->translator = $translator;
$this->hubspotForms = $hubspotForms;
$this->portalId = $portalId;
$this->formId = $formId;
}
/**
* @phpstan-param GenericEvent<FormAwareInterface> $event
* @psalm-param GenericEvent $event
*/
public function onContactSuccess(GenericEvent $event): void
{
$model = $event->getSubject()->getForm()->getData();
$this->sendHubspot([
'fields' => [
[
'name' => 'firstname',
'value' => $model->getName(),
],
[
'name' => 'phone',
'value' => $model->getPhone(),
],
[
'name' => 'email',
'value' => $model->getEmail(),
],
[
'name' => 'comentario',
'value' => $model->getComment(),
],
],
'legalConsentOptions' => [
'consent' => [
'text' => $this->translator->trans('form.privacy_policy', [], 'messages', 'es'),
'consentToProcess' => $model->getPrivacyPolicy(),
],
],
]);
}
public static function getSubscribedEvents(): array
{
return ['form.contact_hubspot_form.event.success' => 'onContactSuccess'];
}
/** @param array{ fields: array{name: string, value: mixed}[], legalConsentOptions: array{consent: array{consentToProcess: mixed, text: string} } } $data */
private function sendHubspot(array $data): void
{
$this->hubspotForms->submit($this->portalId, $this->formId, $data);
}
}