Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
SetOrganizersHandler
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
6 / 6
11
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
 validate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 validateEventWiki
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 run
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
 getParamSettings
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBodyParamSettings
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare( strict_types=1 );
4
5namespace MediaWiki\Extension\CampaignEvents\Rest;
6
7use MediaWiki\DAO\WikiAwareEntity;
8use MediaWiki\Extension\CampaignEvents\Event\EditEventCommand;
9use MediaWiki\Extension\CampaignEvents\Event\ExistingEventRegistration;
10use MediaWiki\Extension\CampaignEvents\Event\Store\IEventLookup;
11use MediaWiki\Extension\CampaignEvents\MWEntity\MWAuthorityProxy;
12use MediaWiki\ParamValidator\TypeDef\UserDef;
13use MediaWiki\Permissions\PermissionStatus;
14use MediaWiki\Rest\LocalizedHttpException;
15use MediaWiki\Rest\Response;
16use MediaWiki\Rest\SimpleHandler;
17use MediaWiki\Rest\TokenAwareHandlerTrait;
18use MediaWiki\Rest\Validator\Validator;
19use Wikimedia\Message\MessageValue;
20use Wikimedia\ParamValidator\ParamValidator;
21
22class SetOrganizersHandler extends SimpleHandler {
23    use EventIDParamTrait;
24    use FailStatusUtilTrait;
25    use TokenAwareHandlerTrait;
26
27    private IEventLookup $eventLookup;
28    private EditEventCommand $editEventCommand;
29
30    /**
31     * @param IEventLookup $eventLookup
32     * @param EditEventCommand $editEventCommand
33     */
34    public function __construct(
35        IEventLookup $eventLookup,
36        EditEventCommand $editEventCommand
37    ) {
38        $this->eventLookup = $eventLookup;
39        $this->editEventCommand = $editEventCommand;
40    }
41
42    /**
43     * @inheritDoc
44     */
45    public function validate( Validator $restValidator ): void {
46        parent::validate( $restValidator );
47        $this->validateToken();
48    }
49
50    /**
51     * @param ExistingEventRegistration $event
52     */
53    private function validateEventWiki( ExistingEventRegistration $event ): void {
54        $wikiID = $event->getPage()->getWikiId();
55        if ( $wikiID !== WikiAwareEntity::LOCAL ) {
56            throw new LocalizedHttpException(
57                MessageValue::new( 'campaignevents-rest-set-organizers-nonlocal-error-message' )
58                    ->params( $wikiID ),
59                400
60            );
61        }
62    }
63
64    /**
65     * @param int $eventID
66     * @return Response
67     */
68    protected function run( int $eventID ): Response {
69        $event = $this->getRegistrationOrThrow( $this->eventLookup, $eventID );
70        $this->validateEventWiki( $event );
71        $body = $this->getValidatedBody() ?? [];
72        $organizers = $body['organizer_usernames'];
73        if ( !is_array( $organizers ) || !$organizers ) {
74            throw new LocalizedHttpException(
75                MessageValue::new( 'campaignevents-rest-set-organizers-empty-list' ),
76                400
77            );
78        }
79
80        $performer = new MWAuthorityProxy( $this->getAuthority() );
81        $saveStatus = $this->editEventCommand->doEditIfAllowed( $event, $performer, $organizers );
82        // Note that no warnings (e.g., from tracking tools) are expected here
83        if ( !$saveStatus->isGood() ) {
84            $httptStatus = $saveStatus instanceof PermissionStatus ? 403 : 400;
85            $this->exitWithStatus( $saveStatus, $httptStatus );
86        }
87
88        return $this->getResponseFactory()->createNoContent();
89    }
90
91    /**
92     * @inheritDoc
93     */
94    public function getParamSettings(): array {
95        return $this->getIDParamSetting();
96    }
97
98    /**
99     * @inheritDoc
100     */
101    public function getBodyParamSettings(): array {
102        return [
103            'organizer_usernames' => [
104                ParamValidator::PARAM_REQUIRED => true,
105                ParamValidator::PARAM_TYPE => 'user',
106                UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
107                static::PARAM_SOURCE => 'body',
108                ParamValidator::PARAM_ISMULTI => true,
109                ParamValidator::PARAM_DEFAULT => [],
110            ],
111        ] + $this->getTokenParamDefinition();
112    }
113
114}