Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
SignupFormSubmissionListener
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Listeners\Hubspot;
4
5use App\Events\User\Registered;
6use Exception;
7use Illuminate\Contracts\Queue\ShouldQueue;
8use Illuminate\Support\Facades\Config;
9use Illuminate\Support\Facades\Log;
10
11class SignupFormSubmissionListener implements ShouldQueue
12{
13    /**
14     * Handle the event.
15     */
16    public function handle(Registered $event): bool
17    {
18        try {
19            if (config("app.env") == "production") {
20                // Default settings
21                $baseURL = Config::get('hubspotconfig.form_url');
22
23                // Portal ID of client
24                $portalId = Config::get('hubspotconfig.portal_id');
25
26                // Hubspot Form ID (grabbed from URL)
27                $hubspot_form = Config::get('hubspotconfig.signup_form_id');
28
29                $params = '';
30                $params = $params . 'firstname=' . urlencode($event->user->first_name) . '&';
31                $params = $params . 'lastname=' . urlencode($event->user->last_name) . '&';
32                $params = $params . 'email=' . urlencode($event->user->email) . '&';
33
34                $ip_addr = $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? null;
35                $pageUrl = $_SERVER['HTTP_REFERER'] ?? null;
36                $hs_context = [
37                    'hutk' => ':hubspotutk',
38                    'ipAddress' => $ip_addr,
39                    'pageUrl' => $pageUrl,
40                    'pageName' => 'Sign Up - FLYMSG',
41                ];
42
43                $hs_context_json = json_encode($hs_context);
44                $params = $params . '&hs_context=' . urlencode($hs_context_json);
45
46                // Prepare URL
47                $endpoint = $baseURL . $portalId . '/' . $hubspot_form;
48
49                $ch = curl_init();
50                curl_setopt($ch, CURLOPT_POST, true);
51                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
52                curl_setopt($ch, CURLOPT_URL, $endpoint);
53                curl_setopt($ch, CURLOPT_HTTPHEADER, [
54                    'Content-Type: application/x-www-form-urlencoded',
55                ]);
56                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
57                $response = curl_exec($ch); //Log the response from HubSpot as needed.
58                $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
59                curl_close($ch);
60            }
61        } catch (Exception $ex) {
62            Log::error('Error - SignupFormSubmissionListener - ' . $ex->getMessage());
63        } finally {
64            return true;
65        }
66    }
67}