Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
23 / 24
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChangeOpRepresentation
95.83% covered (success)
95.83%
23 / 24
80.00% covered (warning)
80.00%
4 / 5
8
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 apply
100.00% covered (success)
100.00%
4 / 4
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
 updateSummary
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
4.00
1<?php
2
3namespace Wikibase\Lexeme\DataAccess\ChangeOp;
4
5use ValueValidators\Result;
6use Wikibase\DataModel\Entity\EntityDocument;
7use Wikibase\DataModel\Term\Term;
8use Wikibase\Lexeme\Domain\Model\Form;
9use Wikibase\Lib\Summary;
10use Wikibase\Repo\ChangeOp\ChangeOp;
11use Wikibase\Repo\ChangeOp\DummyChangeOpResult;
12use Wikibase\Repo\Store\EntityPermissionChecker;
13use Wikimedia\Assert\Assert;
14
15/**
16 * @license GPL-2.0-or-later
17 */
18class ChangeOpRepresentation implements ChangeOp {
19
20    private const SUMMARY_ACTION_ADD = 'add-form-representations';
21    private const SUMMARY_ACTION_SET = 'set-form-representations';
22
23    /**
24     * @var Term
25     */
26    private $representation;
27
28    public function __construct( Term $representation ) {
29        $this->representation = $representation;
30    }
31
32    public function validate( EntityDocument $entity ) {
33        Assert::parameterType( Form::class, $entity, '$entity' );
34
35        return Result::newSuccess();
36    }
37
38    public function apply( EntityDocument $entity, Summary $summary = null ) {
39        Assert::parameterType( Form::class, $entity, '$entity' );
40        '@phan-var Form $entity';
41
42        /** @var Form $entity */
43
44        $this->updateSummary( $entity, $summary );
45
46        $entity->getRepresentations()->setTerm( $this->representation );
47
48        return new DummyChangeOpResult();
49    }
50
51    public function getActions() {
52        return [ EntityPermissionChecker::ACTION_EDIT ];
53    }
54
55    private function updateSummary( Form $form, Summary $summary = null ) {
56        if ( $summary === null ) {
57            return;
58        }
59
60        // no op to summarize if term existed in identical fashion
61        if ( $form->getRepresentations()->hasTerm( $this->representation ) ) {
62            return;
63        }
64
65        $languageCode = $this->representation->getLanguageCode();
66        $representation = $this->representation->getText();
67        $summary->setAction(
68            $form->getRepresentations()->hasTermForLanguage( $languageCode ) ?
69            self::SUMMARY_ACTION_SET :
70            self::SUMMARY_ACTION_ADD
71        );
72        $summary->setLanguage( $languageCode );
73        $summary->addAutoCommentArgs( [
74            $form->getId()->getSerialization(), // TODO: use FormId not string?
75        ] );
76        $summary->addAutoSummaryArgs( [ $languageCode => $representation ] );
77    }
78
79}