Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
DeleteGrantIdHandler
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
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
2
 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\WikimediaCampaignEvents\Rest;
6
7use MediaWiki\Extension\CampaignEvents\Event\Store\IEventLookup;
8use MediaWiki\Extension\CampaignEvents\MWEntity\MWAuthorityProxy;
9use MediaWiki\Extension\CampaignEvents\Permissions\PermissionChecker;
10use MediaWiki\Extension\CampaignEvents\Rest\EventIDParamTrait;
11use MediaWiki\Extension\WikimediaCampaignEvents\Grants\GrantsStore;
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 DeleteGrantIdHandler extends SimpleHandler {
20    use EventIDParamTrait;
21    use TokenAwareHandlerTrait;
22
23    private IEventLookup $eventLookup;
24    private PermissionChecker $permissionChecker;
25    private GrantsStore $grantsStore;
26
27    public function __construct(
28        IEventLookup $eventLookup,
29        PermissionChecker $permissionChecker,
30        GrantsStore $grantsStore
31    ) {
32        $this->eventLookup = $eventLookup;
33        $this->permissionChecker = $permissionChecker;
34        $this->grantsStore = $grantsStore;
35    }
36
37    /**
38     * @inheritDoc
39     */
40    public function validate( Validator $restValidator ): void {
41        parent::validate( $restValidator );
42        $this->validateToken();
43    }
44
45    /**
46     * @param int $eventID
47     * @return Response
48     */
49    protected function run( int $eventID ): Response {
50        $registration = $this->getRegistrationOrThrow( $this->eventLookup, $eventID );
51
52        $performer = new MWAuthorityProxy( $this->getAuthority() );
53        if ( !$this->permissionChecker->userCanEditRegistration( $performer, $registration ) ) {
54            throw new LocalizedHttpException(
55                MessageValue::new( 'wikimediacampaignevents-rest-grant-id-edit-permission-denied' ),
56                403
57            );
58        }
59        $this->grantsStore->deleteGrantID( $eventID );
60        return $this->getResponseFactory()->createNoContent();
61    }
62
63    /**
64     * @return array[]
65     */
66    public function getParamSettings(): array {
67        return $this->getIDParamSetting();
68    }
69
70    /**
71     * @inheritDoc
72     */
73    public function getBodyParamSettings(): array {
74        return $this->getTokenParamDefinition();
75    }
76}