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