<?php
/**
* HTTP request monitoring components.
*/
class RequestMonitor {
private $startTime = 0;
private $endTime = 0;
private $statusCode = 0;
private $url = '';
private $headers = [];
private $body = '';
private $error = null;
/**
* Starts the request timer.
*/
public function start() {
$this->startTime = microtime(true);
}
/**
* Stops the request timer.
*/
public function stop() {
$this->endTime = microtime(true);
}
/**
* Sets the HTTP status code.
* @param int $statusCode
*/
public function setStatusCode(int $statusCode) {
$this->statusCode = $statusCode;
}
/**
* Sets the URL.
* @param string $url
*/
public function setUrl(string $url) {
$this->url = $url;
}
/**
* Sets the headers.
* @param array $headers
*/
public function setHeaders(array $headers) {
$this->headers = $headers;
}
/**
* Sets the request body.
* @param string $body
*/
public function setBody(string $body) {
$this->body = $body;
}
/**
* Sets the error message.
* @param string $error
*/
public function setError(string $error) {
$this->error = $error;
}
/**
* Gets the request duration in seconds.
* @return float
*/
public function getDuration(): float {
return ($this->endTime - $this->startTime) / 1000;
}
/**
* Returns the status code.
* @return int
*/
public function getStatusCode(): int {
return $this->statusCode;
}
/**
* Returns the URL.
* @return string
*/
public function getUrl(): string {
return $this->url;
}
/**
* Returns the headers.
* @return array
*/
public function getHeaders(): array {
return $this->headers;
}
/**
* Returns the body.
* @return string
*/
public function getBody(): string {
return $this->body;
}
/**
* Returns the error message.
* @return string|null
*/
public function getError(): ?string {
return $this->error;
}
}
?>
Add your comment