Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
12 / 14
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SensePatcher
85.71% covered (warning)
85.71%
12 / 14
50.00% covered (danger)
50.00%
2 / 4
5.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 canPatchEntityType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 patchEntity
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 patch
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\Domain\Diff;
4
5use InvalidArgumentException;
6use Wikibase\DataModel\Entity\EntityDocument;
7use Wikibase\DataModel\Services\Diff\EntityDiff;
8use Wikibase\DataModel\Services\Diff\EntityPatcherStrategy;
9use Wikibase\DataModel\Services\Diff\StatementListPatcher;
10use Wikibase\DataModel\Services\Diff\TermListPatcher;
11use Wikibase\Lexeme\Domain\Model\Sense;
12
13/**
14 * @license GPL-2.0-or-later
15 */
16class SensePatcher implements EntityPatcherStrategy {
17
18    private TermListPatcher $termListPatcher;
19
20    private StatementListPatcher $statementListPatcher;
21
22    public function __construct() {
23        $this->termListPatcher = new TermListPatcher();
24        $this->statementListPatcher = new StatementListPatcher();
25    }
26
27    /**
28     * @param string $entityType
29     *
30     * @return boolean
31     */
32    public function canPatchEntityType( $entityType ) {
33        return $entityType === 'sense';
34    }
35
36    /**
37     * @param EntityDocument $entity
38     * @param EntityDiff $patch
39     *
40     * @throws InvalidArgumentException
41     */
42    public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
43        if ( !( $entity instanceof Sense ) ) {
44            throw new InvalidArgumentException( 'Can only patch Senses' );
45        }
46
47        // @phan-suppress-next-line PhanTypeMismatchArgumentSuperType
48        return $this->patch( $entity, $patch );
49    }
50
51    /**
52     * @param Sense $sense
53     * @param ChangeSenseDiffOp $diff
54     */
55    private function patch( Sense $sense, ChangeSenseDiffOp $diff ) {
56        $this->termListPatcher->patchTermList(
57            $sense->getGlosses(),
58            $diff->getGlossesDiff()
59        );
60
61        $this->statementListPatcher->patchStatementList(
62            $sense->getStatements(),
63            $diff->getStatementsDiff()
64        );
65    }
66
67}