Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.67% |
29 / 30 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
ChangeOpRemoveSense | |
96.67% |
29 / 30 |
|
75.00% |
3 / 4 |
7 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
apply | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
2.00 | |||
updateSummary | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
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\ChangeOpBase; |
12 | use Wikibase\Repo\ChangeOp\DummyChangeOpResult; |
13 | use Wikimedia\Assert\Assert; |
14 | |
15 | /** |
16 | * @license GPL-2.0-or-later |
17 | */ |
18 | class ChangeOpRemoveSense extends ChangeOpBase { |
19 | |
20 | private const SUMMARY_ACTION_REMOVE = 'remove-sense'; |
21 | |
22 | /** |
23 | * @var SenseId |
24 | */ |
25 | private $senseId; |
26 | |
27 | /** |
28 | * @param SenseId $senseId |
29 | */ |
30 | public function __construct( SenseId $senseId ) { |
31 | $this->senseId = $senseId; |
32 | } |
33 | |
34 | public function validate( EntityDocument $entity ) { |
35 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
36 | '@phan-var Lexeme $entity'; |
37 | |
38 | /** @var Lexeme $entity */ |
39 | if ( $entity->getSenses()->getById( $this->senseId ) === null ) { |
40 | return Result::newError( [ |
41 | Error::newError( |
42 | 'Sense does not exist', |
43 | null, |
44 | 'sense-not-found', |
45 | [ $this->senseId->getSerialization() ] |
46 | ), |
47 | ] ); |
48 | } |
49 | |
50 | return Result::newSuccess(); |
51 | } |
52 | |
53 | public function apply( EntityDocument $entity, ?Summary $summary = null ) { |
54 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
55 | '@phan-var Lexeme $entity'; |
56 | |
57 | /** @var Lexeme $entity */ |
58 | |
59 | $sense = $entity->getSense( $this->senseId ); |
60 | $entity->removeSense( $this->senseId ); |
61 | |
62 | if ( $sense->getGlosses()->count() === 1 ) { |
63 | $array = $sense->getGlosses()->toTextArray(); |
64 | reset( $array ); |
65 | $language = key( $array ); |
66 | } else { |
67 | $language = null; |
68 | } |
69 | |
70 | $this->updateSummary( |
71 | $summary, |
72 | self::SUMMARY_ACTION_REMOVE, |
73 | $language, |
74 | array_values( $sense->getGlosses()->toTextArray() ) |
75 | ); |
76 | |
77 | return new DummyChangeOpResult(); |
78 | } |
79 | |
80 | protected function updateSummary( ?Summary $summary, $action, $language = '', $args = '' ) { |
81 | parent::updateSummary( $summary, $action, $language, $args ); |
82 | if ( $summary !== null ) { |
83 | $summary->addAutoCommentArgs( [ $this->senseId->getSerialization() ] ); |
84 | } |
85 | } |
86 | |
87 | } |