Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ListsIdHandler
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 doGetLists
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getParamSettings
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\ReadingLists\Rest;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Extension\ReadingLists\Doc\ReadingListRow;
7use MediaWiki\Extension\ReadingLists\ReadingListRepositoryException;
8use MediaWiki\User\CentralId\CentralIdLookup;
9use Wikimedia\ParamValidator\ParamValidator;
10use Wikimedia\ParamValidator\TypeDef\NumericDef;
11use Wikimedia\Rdbms\FakeResultWrapper;
12use Wikimedia\Rdbms\IResultWrapper;
13use Wikimedia\Rdbms\LBFactory;
14
15/**
16 * Handle GET requests to /readinglists/v0/lists/{id}.
17 * This endpoint is for getting lists by id.
18 */
19class ListsIdHandler extends ListsHandler {
20    /**
21     * @param LBFactory $dbProvider
22     * @param Config $config
23     * @param CentralIdLookup $centralIdLookup
24     */
25    public function __construct(
26        LBFactory $dbProvider,
27        Config $config,
28        CentralIdLookup $centralIdLookup
29    ) {
30        parent::__construct( $dbProvider, $config, $centralIdLookup );
31    }
32
33    /**
34     * @return array
35     */
36    public function execute() {
37        // Sorting/pagination is nonsensical for this endpoint, but getLists() expects defaults. So provide happy ones.
38        $params = $this->getValidatedParams();
39        $params['sort'] = 'name';
40        $params['dir'] = 'ascending';
41        $params['next'] = '';
42        $params['limit'] = 1;
43
44        // Because this handler returns a single list, we simplify the returned data
45        $ret = $this->getLists( $params );
46        return $ret['lists'][0] ?? [];
47    }
48
49    /**
50     * Worker function for getting Reading Lists.
51     *
52     * @param array $params all parameters (path and query)
53     * @return IResultWrapper<ReadingListRow>
54     */
55    protected function doGetLists( array $params ) {
56        $repository = $this->getRepository();
57
58        try {
59            // @phan-suppress-next-line PhanTypeMismatchArgument
60            $result = new FakeResultWrapper( [ $repository->selectValidList( $params['id'] ) ] );
61        } catch ( ReadingListRepositoryException $e ) {
62            $this->die( $e->getMessageObject() );
63        }
64
65        return $result;
66    }
67
68    /**
69     * @return array[]
70     */
71    public function getParamSettings() {
72        return [
73                'id' => [
74                    self::PARAM_SOURCE => 'path',
75                    ParamValidator::PARAM_TYPE => 'integer',
76                    ParamValidator::PARAM_REQUIRED => true,
77                    NumericDef::PARAM_MIN => 1,
78                ]
79            ];
80    }
81}