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