wikimedia/mediawiki-extensions-Wikibase

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

Summary

Maintainability
A
2 hrs
Test Coverage
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCases;

use Wikibase\DataModel\Entity\ItemId;
use Wikibase\Repo\RestApi\Domain\Services\ItemRevisionMetadataRetriever;

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

    private ItemRevisionMetadataRetriever $metadataRetriever;

    public function __construct( ItemRevisionMetadataRetriever $metadataRetriever ) {
        $this->metadataRetriever = $metadataRetriever;
    }

    /**
     * @throws ItemRedirect if the item is a redirect
     * @throws UseCaseError if the item does not exist
     *
     * @return array{int, string}
     */
    public function execute( ItemId $id ): array {
        $metaDataResult = $this->metadataRetriever->getLatestRevisionMetadata( $id );

        if ( !$metaDataResult->itemExists() ) {
            throw UseCaseError::newResourceNotFound( 'item' );
        }

        if ( $metaDataResult->isRedirect() ) {
            throw new ItemRedirect( $metaDataResult->getRedirectTarget()->getSerialization() );
        }

        return [ $metaDataResult->getRevisionId(), $metaDataResult->getRevisionTimestamp() ];
    }

}