Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ListsEntriesCreateHandler
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
5 / 5
5
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
 run
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getParamSettings
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\ReadingLists\Rest;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Logger\LoggerFactory;
7use MediaWiki\Rest\Handler;
8use MediaWiki\Rest\SimpleHandler;
9use MediaWiki\Rest\Validator\Validator;
10use MediaWiki\User\CentralId\CentralIdLookup;
11use Psr\Log\LoggerInterface;
12use Wikimedia\ParamValidator\ParamValidator;
13use Wikimedia\ParamValidator\TypeDef\NumericDef;
14use Wikimedia\Rdbms\LBFactory;
15
16/**
17 * Handle POST requests to /readinglists/v0/lists/{id}/entries
18 *
19 * Gets reading list entries
20 */
21class ListsEntriesCreateHandler extends SimpleHandler {
22    use ReadingListsHandlerTrait;
23    use ReadingListsTokenAwareHandlerTrait;
24
25    private LBFactory $dbProvider;
26
27    private Config $config;
28
29    private CentralIdLookup $centralIdLookup;
30
31    private LoggerInterface $logger;
32
33    /**
34     * @param LBFactory $dbProvider
35     * @param Config $config
36     * @param CentralIdLookup $centralIdLookup
37     */
38    public function __construct(
39        LBFactory $dbProvider,
40        Config $config,
41        CentralIdLookup $centralIdLookup
42    ) {
43        $this->dbProvider = $dbProvider;
44        $this->config = $config;
45        $this->centralIdLookup = $centralIdLookup;
46        $this->logger = LoggerFactory::getInstance( 'readinglists' );
47    }
48
49    /**
50     * Create the repository data access object instance.
51     *
52     * @return void
53     */
54    public function postInitSetup() {
55        $this->repository = $this->createRepository(
56            $this->getAuthority()->getUser(), $this->dbProvider, $this->config, $this->centralIdLookup, $this->logger
57        );
58    }
59
60    /**
61     * @inheritDoc
62     */
63    public function validate( Validator $restValidator ) {
64        parent::validate( $restValidator );
65        $this->validateToken();
66    }
67
68    /**
69     * @param int $id the list to update
70     * @return array
71     */
72    public function run( int $id ) {
73        $this->checkAuthority( $this->getAuthority() );
74
75        $validatedBody = $this->getValidatedBody() ?? [];
76        $project = $validatedBody['project'];
77        $title = $validatedBody['title'];
78
79        return $this->createListEntry( $id, $project, $title, $this->getRepository() );
80    }
81
82    /**
83     * @return array[]
84     */
85    public function getParamSettings() {
86        return [
87            'id' => [
88                ParamValidator::PARAM_TYPE => 'integer',
89                ParamValidator::PARAM_REQUIRED => true,
90                Handler::PARAM_SOURCE => 'path',
91                NumericDef::PARAM_MIN => 1,
92            ],
93            'project' => [
94                self::PARAM_SOURCE => 'body',
95                ParamValidator::PARAM_TYPE => 'string',
96                ParamValidator::PARAM_REQUIRED => true,
97            ],
98            'title' => [
99                self::PARAM_SOURCE => 'body',
100                ParamValidator::PARAM_TYPE => 'string',
101                ParamValidator::PARAM_REQUIRED => true,
102            ]
103        ] + $this->getTokenParamDefinition() + $this->getReadingListsTokenParamDefinition();
104    }
105}