Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
24 / 24 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ChangeOpSenseAdd | |
100.00% |
24 / 24 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
apply | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\DataAccess\ChangeOp; |
4 | |
5 | use ValueValidators\Result; |
6 | use Wikibase\DataModel\Entity\EntityDocument; |
7 | use Wikibase\DataModel\Services\Statement\GuidGenerator; |
8 | use Wikibase\Lexeme\Domain\DummyObjects\BlankSense; |
9 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
10 | use Wikibase\Lib\Summary; |
11 | use Wikibase\Repo\ChangeOp\ChangeOp; |
12 | use Wikibase\Repo\ChangeOp\ChangeOpBase; |
13 | use Wikibase\Repo\ChangeOp\DummyChangeOpResult; |
14 | use Wikimedia\Assert\Assert; |
15 | |
16 | /** |
17 | * @license GPL-2.0-or-later |
18 | */ |
19 | class ChangeOpSenseAdd extends ChangeOpBase { |
20 | |
21 | private const SUMMARY_ACTION_ADD = 'add-sense'; |
22 | |
23 | /** |
24 | * @var ChangeOp |
25 | */ |
26 | private $changeOpSense; |
27 | |
28 | /** |
29 | * @var GuidGenerator |
30 | */ |
31 | private $guidGenerator; |
32 | |
33 | /** |
34 | * @param ChangeOp $changeOpSense |
35 | */ |
36 | public function __construct( ChangeOp $changeOpSense, GuidGenerator $guidGenerator ) { |
37 | $this->changeOpSense = $changeOpSense; |
38 | $this->guidGenerator = $guidGenerator; |
39 | } |
40 | |
41 | public function validate( EntityDocument $entity ) { |
42 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
43 | |
44 | return Result::newSuccess(); |
45 | } |
46 | |
47 | public function apply( EntityDocument $entity, ?Summary $summary = null ) { |
48 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
49 | '@phan-var Lexeme $entity'; |
50 | |
51 | /** @var Lexeme $entity */ |
52 | |
53 | $sense = new BlankSense(); |
54 | |
55 | $entity->addOrUpdateSense( $sense ); |
56 | $this->changeOpSense->apply( $sense, null ); |
57 | |
58 | // update statements to have a suitable guid now that the new sense id is known |
59 | // fixme This should find a new home in a more central place, maybe StatementList |
60 | foreach ( $sense->getStatements() as $statement ) { |
61 | $statement->setGuid( $this->guidGenerator->newGuid( $sense->getId() ) ); |
62 | } |
63 | |
64 | if ( $sense->getGlosses()->count() === 1 ) { |
65 | $array = $sense->getGlosses()->toTextArray(); |
66 | reset( $array ); |
67 | $language = key( $array ); |
68 | } else { |
69 | $language = null; |
70 | } |
71 | |
72 | if ( $summary !== null ) { |
73 | // TODO: consistently do not extend ChangeOpBase? |
74 | $this->updateSummary( |
75 | $summary, |
76 | self::SUMMARY_ACTION_ADD, |
77 | $language, |
78 | array_values( $sense->getGlosses()->toTextArray() ) |
79 | ); |
80 | // TODO: use SenseId not string? |
81 | $summary->addAutoCommentArgs( $sense->getId()->getSerialization() ); |
82 | } |
83 | |
84 | return new DummyChangeOpResult(); |
85 | } |
86 | |
87 | } |