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