Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.48% covered (success)
90.48%
19 / 21
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChangeOpsSensesEdit
90.48% covered (success)
90.48%
19 / 21
50.00% covered (danger)
50.00%
2 / 4
8.06
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validate
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
 apply
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
1<?php
2
3namespace Wikibase\Lexeme\DataAccess\ChangeOp;
4
5use ValueValidators\Error;
6use ValueValidators\Result;
7use Wikibase\DataModel\Entity\EntityDocument;
8use Wikibase\Lexeme\Domain\Model\Lexeme;
9use Wikibase\Lexeme\Domain\Model\SenseId;
10use Wikibase\Lib\Summary;
11use Wikibase\Repo\ChangeOp\ChangeOp;
12use Wikibase\Repo\ChangeOp\ChangeOpApplyException;
13use Wikibase\Repo\ChangeOp\DummyChangeOpResult;
14use Wikimedia\Assert\Assert;
15
16/**
17 * This is missing aggregation of summaries but they never would see light of day due to
18 * EditEntity::modifyEntity() & EditEntity::getSummary() anyways
19 *
20 * @license GPL-2.0-or-later
21 */
22class ChangeOpsSensesEdit implements ChangeOp {
23
24    private $changeOpForSense;
25
26    /**
27     * @param ChangeOp[] $changeOpForSense [ string $senseId => ChangeOp $changeOp ]
28     */
29    public function __construct( array $changeOpForSense ) {
30        $this->changeOpForSense = $changeOpForSense;
31    }
32
33    public function getActions() {
34        return [];
35    }
36
37    public function validate( EntityDocument $entity ) {
38        Assert::parameterType( Lexeme::class, $entity, '$entity' );
39        '@phan-var Lexeme $entity';
40
41        /** @var Lexeme $entity */
42
43        foreach ( $this->changeOpForSense as $senseId => $changeOps ) {
44            if ( $entity->getSenses()->getById( new SenseId( $senseId ) ) === null ) {
45                return Result::newError( [
46                    Error::newError(
47                        'Sense does not exist',
48                        null,
49                        'sense-not-found',
50                        [ $senseId ]
51                    ),
52                ] );
53            }
54        }
55
56        return Result::newSuccess();
57    }
58
59    public function apply( EntityDocument $entity, Summary $summary = null ) {
60        Assert::parameterType( Lexeme::class, $entity, '$entity' );
61        '@phan-var Lexeme $entity';
62        /** @var Lexeme $entity */
63
64        foreach ( $this->changeOpForSense as $senseId => $changeOp ) {
65            $sense = $entity->getSenses()->getById( new SenseId( $senseId ) );
66            if ( $sense === null ) {
67                throw new ChangeOpApplyException( 'wikibase-validator-sense-not-found' );
68            }
69
70            // Passes summary albeit there is no clear definition how summaries should be combined
71            $changeOp->apply( $sense, $summary );
72        }
73
74        return new DummyChangeOpResult();
75    }
76
77}