wikimedia/mediawiki-extensions-Wikibase

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

Summary

Maintainability
D
1 day
Test Coverage
<?php declare( strict_types=1 );

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

use OutOfBoundsException;
use Wikibase\Repo\RestApi\Application\UseCases\AssertItemExists;
use Wikibase\Repo\RestApi\Application\UseCases\AssertUserIsAuthorized;
use Wikibase\Repo\RestApi\Application\UseCases\UpdateExceptionHandler;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\Domain\Model\EditMetadata;
use Wikibase\Repo\RestApi\Domain\Model\LabelEditSummary;
use Wikibase\Repo\RestApi\Domain\Services\ItemUpdater;
use Wikibase\Repo\RestApi\Domain\Services\ItemWriteModelRetriever;

/**
 * @license GPL-2.0-or-later
 */
class RemoveItemLabel {
    use UpdateExceptionHandler;

    private RemoveItemLabelValidator $useCaseValidator;
    private AssertItemExists $assertItemExists;
    private AssertUserIsAuthorized $assertUserIsAuthorized;
    private ItemWriteModelRetriever $itemRetriever;
    private ItemUpdater $itemUpdater;

    public function __construct(
        RemoveItemLabelValidator $useCaseValidator,
        AssertItemExists $assertItemExists,
        AssertUserIsAuthorized $assertUserIsAuthorized,
        ItemWriteModelRetriever $itemRetriever,
        ItemUpdater $itemUpdater
    ) {
        $this->useCaseValidator = $useCaseValidator;
        $this->assertItemExists = $assertItemExists;
        $this->assertUserIsAuthorized = $assertUserIsAuthorized;
        $this->itemRetriever = $itemRetriever;
        $this->itemUpdater = $itemUpdater;
    }

    public function execute( RemoveItemLabelRequest $request ): void {
        $deserializedRequest = $this->useCaseValidator->validateAndDeserialize( $request );

        $itemId = $deserializedRequest->getItemId();
        $languageCode = $deserializedRequest->getLanguageCode();
        $providedEditMetadata = $deserializedRequest->getEditMetadata();

        $this->assertItemExists->execute( $itemId );
        $this->assertUserIsAuthorized->checkEditPermissions( $itemId, $providedEditMetadata->getUser() );

        $item = $this->itemRetriever->getItemWriteModel( $itemId );

        try {
            $label = $item->getLabels()->getByLanguage( $languageCode );
        } catch ( OutOfBoundsException $e ) {
            throw UseCaseError::newResourceNotFound( 'label' );
        }

        $item->getLabels()->removeByLanguage( $languageCode );

        $summary = LabelEditSummary::newRemoveSummary( $providedEditMetadata->getComment(), $label );

        $this->executeWithExceptionHandling( fn() => $this->itemUpdater->update(
            $item, // @phan-suppress-current-line PhanTypeMismatchArgumentNullable
            new EditMetadata( $providedEditMetadata->getTags(), $providedEditMetadata->isBot(), $summary )
        ) );
    }

}