Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChangeTagsConstraint
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
6
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
 checkConstraint
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 getLegacyStatus
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\EditPage\Constraint;
22
23use ChangeTags;
24use MediaWiki\Permissions\Authority;
25use StatusValue;
26
27/**
28 * Verify user can add change tags
29 *
30 * @since 1.36
31 * @internal
32 * @author DannyS712
33 */
34class ChangeTagsConstraint implements IEditConstraint {
35
36    private Authority $performer;
37    /** @var array */
38    private $tags;
39    /** @var StatusValue|string */
40    private $result;
41
42    /**
43     * @param Authority $performer
44     * @param string[] $tags
45     */
46    public function __construct(
47        Authority $performer,
48        array $tags
49    ) {
50        $this->performer = $performer;
51        $this->tags = $tags;
52    }
53
54    public function checkConstraint(): string {
55        if ( !$this->tags ) {
56            $this->result = self::CONSTRAINT_PASSED;
57            return self::CONSTRAINT_PASSED;
58        }
59
60        // TODO inject a service once canAddTagsAccompanyingChange is moved to a
61        // service as part of T245964
62        $changeTagStatus = ChangeTags::canAddTagsAccompanyingChange(
63            $this->tags,
64            $this->performer,
65            false
66        );
67
68        if ( $changeTagStatus->isOK() ) {
69            $this->result = self::CONSTRAINT_PASSED;
70            return self::CONSTRAINT_PASSED;
71        }
72
73        $this->result = $changeTagStatus; // The same status object is returned
74        return self::CONSTRAINT_FAILED;
75    }
76
77    public function getLegacyStatus(): StatusValue {
78        if ( $this->result === self::CONSTRAINT_PASSED ) {
79            $statusValue = StatusValue::newGood();
80        } else {
81            $statusValue = $this->result;
82            $statusValue->value = self::AS_CHANGE_TAG_ERROR;
83        }
84        return $statusValue;
85    }
86
87}