wikimedia/mediawiki-extensions-Wikibase

View on GitHub
repo/rest-api/src/Application/UseCases/GetItem/GetItem.php

Summary

Maintainability
B
4 hrs
Test Coverage
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\GetItem;

use Wikibase\Repo\RestApi\Application\UseCases\GetLatestItemRevisionMetadata;
use Wikibase\Repo\RestApi\Application\UseCases\ItemRedirect;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\Domain\Services\ItemPartsRetriever;

/**
 * @license GPL-2.0-or-later
 */
class GetItem {

    private GetLatestItemRevisionMetadata $getLatestRevisionMetadata;
    private ItemPartsRetriever $itemPartsRetriever;
    private GetItemValidator $validator;

    public function __construct(
        GetLatestItemRevisionMetadata $getLatestRevisionMetadata,
        ItemPartsRetriever $itemPartsRetriever,
        GetItemValidator $validator
    ) {
        $this->validator = $validator;
        $this->getLatestRevisionMetadata = $getLatestRevisionMetadata;
        $this->itemPartsRetriever = $itemPartsRetriever;
    }

    /**
     * @throws UseCaseError
     * @throws ItemRedirect
     */
    public function execute( GetItemRequest $itemRequest ): GetItemResponse {
        $deserializedRequest = $this->validator->validateAndDeserialize( $itemRequest );
        $itemId = $deserializedRequest->getItemId();

        [ $revisionId, $lastModified ] = $this->getLatestRevisionMetadata->execute( $itemId );

        return new GetItemResponse(
            // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Item validated and exists
            $this->itemPartsRetriever->getItemParts( $itemId, $deserializedRequest->getItemFields() ),
            $lastModified,
            $revisionId
        );
    }

}