Files
api/services/nginx/app/classes/motorapi.php
T
Jepp9350 8702b41777 Add MotorAPI integration and Economic products support
Implemented MotorAPI lookup functionality with proper validation and logging. Added Economic products endpoint for fetching product details. Enhanced route parameter validation with length constraints and improved response handling for objects.
2025-02-18 12:34:26 +01:00

217 lines
6.6 KiB
PHP

<?php
namespace classes;
require_once WD . '/modules/motorapi/motorapi_c.php';
use Exception;
use interfaces\motorapi_i;
use motorapi\motorapi_c;
use objects\motorapi_lookups_o;
class motorapi implements motorapi_i
{
/**
* Configuration of the motorapi module
* @var motorapi_c
*/
public motorapi_c $config;
/**
* API URL
* @var string
*/
private string $api_url = 'https://v1.motorapi.dk/';
public function __construct()
{
$this->config = new motorapi_c();
}
/**
* @inheritDoc
* @throws Exception If the module is not enabled, the license plate is invalid, the daily limit is exceeded, or the secret key is invalid
*/
function getLicensePlateInformation(string $licensePlate): object
{
// Get the license plate information from the motorapi
return $this->sendRequest($licensePlate, 'vehicles', [], 'GET');
}
/**
* @inheritDoc
* @throws Exception If the module is not enabled, the license plate is invalid, the daily limit is exceeded, or the secret key is invalid
*/
function sendRequest(string $licensePlate, string $endpoint, array $data = [], string $method = 'GET'): object
{
// Validate the module is enabled
self::requireModuleEnabled();
// Validate the license plate
self::requireValidLicensePlate($licensePlate);
// Validate the daily limit
self::requireDailyLimitNotExceeded();
// Validate the secret key
self::requireValidSecretKey();
// Send the request
$response = match ($method) {
'GET' => self::sendGetRequest($licensePlate, $endpoint, $data),
'POST' => self::sendPostRequest($licensePlate, $endpoint, $data),
'PUT' => self::sendPutRequest($licensePlate, $endpoint, $data),
'DELETE' => self::sendDeleteRequest($licensePlate, $endpoint, $data),
default => throw new Exception('Invalid method'),
};
// Add the request to the log
self::addRequestToLog($licensePlate, $endpoint, $response);
// Return the response
return $response;
}
/**
* @inheritDoc
*/
function requireModuleEnabled(): void
{
// Check if the module is enabled
if (!$this->config->enabled->isTrue()) {
throw new Exception('The motorapi module is not enabled');
}
}
/**
* @inheritDoc
*/
function requireValidLicensePlate(string $licensePlate): void
{
// Check if the license plate is valid
if (!preg_match('/^[A-Z0-9]{1,10}$/', $licensePlate)) {
throw new Exception('Invalid license plate');
}
}
/**
* @inheritDoc
*/
function requireDailyLimitNotExceeded(): void
{
// Check if the daily limit is exceeded
if ($this->getDailyRequestCounter() >= $this->config->daily_limit->getVariableValue()) {
throw new Exception('Daily limit exceeded');
}
}
/**
* @inheritDoc
*/
function getDailyRequestCounter(): int
{
// Count the rows from the motorapi request log that was made today
$motorapi_lookups = new motorapi_lookups_o();
$motorapi_lookups->getTodayCount();
return $motorapi_lookups->getTodayCount();
}
/**
* @inheritDoc
*/
function requireValidSecretKey(): void
{
// Check if the secret key is valid
if ($this->config->secret_key->getVariableValue() === null) {
throw new Exception('Invalid secret key');
}
}
/**
* @inheritDoc
* @throws Exception
*/
function sendGetRequest(string $licensePlate, string $endpoint, array $data): object
{
// Build the query string if any data is provided
$query_string = '';
if (!empty($data)) {
$query_string = '?' . http_build_query($data);
}
// Initialize cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->api_url . $endpoint . '/' . $licensePlate . $query_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set HTTP headers including the X-AUTH-TOKEN
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-AUTH-TOKEN: ' . $this->config->secret_key->getVariableValue(),
]);
// Execute the request and handle response
$output = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
throw new Exception('cURL error: ' . curl_error($ch));
}
curl_close($ch);
// Decode and return JSON response
return json_decode($output);
}
/**
* @inheritDoc
*/
function sendPostRequest(string $licensePlate, string $endpoint, array $data): object
{
// Send a POST request to the motorapi
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->api_url . $endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output);
}
/**
* @inheritDoc
*/
function sendPutRequest(string $licensePlate, string $endpoint, array $data): object
{
// Send a PUT request to the motorapi
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->api_url . $endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output);
}
/**
* @inheritDoc
*/
function sendDeleteRequest(string $licensePlate, string $endpoint, array $data): object
{
// Send a DELETE request to the motorapi
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->api_url . $endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output);
}
/**
* @inheritDoc
* @throws Exception
*/
function addRequestToLog(string $licensePlate, string $endpoint, object $response): void
{
// Add the request to the motorapi request log
$motorapi_lookups = new motorapi_lookups_o();
$motorapi_lookups->add($licensePlate, json_encode($response), $endpoint);
}
}