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    /** @var ChangeOp[] */
25    private array $changeOpForSense;
26
27    /**
28     * @param ChangeOp[] $changeOpForSense [ string $senseId => ChangeOp $changeOp ]
29     */
30    public function __construct( array $changeOpForSense ) {
31        $this->changeOpForSense = $changeOpForSense;
32    }
33
34    public function getActions() {
35        return [];
36    }
37
38    public function validate( EntityDocument $entity ) {
39        Assert::parameterType( Lexeme::class, $entity, '$entity' );
40        '@phan-var Lexeme $entity';
41
42        /** @var Lexeme $entity */
43
44        foreach ( $this->changeOpForSense as $senseId => $changeOps ) {
45            if ( $entity->getSenses()->getById( new SenseId( $senseId ) ) === null ) {
46                return Result::newError( [
47                    Error::newError(
48                        'Sense does not exist',
49                        null,
50                        'sense-not-found',
51                        [ $senseId ]
52                    ),
53                ] );
54            }
55        }
56
57        return Result::newSuccess();
58    }
59
60    public function apply( EntityDocument $entity, ?Summary $summary = null ) {
61        Assert::parameterType( Lexeme::class, $entity, '$entity' );
62        '@phan-var Lexeme $entity';
63        /** @var Lexeme $entity */
64
65        foreach ( $this->changeOpForSense as $senseId => $changeOp ) {
66            $sense = $entity->getSenses()->getById( new SenseId( $senseId ) );
67            if ( $sense === null ) {
68                throw new ChangeOpApplyException( 'wikibase-validator-sense-not-found' );
69            }
70
71            // Passes summary albeit there is no clear definition how summaries should be combined
72            $changeOp->apply( $sense, $summary );
73        }
74
75        return new DummyChangeOpResult();
76    }
77
78}