Skip to content

Commit d8a9ce3

Browse files
refactor: improve delete extension method documentation and add precondition failed response handling
- Updated the documentation for the delete extension method to clarify that it removes the extension and requires no existing instances. - Added handling for precondition failed responses in the extension delete implementation.
1 parent 85532a5 commit d8a9ce3

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mittwald\ApiClient\Generated\V2\Clients\Marketplace\ExtensionDeleteExtension;
6+
7+
use InvalidArgumentException;
8+
use JsonSchema\Validator;
9+
use Mittwald\ApiClient\Client\ResponseContainer;
10+
use Mittwald\ApiClient\Generated\V2\Schemas\Commons\Error;
11+
use Psr\Http\Message\ResponseInterface;
12+
13+
class ExtensionDeleteExtensionPreconditionFailedResponse implements ResponseContainer
14+
{
15+
/**
16+
* Schema used to validate input for creating instances of this class
17+
*/
18+
private static array $internalValidationSchema = [
19+
'type' => 'object',
20+
'required' => [
21+
'body',
22+
],
23+
'properties' => [
24+
'body' => [
25+
'$ref' => '#/components/schemas/de.mittwald.v1.commons.Error',
26+
],
27+
],
28+
];
29+
30+
private Error $body;
31+
32+
private ResponseInterface|null $httpResponse = null;
33+
34+
public function __construct(Error $body)
35+
{
36+
$this->body = $body;
37+
}
38+
39+
public function getBody(): Error
40+
{
41+
return $this->body;
42+
}
43+
44+
public function withBody(Error $body): self
45+
{
46+
$clone = clone $this;
47+
$clone->body = $body;
48+
49+
return $clone;
50+
}
51+
52+
/**
53+
* Builds a new instance from an input array
54+
*
55+
* @param array|object $input Input data
56+
* @param bool $validate Set this to false to skip validation; use at own risk
57+
* @return ExtensionDeleteExtensionPreconditionFailedResponse Created instance
58+
* @throws InvalidArgumentException
59+
*/
60+
public static function buildFromInput(array|object $input, bool $validate = true): ExtensionDeleteExtensionPreconditionFailedResponse
61+
{
62+
$input = is_array($input) ? Validator::arrayToObjectRecursive($input) : $input;
63+
if ($validate) {
64+
static::validateInput($input);
65+
}
66+
67+
$body = Error::buildFromInput($input->{'body'}, validate: $validate);
68+
69+
$obj = new self($body);
70+
71+
return $obj;
72+
}
73+
74+
/**
75+
* Converts this object back to a simple array that can be JSON-serialized
76+
*
77+
* @return array Converted array
78+
*/
79+
public function toJson(): array
80+
{
81+
$output = [];
82+
$output['body'] = $this->body->toJson();
83+
84+
return $output;
85+
}
86+
87+
/**
88+
* Validates an input array
89+
*
90+
* @param array|object $input Input data
91+
* @param bool $return Return instead of throwing errors
92+
* @return bool Validation result
93+
* @throws InvalidArgumentException
94+
*/
95+
public static function validateInput(array|object $input, bool $return = false): bool
96+
{
97+
$validator = new \Mittwald\ApiClient\Validator\Validator();
98+
$input = is_array($input) ? Validator::arrayToObjectRecursive($input) : $input;
99+
$validator->validate($input, self::$internalValidationSchema);
100+
101+
if (!$validator->isValid() && !$return) {
102+
$errors = array_map(function (array $e): string {
103+
return $e["property"] . ": " . $e["message"];
104+
}, $validator->getErrors());
105+
throw new InvalidArgumentException(join(", ", $errors));
106+
}
107+
108+
return $validator->isValid();
109+
}
110+
111+
public function __clone()
112+
{
113+
}
114+
115+
public static function fromResponse(ResponseInterface $httpResponse): self
116+
{
117+
$parsedBody = json_decode($httpResponse->getBody()->getContents(), associative: true);
118+
$response = static::buildFromInput(['body' => $parsedBody], validate: false);
119+
$response->httpResponse = $httpResponse;
120+
return $response;
121+
}
122+
123+
public function getResponse(): ResponseInterface|null
124+
{
125+
return $this->httpResponse;
126+
}
127+
}

src/Generated/V2/Clients/Marketplace/MarketplaceClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public function extensionCreateRetrievalKey(ExtensionCreateRetrievalKeyRequest $
383383
/**
384384
* Delete an extension.
385385
*
386-
* This action deletes all ExtensionInstances and afterwards the Extension itself.
386+
* Remove the Extension. Make sure that there are no instances for this extension
387387
*
388388
* @see https://developer.mittwald.de/reference/v2/#tag/Marketplace/operation/extension-delete-extension
389389
* @throws GuzzleException

src/Generated/V2/Clients/Marketplace/MarketplaceClientImpl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
use Mittwald\ApiClient\Generated\V2\Clients\Marketplace\ExtensionCreateRetrievalKey\ExtensionCreateRetrievalKeyTooManyRequestsResponse;
152152
use Mittwald\ApiClient\Generated\V2\Clients\Marketplace\ExtensionDeleteExtension\ExtensionDeleteExtensionDefaultResponse;
153153
use Mittwald\ApiClient\Generated\V2\Clients\Marketplace\ExtensionDeleteExtension\ExtensionDeleteExtensionNotFoundResponse;
154+
use Mittwald\ApiClient\Generated\V2\Clients\Marketplace\ExtensionDeleteExtension\ExtensionDeleteExtensionPreconditionFailedResponse;
154155
use Mittwald\ApiClient\Generated\V2\Clients\Marketplace\ExtensionDeleteExtension\ExtensionDeleteExtensionRequest;
155156
use Mittwald\ApiClient\Generated\V2\Clients\Marketplace\ExtensionDeleteExtension\ExtensionDeleteExtensionTooManyRequestsResponse;
156157
use Mittwald\ApiClient\Generated\V2\Clients\Marketplace\ExtensionDeleteExtensionInstance\ExtensionDeleteExtensionInstanceDefaultResponse;
@@ -929,7 +930,7 @@ public function extensionCreateRetrievalKey(ExtensionCreateRetrievalKeyRequest $
929930
/**
930931
* Delete an extension.
931932
*
932-
* This action deletes all ExtensionInstances and afterwards the Extension itself.
933+
* Remove the Extension. Make sure that there are no instances for this extension
933934
*
934935
* @see https://developer.mittwald.de/reference/v2/#tag/Marketplace/operation/extension-delete-extension
935936
* @throws GuzzleException
@@ -946,6 +947,7 @@ public function extensionDeleteExtension(ExtensionDeleteExtensionRequest $reques
946947
}
947948
throw new UnexpectedResponseException(match ($httpResponse->getStatusCode()) {
948949
404 => ExtensionDeleteExtensionNotFoundResponse::fromResponse($httpResponse),
950+
412 => ExtensionDeleteExtensionPreconditionFailedResponse::fromResponse($httpResponse),
949951
429 => ExtensionDeleteExtensionTooManyRequestsResponse::fromResponse($httpResponse),
950952
default => ExtensionDeleteExtensionDefaultResponse::fromResponse($httpResponse),
951953
});

0 commit comments

Comments
 (0)