Skip to content

Commit 8e910f9

Browse files
jenkins-botGerrit Code Review
authored andcommitted
Merge "REST: Modify ReplaceItemStatement to use exceptions"
2 parents f27249f + a7f4d32 commit 8e910f9

File tree

9 files changed

+424
-413
lines changed

9 files changed

+424
-413
lines changed

repo/rest-api/src/RouteHandlers/ReplaceItemStatementRouteHandler.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Wikibase\Repo\RestApi\RouteHandlers;
44

5-
use LogicException;
65
use MediaWiki\MediaWikiServices;
76
use MediaWiki\Rest\Handler;
87
use MediaWiki\Rest\RequestInterface;
@@ -18,9 +17,9 @@
1817
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\UserAgentCheckMiddleware;
1918
use Wikibase\Repo\RestApi\Serialization\StatementSerializer;
2019
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatement;
21-
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementErrorResponse;
2220
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementRequest;
23-
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementSuccessResponse;
21+
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementResponse;
22+
use Wikibase\Repo\RestApi\UseCases\UseCaseException;
2423
use Wikibase\Repo\RestApi\WbRestApi;
2524
use Wikimedia\ParamValidator\ParamValidator;
2625

@@ -92,22 +91,20 @@ public function run( ...$args ): Response {
9291

9392
public function runUseCase( string $itemId, string $statementId ): Response {
9493
$requestBody = $this->getValidatedBody();
95-
$useCaseResponse = $this->replaceItemStatement->execute( new ReplaceItemStatementRequest(
96-
$statementId,
97-
$requestBody[self::STATEMENT_BODY_PARAM],
98-
$requestBody[self::TAGS_BODY_PARAM],
99-
$requestBody[self::BOT_BODY_PARAM],
100-
$requestBody[self::COMMENT_BODY_PARAM],
101-
$this->getUsername(),
102-
$itemId
103-
) );
104-
105-
if ( $useCaseResponse instanceof ReplaceItemStatementSuccessResponse ) {
94+
95+
try {
96+
$useCaseResponse = $this->replaceItemStatement->execute( new ReplaceItemStatementRequest(
97+
$statementId,
98+
$requestBody[self::STATEMENT_BODY_PARAM],
99+
$requestBody[self::TAGS_BODY_PARAM],
100+
$requestBody[self::BOT_BODY_PARAM],
101+
$requestBody[self::COMMENT_BODY_PARAM],
102+
$this->getUsername(),
103+
$itemId
104+
) );
106105
return $this->newSuccessHttpResponse( $useCaseResponse );
107-
} elseif ( $useCaseResponse instanceof ReplaceItemStatementErrorResponse ) {
108-
return $this->responseFactory->newErrorResponse( $useCaseResponse );
109-
} else {
110-
throw new LogicException( 'Received an unexpected use case result in ' . __CLASS__ );
106+
} catch ( UseCaseException $e ) {
107+
return $this->responseFactory->newErrorResponseFromException( $e );
111108
}
112109
}
113110

@@ -157,7 +154,7 @@ public function getBodyValidator( $contentType ): BodyValidator {
157154
] ) : parent::getBodyValidator( $contentType );
158155
}
159156

