Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| Request | |
100.00% |
23 / 23 |
|
100.00% |
10 / 10 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| getMethod | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMethod | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addHeaders | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Beyondwords\Wordpress\Core; |
| 6 | |
| 7 | class Request |
| 8 | { |
| 9 | public const AUTH_HEADER_NAME = 'X-Api-Key'; |
| 10 | |
| 11 | public const CONTENT_TYPE_HEADER_NAME = 'Content-Type'; |
| 12 | |
| 13 | public const CONTENT_TYPE_HEADER_VALUE = 'application/json'; |
| 14 | |
| 15 | private string $method = ''; |
| 16 | |
| 17 | private string $url = ''; |
| 18 | |
| 19 | private string $body = ''; |
| 20 | |
| 21 | private array $headers = []; |
| 22 | |
| 23 | /** |
| 24 | * Request constructor. |
| 25 | * |
| 26 | * @param mixed $body |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | public function __construct( |
| 31 | string $method, |
| 32 | string $url, |
| 33 | string $body = '', |
| 34 | array $headers = [] |
| 35 | ) { |
| 36 | $this->setMethod($method); |
| 37 | $this->setUrl($url); |
| 38 | $this->setBody($body); |
| 39 | |
| 40 | // Add API key header to all requests. |
| 41 | $this->addHeaders([ |
| 42 | self::AUTH_HEADER_NAME => get_option('beyondwords_api_key'), |
| 43 | ]); |
| 44 | |
| 45 | // Add Content-Type header for non-GET requests. |
| 46 | if (in_array($method, ['POST', 'PUT', 'DELETE'])) { |
| 47 | // Default headers. |
| 48 | $this->addHeaders([ |
| 49 | self::CONTENT_TYPE_HEADER_NAME => self::CONTENT_TYPE_HEADER_VALUE, |
| 50 | ]); |
| 51 | } |
| 52 | |
| 53 | // Add custom headers. |
| 54 | $this->addHeaders($headers); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the HTTP method for the request. |
| 59 | */ |
| 60 | public function getMethod(): string |
| 61 | { |
| 62 | return $this->method; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set the HTTP method for the request. |
| 67 | * |
| 68 | * |
| 69 | */ |
| 70 | public function setMethod(string $method): void |
| 71 | { |
| 72 | $this->method = strtoupper($method); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the URL for the request. |
| 77 | */ |
| 78 | public function getUrl(): string |
| 79 | { |
| 80 | return $this->url; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set the URL for the request. |
| 85 | * |
| 86 | * |
| 87 | */ |
| 88 | public function setUrl(string $url): void |
| 89 | { |
| 90 | $this->url = $url; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the body for the request. |
| 95 | */ |
| 96 | public function getBody(): string |
| 97 | { |
| 98 | return $this->body; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Set the body for the request. |
| 103 | * |
| 104 | * |
| 105 | */ |
| 106 | public function setBody(string $body): void |
| 107 | { |
| 108 | $this->body = $body; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the headers for the request. |
| 113 | */ |
| 114 | public function getHeaders(): array |
| 115 | { |
| 116 | return $this->headers; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Set the headers to the request. |
| 121 | * |
| 122 | * |
| 123 | */ |
| 124 | public function setHeaders(array $headers): void |
| 125 | { |
| 126 | $this->headers = $headers; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Add extra headers to the request. |
| 131 | * |
| 132 | * @since 6.0.0 Introduced. |
| 133 | * |
| 134 | * |
| 135 | */ |
| 136 | public function addHeaders(array $headers): void |
| 137 | { |
| 138 | $this->setHeaders(array_merge( |
| 139 | $this->getHeaders(), |
| 140 | $headers |
| 141 | )); |
| 142 | } |
| 143 | } |