wikimedia/mediawiki-extensions-Wikibase

View on GitHub
lib/packages/wikibase/data-model-services/src/Diff/PropertyPatcher.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Wikibase\DataModel\Services\Diff;

use InvalidArgumentException;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\Entity\Property;
use Wikibase\DataModel\Services\Diff\Internal\FingerprintPatcher;

/**
 * @since 1.0
 *
 * @license GPL-2.0-or-later
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class PropertyPatcher implements EntityPatcherStrategy {

    /**
     * @var FingerprintPatcher
     */
    private $fingerprintPatcher;

    /**
     * @var StatementListPatcher
     */
    private $statementListPatcher;

    public function __construct() {
        $this->fingerprintPatcher = new FingerprintPatcher();
        $this->statementListPatcher = new StatementListPatcher();
    }

    /**
     * @param string $entityType
     *
     * @return bool
     */
    public function canPatchEntityType( $entityType ) {
        return $entityType === 'property';
    }

    /**
     * @param EntityDocument $entity
     * @param EntityDiff $patch
     *
     * @throws InvalidArgumentException
     */
    public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
        $this->assertIsProperty( $entity );

        $this->patchProperty( $entity, $patch );
    }

    private function assertIsProperty( EntityDocument $property ) {
        if ( !( $property instanceof Property ) ) {
            throw new InvalidArgumentException( '$property must be an instance of Property' );
        }
    }

    private function patchProperty( Property $property, EntityDiff $patch ) {
        $this->fingerprintPatcher->patchFingerprint( $property->getFingerprint(), $patch );

        $this->statementListPatcher->patchStatementList(
            $property->getStatements(),
            $patch->getClaimsDiff()
        );
    }

}