Skip to content

Commit 10d7ff0

Browse files
committed
IMP: Use strong typing and check for HTTPClient
1 parent e9ece43 commit 10d7ff0

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

Model/HttpClient/Page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class Page extends Wordpress
1010
{
11-
public $type = 'pages';
11+
public ?string $type = 'pages';
1212

1313
public function postMetaDataToPage(int $pageId, string $key, string $value, string $authentication)
1414
{

Model/HttpClient/Wordpress.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class Wordpress
1212
{
1313
const WP_JSON_URL_PREFIX = '/wp-json/wp/v2/';
1414

15-
public $type = '';
15+
public ?string $type = null;
1616

1717
/**
1818
* @var \Symfony\Contracts\HttpClient\HttpClientInterface
@@ -52,6 +52,9 @@ public function setBaseUrl(string $baseUrl)
5252
*/
5353
public function all(int $pageSize = 10, array $filters = []): \Generator
5454
{
55+
if (is_null($this->type)) {
56+
throw new \Exception("Type is required for HTTPClient");
57+
}
5558
$peekHeaders = $this->peek($pageSize);
5659

5760
if (empty($peekHeaders['x-wp-total'])) {
@@ -95,13 +98,21 @@ public function all(int $pageSize = 10, array $filters = []): \Generator
9598
*/
9699
public function get(int $id): array
97100
{
101+
if (is_null($this->type)) {
102+
throw new \Exception("Type is required for HTTPClient");
103+
}
104+
98105
$response = $this->client->request('GET', static::WP_JSON_URL_PREFIX . $this->type . '/' . $id);
99106

100107
return json_decode($response->getContent(), true);
101108
}
102109

103110
public function postMetaDataToPage(int $pageId, string $key, string $value, string $authentication)
104111
{
112+
if (is_null($this->type)) {
113+
throw new \Exception("Type is required for HTTPClient");
114+
}
115+
105116
try {
106117
$response = $this->client->request(
107118
'POST',
@@ -128,6 +139,10 @@ public function postMetaDataToPage(int $pageId, string $key, string $value, stri
128139
*/
129140
public function peek(int $pageSize): array
130141
{
142+
if (is_null($this->type)) {
143+
throw new \Exception("Type is required for HTTPClient");
144+
}
145+
131146
$peekResponse = $this->client->request('HEAD', static::WP_JSON_URL_PREFIX . $this->type . '?per_page=' . $pageSize);
132147

133148
return $peekResponse->getHeaders();

0 commit comments

Comments
 (0)