Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SetupHandler
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 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%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getParamSettings
100.00% covered (success)
100.00%
1 / 1
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\Validator\Validator;
11use MediaWiki\User\CentralId\CentralIdLookup;
12use Psr\Log\LoggerInterface;
13use stdClass;
14use Wikimedia\Rdbms\LBFactory;
15
16/**
17 * Sets up reading lists for the logged-in user
18 */
19class SetupHandler extends Handler {
20    use ReadingListsHandlerTrait;
21    use ReadingListsTokenAwareHandlerTrait;
22
23    private LBFactory $dbProvider;
24
25    private Config $config;
26
27    private CentralIdLookup $centralIdLookup;
28
29    private LoggerInterface $logger;
30
31    /**
32     * @param LBFactory $dbProvider
33     * @param Config $config
34     * @param CentralIdLookup $centralIdLookup
35     */
36    public function __construct(
37        LBFactory $dbProvider,
38        Config $config,
39        CentralIdLookup $centralIdLookup
40    ) {
41        $this->dbProvider = $dbProvider;
42        $this->config = $config;
43        $this->centralIdLookup = $centralIdLookup;
44        $this->logger = LoggerFactory::getInstance( 'readinglists' );
45    }
46
47    /**
48     * Create the repository data access object instance.
49     *
50     * @return void
51     */
52    public function postInitSetup() {
53        $this->repository = $this->createRepository(
54            $this->getAuthority()->getUser(), $this->dbProvider, $this->config, $this->centralIdLookup, $this->logger,
55        );
56    }
57
58    /**
59     * @inheritDoc
60     */
61    public function validate( Validator $restValidator ) {
62        parent::validate( $restValidator );
63        $this->validateToken();
64    }
65
66    /**
67     * @return Response
68     */
69    public function execute() {
70        $this->checkAuthority( $this->getAuthority() );
71
72        try {
73            $this->getRepository()->setupForUser();
74        } catch ( ReadingListRepositoryException $e ) {
75            $this->die( $e->getMessageObject() );
76        }
77
78        // For historical compatibility, the response must be an empty object.
79        // The equivalent Action API endpoint returns the default list, but this list is not
80        // included the equivalent RESTBase response, and that's the contract this endpoint
81        // was created to match.
82        return $this->getResponseFactory()->createJson( new stdClass() );
83    }
84
85    /**
86     * @return array|array[]
87     */
88    public function getParamSettings() {
89        return [] + $this->getReadingListsTokenParamDefinition() + $this->getTokenParamDefinition();
90    }
91}