Skip to content

Commit 46b65d7

Browse files
committed
[FT] Moved constants and functions into class containers (#48)
1 parent f0934ad commit 46b65d7

File tree

13 files changed

+488
-552
lines changed

13 files changed

+488
-552
lines changed

composer.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@
3737
"autoload": {
3838
"psr-4": {
3939
"FlixTech\\SchemaRegistryApi\\": "src/"
40-
},
41-
"files": [
42-
"src/Requests/Functions.php",
43-
"src/Constants/Constants.php"
44-
]
40+
}
4541
},
4642
"autoload-dev": {
4743
"psr-4": {

src/Constants.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi;
6+
7+
final class Constants
8+
{
9+
public const COMPATIBILITY_NONE = 'NONE';
10+
public const COMPATIBILITY_BACKWARD = 'BACKWARD';
11+
public const COMPATIBILITY_BACKWARD_TRANSITIVE = 'BACKWARD_TRANSITIVE';
12+
public const COMPATIBILITY_FORWARD = 'FORWARD';
13+
public const COMPATIBILITY_FORWARD_TRANSITIVE = 'FORWARD_TRANSITIVE';
14+
public const COMPATIBILITY_FULL = 'FULL';
15+
public const COMPATIBILITY_FULL_TRANSITIVE = 'FULL_TRANSITIVE';
16+
public const VERSION_LATEST = 'latest';
17+
public const ACCEPT = 'Accept';
18+
public const ACCEPT_HEADER = [self::ACCEPT => 'application/vnd.schemaregistry.v1+json'];
19+
public const CONTENT_TYPE_HEADER = [self::CONTENT_TYPE => 'application/vnd.schemaregistry.v1+json'];
20+
public const CONTENT_TYPE = 'Content-Type';
21+
22+
private function __construct()
23+
{
24+
}
25+
26+
private function __clone()
27+
{
28+
}
29+
}

src/Constants/Constants.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Exception/ExceptionMap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace FlixTech\SchemaRegistryApi\Exception;
66

77
use Exception;
8+
use FlixTech\SchemaRegistryApi\Json;
89
use Psr\Http\Message\ResponseInterface;
910
use function array_key_exists;
10-
use function FlixTech\SchemaRegistryApi\Requests\jsonDecode;
1111
use function sprintf;
1212

1313
final class ExceptionMap
@@ -89,7 +89,7 @@ public function hasMappableError(ResponseInterface $response): bool
8989
private function guardAgainstMissingErrorCode(ResponseInterface $response): array
9090
{
9191
try {
92-
$decodedBody = jsonDecode((string) $response->getBody());
92+
$decodedBody = Json::jsonDecode((string)$response->getBody());
9393

9494
if (!is_array($decodedBody) || !array_key_exists(self::ERROR_CODE_FIELD_NAME, $decodedBody)) {
9595
throw new RuntimeException(

src/Json.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi;
6+
7+
use Assert\Assert;
8+
use InvalidArgumentException;
9+
use JsonException;
10+
use Psr\Http\Message\ResponseInterface;
11+
12+
final class Json
13+
{
14+
private function __construct()
15+
{
16+
}
17+
18+
public static function validateStringAsJson(string $schema): string
19+
{
20+
Assert::that($schema)->isJsonString('$schema must be a valid JSON string');
21+
22+
return $schema;
23+
}
24+
25+
/**
26+
* @param string $jsonString
27+
* @param int $depth
28+
*
29+
* @return mixed
30+
*
31+
* @throws JsonException
32+
*/
33+
public static function jsonDecode(string $jsonString, int $depth = 512)
34+
{
35+
return json_decode($jsonString, true, $depth, JSON_THROW_ON_ERROR);
36+
}
37+
38+
/**
39+
* @param mixed $data
40+
*
41+
* @return string
42+
*
43+
* @throws JsonException
44+
*/
45+
public static function jsonEncode($data): string
46+
{
47+
return json_encode($data, JSON_THROW_ON_ERROR);
48+
}
49+
50+
/**
51+
* @param ResponseInterface $response
52+
*
53+
* @return array<mixed, mixed>
54+
*/
55+
public static function decodeResponse(ResponseInterface $response): array
56+
{
57+
$body = (string)$response->getBody();
58+
59+
try {
60+
return Json::jsonDecode($body);
61+
} catch (JsonException $e) {
62+
throw new InvalidArgumentException(
63+
sprintf('%s - with content "%s"', $e->getMessage(), $body),
64+
$e->getCode(),
65+
$e
66+
);
67+
}
68+
}
69+
70+
private function __clone()
71+
{
72+
}
73+
}

src/Registry/GuzzlePromiseAsyncRegistry.php

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,18 @@
77
use AvroSchema;
88
use Closure;
99
use FlixTech\SchemaRegistryApi\AsynchronousRegistry;
10+
use FlixTech\SchemaRegistryApi\Constants;
1011
use FlixTech\SchemaRegistryApi\Exception\ExceptionMap;
1112
use FlixTech\SchemaRegistryApi\Schema\AvroReference;
1213
use FlixTech\SchemaRegistryApi\Exception\RuntimeException;
1314
use FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException;
15+
use FlixTech\SchemaRegistryApi\Json;
16+
use FlixTech\SchemaRegistryApi\Requests;
1417
use GuzzleHttp\ClientInterface;
1518
use GuzzleHttp\Exception\RequestException;
1619
use GuzzleHttp\Promise\PromiseInterface;
1720
use Psr\Http\Message\RequestInterface;
1821
use Psr\Http\Message\ResponseInterface;
19-
use const FlixTech\SchemaRegistryApi\Constants\VERSION_LATEST;
20-
use function FlixTech\SchemaRegistryApi\Requests\checkIfSubjectHasSchemaRegisteredRequest;
21-
use function FlixTech\SchemaRegistryApi\Requests\decodeResponse;
22-
use function FlixTech\SchemaRegistryApi\Requests\registerNewSchemaVersionWithSubjectRequest;
23-
use function FlixTech\SchemaRegistryApi\Requests\schemaRequest;
24-
use function FlixTech\SchemaRegistryApi\Requests\singleSubjectVersionRequest;
25-
use function FlixTech\SchemaRegistryApi\Requests\validateSchemaId;
26-
use function FlixTech\SchemaRegistryApi\Requests\validateVersionId;
2722

2823
class GuzzlePromiseAsyncRegistry implements AsynchronousRegistry
2924
{
@@ -71,10 +66,10 @@ public function __construct(ClientInterface $client)
7166
*/
7267
public function register(string $subject, AvroSchema $schema, AvroReference ...$references): PromiseInterface
7368
{
74-
$request = registerNewSchemaVersionWithSubjectRequest((string) $schema, $subject, ...$references);
69+
$request = Requests::registerNewSchemaVersionWithSubjectRequest((string) $schema, $subject, ...$references);
7570

7671
$onFulfilled = function (ResponseInterface $response) {
77-
return $this->getJsonFromResponseBody($response)['id'];
72+
return Json::decodeResponse($response)['id'];
7873
};
7974

8075
return $this->makeRequest($request, $onFulfilled);
@@ -87,10 +82,10 @@ public function register(string $subject, AvroSchema $schema, AvroReference ...$
8782
*/
8883
public function schemaId(string $subject, AvroSchema $schema): PromiseInterface
8984
{
90-
$request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema);
85+
$request = Requests::checkIfSubjectHasSchemaRegisteredRequest($subject, (string)$schema);
9186

9287
$onFulfilled = function (ResponseInterface $response) {
93-
return $this->getJsonFromResponseBody($response)['id'];
88+
return Json::decodeResponse($response)['id'];
9489
};
9590

9691
return $this->makeRequest($request, $onFulfilled);
@@ -103,11 +98,11 @@ public function schemaId(string $subject, AvroSchema $schema): PromiseInterface
10398
*/
10499
public function schemaForId(int $schemaId): PromiseInterface
105100
{
106-
$request = schemaRequest(validateSchemaId($schemaId));
101+
$request = Requests::schemaRequest(Requests::validateSchemaId($schemaId));
107102

108103
$onFulfilled = function (ResponseInterface $response) {
109104
return AvroSchema::parse(
110-
$this->getJsonFromResponseBody($response)['schema']
105+
Json::decodeResponse($response)['schema']
111106
);
112107
};
113108

@@ -121,11 +116,11 @@ public function schemaForId(int $schemaId): PromiseInterface
121116
*/
122117
public function schemaForSubjectAndVersion(string $subject, int $version): PromiseInterface
123118
{
124-
$request = singleSubjectVersionRequest($subject, validateVersionId($version));
119+
$request = Requests::singleSubjectVersionRequest($subject, Requests::validateVersionId($version));
125120

126121
$onFulfilled = function (ResponseInterface $response) {
127122
return AvroSchema::parse(
128-
$this->getJsonFromResponseBody($response)['schema']
123+
Json::decodeResponse($response)['schema']
129124
);
130125
};
131126

@@ -139,10 +134,10 @@ public function schemaForSubjectAndVersion(string $subject, int $version): Promi
139134
*/
140135
public function schemaVersion(string $subject, AvroSchema $schema): PromiseInterface
141136
{
142-
$request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema);
137+
$request = Requests::checkIfSubjectHasSchemaRegisteredRequest($subject, (string)$schema);
143138

144139
$onFulfilled = function (ResponseInterface $response) {
145-
return $this->getJsonFromResponseBody($response)['version'];
140+
return Json::decodeResponse($response)['version'];
146141
};
147142

148143
return $this->makeRequest($request, $onFulfilled);
@@ -155,11 +150,11 @@ public function schemaVersion(string $subject, AvroSchema $schema): PromiseInter
155150
*/
156151
public function latestVersion(string $subject): PromiseInterface
157152
{
158-
$request = singleSubjectVersionRequest($subject, VERSION_LATEST);
153+
$request = Requests::singleSubjectVersionRequest($subject, Constants::VERSION_LATEST);
159154

160155
$onFulfilled = function (ResponseInterface $response) {
161156
return AvroSchema::parse(
162-
$this->getJsonFromResponseBody($response)['schema']
157+
Json::decodeResponse($response)['schema']
163158
);
164159
};
165160

@@ -178,31 +173,4 @@ private function makeRequest(RequestInterface $request, callable $onFulfilled):
178173
->sendAsync($request)
179174
->then($onFulfilled, $this->rejectedCallback);
180175
}
181-
182-
/**
183-
* @param ResponseInterface $response
184-
* @return array<mixed, mixed>
185-
*/
186-
private function getJsonFromResponseBody(ResponseInterface $response): array
187-
{
188-
$body = (string) $response->getBody();
189-
190-
try {
191-
$decoded = \GuzzleHttp\json_decode($body, true);
192-
193-
if (!is_array($decoded)) {
194-
throw new InvalidArgumentException(
195-
sprintf('response content "%s" is not a valid json object', $body)
196-
);
197-
}
198-
199-
return $decoded;
200-
} catch (InvalidArgumentException $e) {
201-
throw new InvalidArgumentException(
202-
sprintf('%s - with content "%s"', $e->getMessage(), $body),
203-
$e->getCode(),
204-
$e
205-
);
206-
}
207-
}
208176
}

src/Registry/Psr18SyncRegistry.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@
66

77
use AvroSchema;
88
use AvroSchemaParseException;
9+
use FlixTech\SchemaRegistryApi\Constants;
910
use FlixTech\SchemaRegistryApi\Exception\ExceptionMap;
1011
use FlixTech\SchemaRegistryApi\Exception\InvalidAvroSchemaException;
1112
use FlixTech\SchemaRegistryApi\Exception\RuntimeException;
1213
use FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException;
14+
use FlixTech\SchemaRegistryApi\Json;
15+
use FlixTech\SchemaRegistryApi\Requests;
1316
use FlixTech\SchemaRegistryApi\SynchronousRegistry;
1417
use Psr\Http\Client\ClientExceptionInterface;
1518
use Psr\Http\Client\ClientInterface;
1619
use Psr\Http\Message\RequestInterface;
1720
use Psr\Http\Message\ResponseInterface;
18-
use const FlixTech\SchemaRegistryApi\Constants\VERSION_LATEST;
19-
use function FlixTech\SchemaRegistryApi\Requests\checkIfSubjectHasSchemaRegisteredRequest;
20-
use function FlixTech\SchemaRegistryApi\Requests\decodeResponse;
21-
use function FlixTech\SchemaRegistryApi\Requests\registerNewSchemaVersionWithSubjectRequest;
22-
use function FlixTech\SchemaRegistryApi\Requests\schemaRequest;
23-
use function FlixTech\SchemaRegistryApi\Requests\singleSubjectVersionRequest;
24-
use function FlixTech\SchemaRegistryApi\Requests\validateSchemaId;
25-
use function FlixTech\SchemaRegistryApi\Requests\validateVersionId;
2621

2722
class Psr18SyncRegistry implements SynchronousRegistry
2823
{
@@ -44,62 +39,62 @@ public function __construct(ClientInterface $client)
4439

4540
public function register(string $subject, AvroSchema $schema): int
4641
{
47-
$request = registerNewSchemaVersionWithSubjectRequest((string) $schema, $subject);
42+
$request = Requests::registerNewSchemaVersionWithSubjectRequest((string)$schema, $subject);
4843

4944
$response = $this->makeRequest($request);
5045
$this->guardAgainstErrorResponse($response);
5146

52-
return decodeResponse($response)['id'];
47+
return Json::decodeResponse($response)['id'];
5348
}
5449

5550
public function schemaVersion(string $subject, AvroSchema $schema): int
5651
{
57-
$request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema);
52+
$request = Requests::checkIfSubjectHasSchemaRegisteredRequest($subject, (string)$schema);
5853

5954
$response = $this->makeRequest($request);
6055
$this->guardAgainstErrorResponse($response);
6156

62-
return decodeResponse($response)['version'];
57+
return Json::decodeResponse($response)['version'];
6358
}
6459

6560
public function latestVersion(string $subject): AvroSchema
6661
{
67-
$request = singleSubjectVersionRequest($subject, VERSION_LATEST);
62+
$request = Requests::singleSubjectVersionRequest($subject, Constants::VERSION_LATEST);
6863

6964
$response = $this->makeRequest($request);
7065
$this->guardAgainstErrorResponse($response);
7166

72-
return $this->parseAvroSchema(decodeResponse($response)['schema']);
67+
return $this->parseAvroSchema(Json::decodeResponse($response)['schema']);
7368
}
7469

7570
public function schemaId(string $subject, AvroSchema $schema): int
7671
{
77-
$request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema);
72+
$request = Requests::checkIfSubjectHasSchemaRegisteredRequest($subject, (string)$schema);
7873

7974
$response = $this->makeRequest($request);
8075
$this->guardAgainstErrorResponse($response);
8176

82-
return decodeResponse($response)['id'];
77+
return Json::decodeResponse($response)['id'];
8378
}
8479

8580
public function schemaForId(int $schemaId): AvroSchema
8681
{
87-
$request = schemaRequest(validateSchemaId($schemaId));
82+
$request = Requests::schemaRequest(Requests::validateSchemaId($schemaId));
8883

8984
$response = $this->makeRequest($request);
9085
$this->guardAgainstErrorResponse($response);
9186

92-
return $this->parseAvroSchema(decodeResponse($response)['schema']);
87+
return $this->parseAvroSchema(Json::decodeResponse($response)['schema']);
9388
}
9489

9590
public function schemaForSubjectAndVersion(string $subject, int $version): AvroSchema
9691
{
97-
$request = singleSubjectVersionRequest($subject, validateVersionId($version));
92+
$request = Requests::singleSubjectVersionRequest($subject, Requests::validateVersionId($version));
9893

9994
$response = $this->makeRequest($request);
10095
$this->guardAgainstErrorResponse($response);
10196

102-
return $this->parseAvroSchema(decodeResponse($response)['schema']);
97+
return $this->parseAvroSchema(Json::decodeResponse($response)['schema']);
10398
}
10499

105100
/**

0 commit comments

Comments
 (0)