Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
GetGrantIdHandler
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
5
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
 run
100.00% covered (success)
100.00%
13 / 13
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
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 Wikimedia\Message\MessageValue;
16
17class GetGrantIdHandler extends SimpleHandler {
18    use EventIDParamTrait;
19
20    /** @var IEventLookup */
21    private IEventLookup $eventLookup;
22    private PermissionChecker $permissionChecker;
23    private GrantsStore $grantsStore;
24
25    /**
26     * @param IEventLookup $eventLookup
27     * @param PermissionChecker $permissionChecker
28     * @param GrantsStore $grantsStore
29     */
30    public function __construct(
31        IEventLookup $eventLookup,
32        PermissionChecker $permissionChecker,
33        GrantsStore $grantsStore
34    ) {
35        $this->eventLookup = $eventLookup;
36        $this->permissionChecker = $permissionChecker;
37        $this->grantsStore = $grantsStore;
38    }
39
40    /**
41     * @param int $eventID
42     * @return Response
43     */
44    protected function run( int $eventID ): Response {
45        $registration = $this->getRegistrationOrThrow( $this->eventLookup, $eventID );
46
47        $performer = new MWAuthorityProxy( $this->getAuthority() );
48        if ( !$this->permissionChecker->userCanEditRegistration( $performer, $registration ) ) {
49            throw new LocalizedHttpException(
50                MessageValue::new( 'wikimediacampaignevents-rest-grant-id-get-permission-denied' ),
51                403
52            );
53        }
54
55        $grantID = $this->grantsStore->getGrantID( $eventID );
56        if ( $grantID === null ) {
57            return $this->getResponseFactory()->createHttpError( 404 );
58        }
59        return $this->getResponseFactory()->createJson( [
60            'grant_id' => $grantID
61        ] );
62    }
63
64    /**
65     * @return array[]
66     */
67    public function getParamSettings(): array {
68        return $this->getIDParamSetting();
69    }
70}