Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CancelEventRegistrationHandler
94.12% covered (success)
94.12%
16 / 17
80.00% covered (warning)
80.00%
4 / 5
8.01
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%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 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\Extension\CampaignEvents\Event\Store\IEventLookup;
8use MediaWiki\Extension\CampaignEvents\MWEntity\MWAuthorityProxy;
9use MediaWiki\Extension\CampaignEvents\Participants\UnregisterParticipantCommand;
10use MediaWiki\Permissions\PermissionStatus;
11use MediaWiki\Rest\Response;
12use MediaWiki\Rest\SimpleHandler;
13use MediaWiki\Rest\TokenAwareHandlerTrait;
14use MediaWiki\Rest\Validator\JsonBodyValidator;
15use MediaWiki\Rest\Validator\UnsupportedContentTypeBodyValidator;
16use MediaWiki\Rest\Validator\Validator;
17
18class CancelEventRegistrationHandler extends SimpleHandler {
19    use EventIDParamTrait;
20    use TokenAwareHandlerTrait;
21    use FailStatusUtilTrait;
22
23    private IEventLookup $eventLookup;
24    private UnregisterParticipantCommand $unregisterParticipantCommand;
25
26    /**
27     * @param IEventLookup $eventLookup
28     * @param UnregisterParticipantCommand $unregisterParticipantCommand
29     */
30    public function __construct(
31        IEventLookup $eventLookup,
32        UnregisterParticipantCommand $unregisterParticipantCommand
33    ) {
34        $this->eventLookup = $eventLookup;
35        $this->unregisterParticipantCommand = $unregisterParticipantCommand;
36    }
37
38    /**
39     * @inheritDoc
40     */
41    public function validate( Validator $restValidator ): void {
42        parent::validate( $restValidator );
43        $this->validateToken();
44    }
45
46    /**
47     * @param int $eventID
48     * @return Response
49     */
50    protected function run( int $eventID ): Response {
51        $eventRegistration = $this->getRegistrationOrThrow( $this->eventLookup, $eventID );
52        $performer = new MWAuthorityProxy( $this->getAuthority() );
53        $status = $this->unregisterParticipantCommand->unregisterIfAllowed( $eventRegistration, $performer );
54        if ( !$status->isGood() ) {
55            $httptStatus = $status instanceof PermissionStatus ? 403 : 400;
56            $this->exitWithStatus( $status, $httptStatus );
57        }
58        return $this->getResponseFactory()->createJson( [
59            'modified' => $status->getValue()
60        ] );
61    }
62
63    /**
64     * @inheritDoc
65     */
66    public function getParamSettings(): array {
67        return $this->getIDParamSetting();
68    }
69
70    /**
71     * @inheritDoc
72     */
73    public function getBodyValidator( $contentType ) {
74        if ( $contentType !== 'application/json' ) {
75            return new UnsupportedContentTypeBodyValidator( $contentType );
76        }
77
78        return new JsonBodyValidator( $this->getTokenParamDefinition() );
79    }
80}