Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.48% |
19 / 21 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ChangeOpsSensesEdit | |
90.48% |
19 / 21 |
|
50.00% |
2 / 4 |
8.06 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getActions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validate | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| apply | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikibase\Lexeme\DataAccess\ChangeOp; |
| 4 | |
| 5 | use ValueValidators\Error; |
| 6 | use ValueValidators\Result; |
| 7 | use Wikibase\DataModel\Entity\EntityDocument; |
| 8 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
| 9 | use Wikibase\Lexeme\Domain\Model\SenseId; |
| 10 | use Wikibase\Lib\Summary; |
| 11 | use Wikibase\Repo\ChangeOp\ChangeOp; |
| 12 | use Wikibase\Repo\ChangeOp\ChangeOpApplyException; |
| 13 | use Wikibase\Repo\ChangeOp\DummyChangeOpResult; |
| 14 | use 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 | */ |
| 22 | class 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 | /** @inheritDoc */ |
| 35 | public function getActions() { |
| 36 | return []; |
| 37 | } |
| 38 | |
| 39 | /** @inheritDoc */ |
| 40 | public function validate( EntityDocument $entity ) { |
| 41 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
| 42 | '@phan-var Lexeme $entity'; |
| 43 | |
| 44 | /** @var Lexeme $entity */ |
| 45 | |
| 46 | foreach ( $this->changeOpForSense as $senseId => $changeOps ) { |
| 47 | if ( $entity->getSenses()->getById( new SenseId( $senseId ) ) === null ) { |
| 48 | return Result::newError( [ |
| 49 | Error::newError( |
| 50 | 'Sense does not exist', |
| 51 | null, |
| 52 | 'sense-not-found', |
| 53 | [ $senseId ] |
| 54 | ), |
| 55 | ] ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return Result::newSuccess(); |
| 60 | } |
| 61 | |
| 62 | /** @inheritDoc */ |
| 63 | public function apply( EntityDocument $entity, ?Summary $summary = null ) { |
| 64 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
| 65 | '@phan-var Lexeme $entity'; |
| 66 | /** @var Lexeme $entity */ |
| 67 | |
| 68 | foreach ( $this->changeOpForSense as $senseId => $changeOp ) { |
| 69 | $sense = $entity->getSenses()->getById( new SenseId( $senseId ) ); |
| 70 | if ( $sense === null ) { |
| 71 | throw new ChangeOpApplyException( 'wikibase-validator-sense-not-found' ); |
| 72 | } |
| 73 | |
| 74 | // Passes summary albeit there is no clear definition how summaries should be combined |
| 75 | $changeOp->apply( $sense, $summary ); |
| 76 | } |
| 77 | |
| 78 | return new DummyChangeOpResult(); |
| 79 | } |
| 80 | |
| 81 | } |