Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.30% covered (success)
91.30%
21 / 23
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListsDeleteHandler
91.30% covered (success)
91.30%
21 / 23
80.00% covered (warning)
80.00%
4 / 5
7.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 postInitSetup
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 run
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
2.26
 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\ReadingListRepositoryException;
7use MediaWiki\Logger\LoggerFactory;
8use MediaWiki\Rest\Handler;
9use MediaWiki\Rest\LocalizedHttpException;
10use MediaWiki\Rest\Response;
11use MediaWiki\Rest\SimpleHandler;
12use MediaWiki\Rest\Validator\Validator;
13use MediaWiki\User\CentralId\CentralIdLookup;
14use Psr\Log\LoggerInterface;
15use stdClass;
16use Wikimedia\ParamValidator\ParamValidator;
17use Wikimedia\ParamValidator\TypeDef\NumericDef;
18use Wikimedia\Rdbms\LBFactory;
19
20/**
21 * Handle DELETE requests to /{module}/lists/{id}
22 *
23 * Deletes reading lists
24 */
25class ListsDeleteHandler extends SimpleHandler {
26    use ReadingListsHandlerTrait;
27
28    private LBFactory $dbProvider;
29
30    private Config $config;
31
32    private CentralIdLookup $centralIdLookup;
33
34    private LoggerInterface $logger;
35
36    /**
37     * @param LBFactory $dbProvider
38     * @param Config $config
39     * @param CentralIdLookup $centralIdLookup
40     */
41    public function __construct(
42        LBFactory $dbProvider,
43        Config $config,
44        CentralIdLookup $centralIdLookup
45    ) {
46        $this->dbProvider = $dbProvider;
47        $this->config = $config;
48        $this->centralIdLookup = $centralIdLookup;
49        $this->logger = LoggerFactory::getInstance( 'readinglists' );
50    }
51
52    /**
53     * Create the repository data access object instance.
54     *
55     * @return void
56     */
57    public function postInitSetup() {
58        $this->repository = $this->createRepository(
59            $this->getAuthority()->getUser(), $this->dbProvider, $this->config, $this->centralIdLookup, $this->logger
60        );
61    }
62
63    /**
64     * @inheritDoc
65     */
66    public function validate( Validator $restValidator ) {
67        try {
68            // We intentionally do not require a csrf token, to match RESTBase
69            parent::validate( $restValidator );
70        } catch ( LocalizedHttpException $e ) {
71            // Add fields expected by WMF mobile apps
72            $this->die( $e->getMessageValue(), [], $e->getCode(), $e->getErrorData() );
73        }
74    }
75
76    /**
77     * @param int $id the list to update
78     * @return Response
79     */
80    public function run( int $id ) {
81        $this->checkAuthority( $this->getAuthority() );
82
83        try {
84            $this->getRepository()->deleteList( $id );
85        } catch ( ReadingListRepositoryException $e ) {
86            $this->die( $e->getMessageObject() );
87        }
88
89        // Return value is expected to be an empty json object
90        return $this->getResponseFactory()->createJson( new stdClass );
91    }
92
93    /**
94     * @return array[]
95     */
96    public function getParamSettings() {
97        return [
98            'id' => [
99                ParamValidator::PARAM_TYPE => 'integer',
100                ParamValidator::PARAM_REQUIRED => true,
101                NumericDef::PARAM_MIN => 1,
102                Handler::PARAM_SOURCE => 'path',
103            ],
104        ];
105    }
106}