Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ChangeOpFormEdit
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 apply
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 validate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getActions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getChangeOps
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\DataAccess\ChangeOp;
4
5use ValueValidators\Result;
6use Wikibase\DataModel\Entity\EntityDocument;
7use Wikibase\Lexeme\Domain\Model\Form;
8use Wikibase\Lexeme\MediaWiki\Api\Summary\SummaryAggregator;
9use Wikibase\Lib\Summary;
10use Wikibase\Repo\ChangeOp\ChangeOp;
11use Wikibase\Repo\ChangeOp\ChangeOpApplyException;
12use Wikibase\Repo\ChangeOp\DummyChangeOpResult;
13use Wikibase\Repo\Store\EntityPermissionChecker;
14use Wikimedia\Assert\Assert;
15
16/**
17 * TODO: give me some better name
18 * @license GPL-2.0-or-later
19 */
20class ChangeOpFormEdit implements ChangeOp {
21
22    private const SUMMARY_ACTION_AGGREGATE = 'update-form-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( Form::class, $entity, '$entity' );
44        '@phan-var Form $entity';
45
46        /** @var Form $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->getRepresentations()->isEmpty() ) {
58            throw new ChangeOpApplyException(
59                'apierror-wikibaselexeme-form-must-have-at-least-one-representation'
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( Form::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}