Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.29% covered (success)
94.29%
33 / 35
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListOrganizersHandler
94.29% covered (success)
94.29%
33 / 35
66.67% covered (warning)
66.67%
2 / 3
5.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 run
90.48% covered (success)
90.48%
19 / 21
0.00% covered (danger)
0.00%
0 / 1
3.01
 getParamSettings
100.00% covered (success)
100.00%
9 / 9
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\Extension\CampaignEvents\Event\Store\IEventLookup;
8use MediaWiki\Extension\CampaignEvents\MWEntity\CampaignsCentralUserLookup;
9use MediaWiki\Extension\CampaignEvents\MWEntity\CentralUserNotFoundException;
10use MediaWiki\Extension\CampaignEvents\MWEntity\HiddenCentralUserException;
11use MediaWiki\Extension\CampaignEvents\MWEntity\UserLinker;
12use MediaWiki\Extension\CampaignEvents\Organizers\OrganizersStore;
13use MediaWiki\Extension\CampaignEvents\Organizers\RoleFormatter;
14use MediaWiki\Rest\Response;
15use MediaWiki\Rest\SimpleHandler;
16use Wikimedia\ParamValidator\ParamValidator;
17
18class ListOrganizersHandler extends SimpleHandler {
19    use EventIDParamTrait;
20
21    // TODO: Implement proper pagination (T305389)
22    private const RES_LIMIT = 11;
23
24    private IEventLookup $eventLookup;
25    private OrganizersStore $organizersStore;
26    private RoleFormatter $roleFormatter;
27    private CampaignsCentralUserLookup $centralUserLookup;
28    private UserLinker $userLinker;
29
30    /**
31     * @param IEventLookup $eventLookup
32     * @param OrganizersStore $organizersStore
33     * @param RoleFormatter $roleFormatter
34     * @param CampaignsCentralUserLookup $centralUserLookup
35     * @param UserLinker $userLinker
36     */
37    public function __construct(
38        IEventLookup $eventLookup,
39        OrganizersStore $organizersStore,
40        RoleFormatter $roleFormatter,
41        CampaignsCentralUserLookup $centralUserLookup,
42        UserLinker $userLinker
43    ) {
44        $this->eventLookup = $eventLookup;
45        $this->organizersStore = $organizersStore;
46        $this->roleFormatter = $roleFormatter;
47        $this->centralUserLookup = $centralUserLookup;
48        $this->userLinker = $userLinker;
49    }
50
51    /**
52     * @param int $eventID
53     * @return Response
54     */
55    protected function run( int $eventID ): Response {
56        $this->getRegistrationOrThrow( $this->eventLookup, $eventID );
57
58        $params = $this->getValidatedParams();
59        $organizers = $this->organizersStore->getEventOrganizers(
60            $eventID,
61            self::RES_LIMIT,
62            $params['last_organizer_id']
63        );
64        $respVal = [];
65        foreach ( $organizers as $organizer ) {
66            $user = $organizer->getUser();
67            try {
68                $userName = $this->centralUserLookup->getUserName( $user );
69            } catch ( CentralUserNotFoundException | HiddenCentralUserException $_ ) {
70                continue;
71            }
72            $respVal[] = [
73                'organizer_id' => $organizer->getOrganizerID(),
74                'user_id' => $user->getCentralID(),
75                'user_name' => $userName,
76                // TODO Should these be localized? It doesn't seem possible right now anyway (T269492)
77                'roles' => array_map( [ $this->roleFormatter, 'getDebugName' ], $organizer->getRoles() ),
78                'user_page' => $this->userLinker->getUserPagePath( $user ),
79            ];
80        }
81        return $this->getResponseFactory()->createJson( $respVal );
82    }
83
84    /**
85     * @inheritDoc
86     */
87    public function getParamSettings(): array {
88        return array_merge(
89            $this->getIDParamSetting(),
90            [
91                'last_organizer_id' => [
92                    static::PARAM_SOURCE => 'query',
93                    ParamValidator::PARAM_TYPE => 'integer'
94                ],
95            ]
96        );
97    }
98}