160-
private function newSuccessHttpResponse( ReplaceItemStatementSuccessResponse $useCaseResponse ): Response {
157+
private function newSuccessHttpResponse( ReplaceItemStatementResponse $useCaseResponse ): Response {
161158
$httpResponse = $this->getResponseFactory()->create();
162159
$httpResponse->setStatus( 200 );
163160
$httpResponse->setHeader( 'Content-Type', 'application/json' );

repo/rest-api/src/RouteHandlers/ReplaceStatementRouteHandler.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Wikibase\Repo\RestApi\RouteHandlers;
44

5-
use LogicException;
65
use MediaWiki\MediaWikiServices;
76
use MediaWiki\Rest\Handler;
87
use MediaWiki\Rest\RequestInterface;
@@ -19,9 +18,9 @@
1918
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\UserAgentCheckMiddleware;
2019
use Wikibase\Repo\RestApi\Serialization\StatementSerializer;
2120
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatement;
22-
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementErrorResponse;
2321
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementRequest;
24-
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementSuccessResponse;
22+
use Wikibase\Repo\RestApi\UseCases\ReplaceItemStatement\ReplaceItemStatementResponse;
23+
use Wikibase\Repo\RestApi\UseCases\UseCaseException;
2524
use Wikibase\Repo\RestApi\WbRestApi;
2625
use Wikimedia\ParamValidator\ParamValidator;
2726

@@ -94,21 +93,19 @@ public function run( ...$args ): Response {
9493

9594
public function runUseCase( string $statementId ): Response {
9695
$requestBody = $this->getValidatedBody();
97-
$useCaseResponse = $this->replaceItemStatement->execute( new ReplaceItemStatementRequest(
98-
$statementId,
99-
$requestBody[self::STATEMENT_BODY_PARAM],
100-
$requestBody[self::TAGS_BODY_PARAM],
101-
$requestBody[self::BOT_BODY_PARAM],
102-
$requestBody[self::COMMENT_BODY_PARAM],
103-
$this->getUsername()
104-
) );
105-
106-
if ( $useCaseResponse instanceof ReplaceItemStatementSuccessResponse ) {
96+
97+
try {
98+
$useCaseResponse = $this->replaceItemStatement->execute( new ReplaceItemStatementRequest(
99+
$statementId,
100+
$requestBody[self::STATEMENT_BODY_PARAM],
101+
$requestBody[self::TAGS_BODY_PARAM],
102+
$requestBody[self::BOT_BODY_PARAM],
103+
$requestBody[self::COMMENT_BODY_PARAM],
104+
$this->getUsername()
105+
) );
107106
return $this->newSuccessHttpResponse( $useCaseResponse );
108-
} elseif ( $useCaseResponse instanceof ReplaceItemStatementErrorResponse ) {
109-
return $this->responseFactory->newErrorResponse( $useCaseResponse );
110-
} else {
111-
throw new LogicException( 'Received an unexpected use case result in ' . __CLASS__ );
107+
} catch ( UseCaseException $e ) {
108+
return $this->responseFactory->newErrorResponseFromException( $e );
112109
}
113110
}
114111

@@ -153,7 +150,7 @@ public function getBodyValidator( $contentType ): BodyValidator {
153150
] ) : parent::getBodyValidator( $contentType );
154151
}
155152

156-
private function newSuccessHttpResponse( ReplaceItemStatementSuccessResponse $useCaseResponse ): Response {
153+
private function newSuccessHttpResponse( ReplaceItemStatementResponse $useCaseResponse ): Response {
157154
$httpResponse = $this->getResponseFactory()->create();
158155
$httpResponse->setStatus( 200 );
159156
$httpResponse->setHeader( 'Content-Type', 'application/json' );

repo/rest-api/src/UseCases/ReplaceItemStatement/ReplaceItemStatement.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Wikibase\Repo\RestApi\Domain\Services\ItemUpdater;
1818
use Wikibase\Repo\RestApi\Domain\Services\PermissionChecker;
1919
use Wikibase\Repo\RestApi\UseCases\ErrorResponse;
20+
use Wikibase\Repo\RestApi\UseCases\UseCaseException;
2021

2122
/**
2223
* @license GPL-2.0-or-later
@@ -44,13 +45,10 @@ public function __construct(
4445
}
4546

4647
/**
47-
* @return ReplaceItemStatementSuccessResponse|ReplaceItemStatementErrorResponse
48+
* @throws UseCaseException
4849
*/
49-
public function execute( ReplaceItemStatementRequest $request ) {
50-
$validationError = $this->validator->validate( $request );
51-
if ( $validationError ) {
52-
return ReplaceItemStatementErrorResponse::newFromValidationError( $validationError );
53-
}
50+
public function execute( ReplaceItemStatementRequest $request ): ReplaceItemStatementResponse {
51+
$this->validator->assertValidRequest( $request );
5452

5553
$requestedItemId = $request->getItemId();
5654
$statementIdParser = new StatementGuidParser( new ItemIdParser() );
@@ -61,19 +59,19 @@ public function execute( ReplaceItemStatementRequest $request ) {
6159

6260
$latestRevision = $this->revisionMetadataRetriever->getLatestRevisionMetadata( $itemId );
6361
if ( $requestedItemId && !$latestRevision->itemExists() ) {
64-
return new ReplaceItemStatementErrorResponse(
62+
throw new UseCaseException(
6563
ErrorResponse::ITEM_NOT_FOUND,
6664
"Could not find an item with the ID: {$itemId}"
6765
);
6866
} elseif ( !$latestRevision->itemExists()
6967
|| $latestRevision->isRedirect()
7068
|| !$itemId->equals( $statementId->getEntityId() ) ) {
71-
return $this->newStatementNotFoundErrorResponse( $statementId );
69+
$this->throwStatementNotFoundException( $statementId );
7270
}
7371

7472
$user = $request->hasUser() ? User::withUsername( $request->getUsername() ) : User::newAnonymous();
7573
if ( !$this->permissionChecker->canEdit( $user, $itemId ) ) {
76-
return new ReplaceItemStatementErrorResponse(
74+
throw new UseCaseException(
7775
ErrorResponse::PERMISSION_DENIED,
7876
'You have no permission to edit this item.'
7977
);
@@ -85,14 +83,14 @@ public function execute( ReplaceItemStatementRequest $request ) {
8583
try {
8684
$item->getStatements()->replaceStatement( $statementId, $newStatement );
8785
} catch ( StatementNotFoundException $e ) {
88-
return $this->newStatementNotFoundErrorResponse( $statementId );
86+
$this->throwStatementNotFoundException( $statementId );
8987
} catch ( StatementGuidChangedException $e ) {
90-
return new ReplaceItemStatementErrorResponse(
88+
throw new UseCaseException(
9189
ErrorResponse::INVALID_OPERATION_CHANGED_STATEMENT_ID,
9290
'Cannot change the ID of the existing statement'
9391
);
9492
} catch ( PropertyChangedException $e ) {
95-
return new ReplaceItemStatementErrorResponse(
93+
throw new UseCaseException(
9694
ErrorResponse::INVALID_OPERATION_CHANGED_PROPERTY,
9795
'Cannot change the property of the existing statement'
9896
);
@@ -105,15 +103,18 @@ public function execute( ReplaceItemStatementRequest $request ) {
105103
);
106104
$newRevision = $this->itemUpdater->update( $item, $editMetadata );
107105

108-
return new ReplaceItemStatementSuccessResponse(
106+
return new ReplaceItemStatementResponse(
109107
$newRevision->getItem()->getStatements()->getStatementById( $statementId ),
110108
$newRevision->getLastModified(),
111109
$newRevision->getRevisionId()
112110
);
113111
}
114112

115-
private function newStatementNotFoundErrorResponse( StatementGuid $statementId ): ReplaceItemStatementErrorResponse {
116-
return new ReplaceItemStatementErrorResponse(
113+
/**
114+
* @throws UseCaseException
115+
*/
116+
private function throwStatementNotFoundException( StatementGuid $statementId ): void {
117+
throw new UseCaseException(
117118
ErrorResponse::STATEMENT_NOT_FOUND,
118119
"Could not find a statement with the ID: $statementId"
119120
);

repo/rest-api/src/UseCases/ReplaceItemStatement/ReplaceItemStatementErrorResponse.php

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @license GPL-2.0-or-later
99
*/
10-
class ReplaceItemStatementSuccessResponse {
10+
class ReplaceItemStatementResponse {
1111

1212
private Statement $statement;
1313
private string $lastModified;

0 commit comments

Comments
 (0)