1. <?php
  2. /**
  3. * HTTP request monitoring components.
  4. */
  5. class RequestMonitor {
  6. private $startTime = 0;
  7. private $endTime = 0;
  8. private $statusCode = 0;
  9. private $url = '';
  10. private $headers = [];
  11. private $body = '';
  12. private $error = null;
  13. /**
  14. * Starts the request timer.
  15. */
  16. public function start() {
  17. $this->startTime = microtime(true);
  18. }
  19. /**
  20. * Stops the request timer.
  21. */
  22. public function stop() {
  23. $this->endTime = microtime(true);
  24. }
  25. /**
  26. * Sets the HTTP status code.
  27. * @param int $statusCode
  28. */
  29. public function setStatusCode(int $statusCode) {
  30. $this->statusCode = $statusCode;
  31. }
  32. /**
  33. * Sets the URL.
  34. * @param string $url
  35. */
  36. public function setUrl(string $url) {
  37. $this->url = $url;
  38. }
  39. /**
  40. * Sets the headers.
  41. * @param array $headers
  42. */
  43. public function setHeaders(array $headers) {
  44. $this->headers = $headers;
  45. }
  46. /**
  47. * Sets the request body.
  48. * @param string $body
  49. */
  50. public function setBody(string $body) {
  51. $this->body = $body;
  52. }
  53. /**
  54. * Sets the error message.
  55. * @param string $error
  56. */
  57. public function setError(string $error) {
  58. $this->error = $error;
  59. }
  60. /**
  61. * Gets the request duration in seconds.
  62. * @return float
  63. */
  64. public function getDuration(): float {
  65. return ($this->endTime - $this->startTime) / 1000;
  66. }
  67. /**
  68. * Returns the status code.
  69. * @return int
  70. */
  71. public function getStatusCode(): int {
  72. return $this->statusCode;
  73. }
  74. /**
  75. * Returns the URL.
  76. * @return string
  77. */
  78. public function getUrl(): string {
  79. return $this->url;
  80. }
  81. /**
  82. * Returns the headers.
  83. * @return array
  84. */
  85. public function getHeaders(): array {
  86. return $this->headers;
  87. }
  88. /**
  89. * Returns the body.
  90. * @return string
  91. */
  92. public function getBody(): string {
  93. return $this->body;
  94. }
  95. /**
  96. * Returns the error message.
  97. * @return string|null
  98. */
  99. public function getError(): ?string {
  100. return $this->error;
  101. }
  102. }
  103. ?>

Add your comment