| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenPanel Driver Implementation |
| 4 |
* |
| 5 |
* @package LinnoSDK\Telemetry |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace LinnoSDK\Telemetry\Drivers; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class OpenPanelDriver |
| 13 |
* |
| 14 |
* Implements DriverInterface for OpenPanel analytics platform. |
| 15 |
* Handles HTTPS communication, authentication, and error handling. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
class OpenPanelDriver implements DriverInterface { |
| 20 |
/** |
| 21 |
* OpenPanel API endpoint URL |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
private const API_ENDPOINT = 'https://analytics.linno.io/api/track'; |
| 26 |
|
| 27 |
/** |
| 28 |
* API key for authentication |
| 29 |
* |
| 30 |
* @var string |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
private string $apiKey; |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* API Secret for authentication |
| 38 |
* |
| 39 |
* @var string |
| 40 |
* @since 1.0.0 |
| 41 |
*/ |
| 42 |
private string $apiSecret; |
| 43 |
|
| 44 |
/** |
| 45 |
* Last error message |
| 46 |
* |
| 47 |
* @var string|null |
| 48 |
* @since 1.0.0 |
| 49 |
*/ |
| 50 |
private ?string $lastError = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* Send event data to OpenPanel |
| 54 |
* |
| 55 |
* @param string $event The event name. |
| 56 |
* @param array $properties The event properties. |
| 57 |
* |
| 58 |
* @return bool True on success, false on failure. |
| 59 |
* @since 1.0.0 |
| 60 |
*/ |
| 61 |
public function send( string $event, array $properties ): bool { |
| 62 |
$this->lastError = null; |
| 63 |
|
| 64 |
$payload = array( |
| 65 |
'event' => $event, |
| 66 |
'properties' => $properties, |
| 67 |
); |
| 68 |
|
| 69 |
return $this->makeRequest( $payload ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Set the API key for authentication |
| 74 |
* |
| 75 |
* @param string $apiKey The API key. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
* @since 1.0.0 |
| 79 |
*/ |
| 80 |
public function setApiKey( string $apiKey ): void { |
| 81 |
$this->apiKey = $apiKey; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
public function setApiSecret( string $apiSecret ): void { |
| 86 |
$this->apiSecret = $apiSecret; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get the last error message |
| 91 |
* |
| 92 |
* @return string|null The last error message or null if no error. |
| 93 |
* @since 1.0.0 |
| 94 |
*/ |
| 95 |
public function getLastError(): ?string { |
| 96 |
return $this->lastError; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Build HTTP headers for the request |
| 101 |
* |
| 102 |
* @return array The headers array. |
| 103 |
* @since 1.0.0 |
| 104 |
*/ |
| 105 |
private function buildHeaders(): array { |
| 106 |
return array( |
| 107 |
'openpanel-client-id' => $this->apiKey, |
| 108 |
'openpanel-client-secret' => $this->apiSecret, |
| 109 |
'Content-Type' => 'application/json', |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Make HTTPS request to OpenPanel API using cURL |
| 115 |
* |
| 116 |
* @param array $payload The payload to send. |
| 117 |
* |
| 118 |
* @return bool True on success, false on failure. |
| 119 |
* @since 1.0.0 |
| 120 |
*/ |
| 121 |
private function makeRequest( array $payload ): bool { |
| 122 |
$ch = curl_init( self::API_ENDPOINT ); |
| 123 |
$body = json_encode( array( |
| 124 |
'type' => 'track', |
| 125 |
'payload' => array( |
| 126 |
'name' => $payload['event'], |
| 127 |
'properties' => $payload['properties'], |
| 128 |
), |
| 129 |
), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
| 130 |
|
| 131 |
// Build headers for cURL |
| 132 |
$headers = array(); |
| 133 |
foreach ( $this->buildHeaders() as $key => $value ) { |
| 134 |
$headers[] = "$key: $value"; |
| 135 |
} |
| 136 |
|
| 137 |
// Set cURL options to match successful Postman request |
| 138 |
curl_setopt_array( $ch, array( |
| 139 |
CURLOPT_RETURNTRANSFER => true, |
| 140 |
CURLOPT_POST => true, |
| 141 |
CURLOPT_POSTFIELDS => $body, |
| 142 |
CURLOPT_HTTPHEADER => $headers, |
| 143 |
CURLOPT_TIMEOUT => 30, |
| 144 |
CURLOPT_CONNECTTIMEOUT => 10, |
| 145 |
CURLOPT_SSL_VERIFYPEER => true, |
| 146 |
CURLOPT_SSL_VERIFYHOST => 2, |
| 147 |
CURLOPT_FOLLOWLOCATION => true, |
| 148 |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
| 149 |
) ); |
| 150 |
|
| 151 |
// Execute the request |
| 152 |
$response = curl_exec( $ch ); |
| 153 |
$httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); |
| 154 |
$error = curl_error( $ch ); |
| 155 |
$errno = curl_errno( $ch ); |
| 156 |
|
| 157 |
curl_close( $ch ); |
| 158 |
|
| 159 |
return $this->handleResponse( $response, $httpCode, $error, $errno ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Handle the API response from cURL |
| 164 |
* |
| 165 |
* @param mixed $response The response body from cURL. |
| 166 |
* @param int $httpCode The HTTP status code. |
| 167 |
* @param string $error The cURL error message. |
| 168 |
* @param int $errno The cURL error number. |
| 169 |
* |
| 170 |
* @return bool True on success, false on failure. |
| 171 |
* @since 1.0.0 |
| 172 |
*/ |
| 173 |
private function handleResponse( $response, int $httpCode, string $error, int $errno ): bool { |
| 174 |
// Check for cURL errors |
| 175 |
if ( $errno !== 0 ) { |
| 176 |
$this->lastError = sprintf( 'cURL error (%d): %s', $errno, $error ); |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
// Check HTTP status code |
| 181 |
if ( $httpCode < 200 || $httpCode >= 300 ) { |
| 182 |
$this->lastError = sprintf( |
| 183 |
'HTTP %d: %s', |
| 184 |
$httpCode, |
| 185 |
$response ? $response : 'Unknown error' |
| 186 |
); |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
return true; |
| 191 |
} |
| 192 |
} |