Skip to content

Commit 2aed4e0

Browse files
authored
[FT] Moved constants and functions into class containers (#48)
1 parent 3bc7980 commit 2aed4e0

File tree

13 files changed

+480
-517
lines changed

13 files changed

+480
-517
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 (!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 & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,17 @@
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\Exception\RuntimeException;
1213
use FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException;
14+
use FlixTech\SchemaRegistryApi\Json;
15+
use FlixTech\SchemaRegistryApi\Requests;
1316
use GuzzleHttp\ClientInterface;
1417
use GuzzleHttp\Exception\RequestException;
1518
use GuzzleHttp\Promise\PromiseInterface;
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 GuzzlePromiseAsyncRegistry implements AsynchronousRegistry
2823
{
@@ -70,10 +65,10 @@ public function __construct(ClientInterface $client)
7065
*/
7166
public function register(string $subject, AvroSchema $schema): PromiseInterface
7267
{
73-
$request = registerNewSchemaVersionWithSubjectRequest((string) $schema, $subject);
68+
$request = Requests::registerNewSchemaVersionWithSubjectRequest((string)$schema, $subject);
7469

7570
$onFulfilled = function (ResponseInterface $response) {
76-
return decodeResponse($response)['id'];
71+
return Json::decodeResponse($response)['id'];
7772
};
7873

7974
return $this->makeRequest($request, $onFulfilled);
@@ -86,10 +81,10 @@ public function register(string $subject, AvroSchema $schema): PromiseInterface
8681
*/
8782
public function schemaId(string $subject, AvroSchema $schema): PromiseInterface
8883
{
89-
$request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema);
84+
$request = Requests::checkIfSubjectHasSchemaRegisteredRequest($subject, (string)$schema);
9085

9186
$onFulfilled = function (ResponseInterface $response) {
92-
return decodeResponse($response)['id'];
87+
return Json::decodeResponse($response)['id'];
9388
};
9489

9590
return $this->makeRequest($request, $onFulfilled);
@@ -102,11 +97,11 @@ public function schemaId(string $subject, AvroSchema $schema): PromiseInterface
10297
*/
10398
public function schemaForId(int $schemaId): PromiseInterface
10499
{
105-
$request = schemaRequest(validateSchemaId($schemaId));
100+
$request = Requests::schemaRequest(Requests::validateSchemaId($schemaId));
106101

107102
$onFulfilled = function (ResponseInterface $response) {
108103
return AvroSchema::parse(
109-
decodeResponse($response)['schema']
104+
Json::decodeResponse($response)['schema']
110105
);
111106
};
112107

@@ -120,11 +115,11 @@ public function schemaForId(int $schemaId): PromiseInterface
120115
*/
121116
public function schemaForSubjectAndVersion(string $subject, int $version): PromiseInterface
122117
{
123-
$request = singleSubjectVersionRequest($subject, validateVersionId($version));
118+
$request = Requests::singleSubjectVersionRequest($subject, Requests::validateVersionId($version));
124119

125120
$onFulfilled = function (ResponseInterface $response) {
126121
return AvroSchema::parse(
127-
decodeResponse($response)['schema']
122+
Json::decodeResponse($response)['schema']
128123
);
129124
};
130125

@@ -138,10 +133,10 @@ public function schemaForSubjectAndVersion(string $subject, int $version): Promi
138133
*/
139134
public function schemaVersion(string $subject, AvroSchema $schema): PromiseInterface
140135
{
141-
$request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema);
136+
$request = Requests::checkIfSubjectHasSchemaRegisteredRequest($subject, (string)$schema);
142137

143138
$onFulfilled = function (ResponseInterface $response) {
144-
return decodeResponse($response)['version'];
139+
return Json::decodeResponse($response)['version'];
145140
};
146141

147142
return $this->makeRequest($request, $onFulfilled);
@@ -154,11 +149,11 @@ public function schemaVersion(string $subject, AvroSchema $schema): PromiseInter
154149
*/
155150
public function latestVersion(string $subject): PromiseInterface
156151
{
157-
$request = singleSubjectVersionRequest($subject, VERSION_LATEST);
152+
$request = Requests::singleSubjectVersionRequest($subject, Constants::VERSION_LATEST);
158153

159154
$onFulfilled = function (ResponseInterface $response) {
160155
return AvroSchema::parse(
161-
decodeResponse($response)['schema']
156+
Json::decodeResponse($response)['schema']
162157
);
163158
};
164159

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)