Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
ChangeOpSenseEdit | |
100.00% |
17 / 17 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
apply | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
validate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getActions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getChangeOps | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\DataAccess\ChangeOp; |
4 | |
5 | use ValueValidators\Result; |
6 | use Wikibase\DataModel\Entity\EntityDocument; |
7 | use Wikibase\Lexeme\Domain\Model\Sense; |
8 | use Wikibase\Lexeme\MediaWiki\Api\Summary\SummaryAggregator; |
9 | use Wikibase\Lib\Summary; |
10 | use Wikibase\Repo\ChangeOp\ChangeOp; |
11 | use Wikibase\Repo\ChangeOp\ChangeOpApplyException; |
12 | use Wikibase\Repo\ChangeOp\DummyChangeOpResult; |
13 | use Wikibase\Repo\Store\EntityPermissionChecker; |
14 | use Wikimedia\Assert\Assert; |
15 | |
16 | /** |
17 | * TODO: give me some better name |
18 | * @license GPL-2.0-or-later |
19 | */ |
20 | class ChangeOpSenseEdit implements ChangeOp { |
21 | |
22 | private const SUMMARY_ACTION_AGGREGATE = 'update-sense-elements'; |
23 | |
24 | /** |
25 | * @var ChangeOp[] |
26 | */ |
27 | private $changeOps; |
28 | |
29 | /** |
30 | * @var SummaryAggregator |
31 | */ |
32 | private $summaryAggregator; |
33 | |
34 | /** |
35 | * @param ChangeOp[] $changeOps |
36 | */ |
37 | public function __construct( array $changeOps ) { |
38 | $this->changeOps = $changeOps; |
39 | $this->summaryAggregator = new SummaryAggregator( self::SUMMARY_ACTION_AGGREGATE ); |
40 | } |
41 | |
42 | public function apply( EntityDocument $entity, Summary $summary = null ) { |
43 | Assert::parameterType( Sense::class, $entity, '$entity' ); |
44 | '@phan-var Sense $entity'; |
45 | |
46 | /** @var Sense $entity */ |
47 | |
48 | foreach ( $this->changeOps as $changeOp ) { |
49 | $subSummary = new Summary(); |
50 | $changeOp->apply( $entity, $subSummary ); |
51 | |
52 | if ( $summary !== null ) { |
53 | $this->summaryAggregator->overrideSummary( $summary, $subSummary ); |
54 | } |
55 | } |
56 | |
57 | if ( $entity->getGlosses()->isEmpty() ) { |
58 | throw new ChangeOpApplyException( |
59 | 'apierror-wikibaselexeme-sense-must-have-at-least-one-gloss' |
60 | ); |
61 | } |
62 | |
63 | return new DummyChangeOpResult(); |
64 | } |
65 | |
66 | public function validate( EntityDocument $entity ) { |
67 | // TODO: should rather combine the validation results from individual change ops |
68 | // OR: return error on first validation error occurred |
69 | Assert::parameterType( Sense::class, $entity, '$entity' ); |
70 | |
71 | return Result::newSuccess(); |
72 | } |
73 | |
74 | public function getActions() { |
75 | // TODO: should rather combine the actions of individual change ops |
76 | return [ EntityPermissionChecker::ACTION_EDIT ]; |
77 | } |
78 | |
79 | /** |
80 | * Get the array of change operations. |
81 | * |
82 | * @return ChangeOp[] |
83 | */ |
84 | public function getChangeOps() { |
85 | return $this->changeOps; |
86 | } |
87 | |
88 | } |