Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
DeleteEventRegistrationHandler
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
5 / 5
9
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
 run
100.00% covered (success)
100.00%
19 / 19
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%
1 / 1
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\DeleteEventCommand;
9use MediaWiki\Extension\CampaignEvents\Event\Store\IEventLookup;
10use MediaWiki\Extension\CampaignEvents\MWEntity\MWAuthorityProxy;
11use MediaWiki\Permissions\PermissionStatus;
12use MediaWiki\Rest\LocalizedHttpException;
13use MediaWiki\Rest\Response;
14use MediaWiki\Rest\SimpleHandler;
15use MediaWiki\Rest\TokenAwareHandlerTrait;
16use MediaWiki\Rest\Validator\Validator;
17use Wikimedia\Message\MessageValue;
18
19class DeleteEventRegistrationHandler extends SimpleHandler {
20    use TokenAwareHandlerTrait;
21    use EventIDParamTrait;
22    use FailStatusUtilTrait;
23
24    private IEventLookup $eventLookup;
25    private DeleteEventCommand $deleteEventCommand;
26
27    /**
28     * @param IEventLookup $eventLookup
29     * @param DeleteEventCommand $deleteEventCommand
30     */
31    public function __construct(
32        IEventLookup $eventLookup,
33        DeleteEventCommand $deleteEventCommand
34    ) {
35        $this->eventLookup = $eventLookup;
36        $this->deleteEventCommand = $deleteEventCommand;
37    }
38
39    /**
40     * @inheritDoc
41     */
42    public function validate( Validator $restValidator ): void {
43        parent::validate( $restValidator );
44        $this->validateToken();
45    }
46
47    /**
48     * @param int $id
49     * @return Response
50     */
51    public function run( int $id ): Response {
52        $registration = $this->getRegistrationOrThrow( $this->eventLookup, $id );
53        if ( $registration->getDeletionTimestamp() !== null ) {
54            throw new LocalizedHttpException(
55                new MessageValue( 'campaignevents-rest-delete-already-deleted' ),
56                404
57            );
58        }
59
60        $wikiID = $registration->getPage()->getWikiId();
61        if ( $wikiID !== WikiAwareEntity::LOCAL ) {
62            throw new LocalizedHttpException(
63                MessageValue::new( 'campaignevents-rest-delete-event-nonlocal-error-message' )
64                    ->params( $wikiID ),
65                400
66            );
67        }
68
69        $performer = new MWAuthorityProxy( $this->getAuthority() );
70        $status = $this->deleteEventCommand->deleteIfAllowed( $registration, $performer );
71        if ( !$status->isGood() ) {
72            $httptStatus = $status instanceof PermissionStatus ? 403 : 400;
73            $this->exitWithStatus( $status, $httptStatus );
74        }
75
76        return $this->getResponseFactory()->createNoContent();
77    }
78
79    /**
80     * @inheritDoc
81     */
82    public function getParamSettings(): array {
83        return $this->getIDParamSetting();
84    }
85
86    /**
87     * @inheritDoc
88     */
89    public function getBodyParamSettings(): array {
90        return $this->getTokenParamDefinition();
91    }
92}