Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.12% covered (success)
95.12%
39 / 41
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListsCreateHandler
95.12% covered (success)
95.12%
39 / 41
80.00% covered (warning)
80.00%
4 / 5
6
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%
7 / 7
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
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
2.01
 getParamSettings
100.00% covered (success)
100.00%
15 / 15
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\ReadingListRepository;
7use MediaWiki\Extension\ReadingLists\ReadingListRepositoryException;
8use MediaWiki\Logger\LoggerFactory;
9use MediaWiki\Rest\Handler;
10use MediaWiki\Rest\Response;
11use MediaWiki\Rest\Validator\Validator;
12use MediaWiki\User\CentralId\CentralIdLookup;
13use Psr\Log\LoggerInterface;
14use Wikimedia\ParamValidator\ParamValidator;
15use Wikimedia\ParamValidator\TypeDef\StringDef;
16use Wikimedia\Rdbms\LBFactory;
17
18/**
19 * Handle POST requests to /readinglists/v0/lists
20 *
21 * Creates reading lists
22 */
23class ListsCreateHandler extends Handler {
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(),
59            $this->dbProvider,
60            $this->config,
61            $this->centralIdLookup,
62            $this->logger
63        );
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function validate( Validator $restValidator ) {
70        parent::validate( $restValidator );
71        $this->validateToken();
72    }
73
74    /**
75     * @return array|Response
76     */
77    public function execute() {
78        $result = [];
79        $repository = $this->getRepository();
80        $this->checkAuthority( $this->getAuthority() );
81
82        $validatedBody = $this->getValidatedBody() ?? [];
83        $name = $validatedBody['name'];
84        $description = $validatedBody['description'];
85
86        try {
87            $list = $repository->addList( $name, $description );
88        } catch ( ReadingListRepositoryException $e ) {
89            $this->die( $e->getMessageObject() );
90        }
91        $listData = $this->getListFromRow( $list );
92        $result['id'] = (int)$list->rl_id;
93        $result['list'] = $listData;
94
95        return $this->getResponseFactory()->createJson( $result );
96    }
97
98    /**
99     * @return array[]
100     */
101    public function getParamSettings() {
102        return [
103                'name' => [
104                    self::PARAM_SOURCE => 'body',
105                    ParamValidator::PARAM_TYPE => 'string',
106                    ParamValidator::PARAM_REQUIRED => true,
107                    StringDef::PARAM_MAX_BYTES => ReadingListRepository::$fieldLength['rl_name'],
108                ],
109                'description' => [
110                    self::PARAM_SOURCE => 'body',
111                    ParamValidator::PARAM_TYPE => 'string',
112                    ParamValidator::PARAM_REQUIRED => false,
113                    ParamValidator::PARAM_DEFAULT => '',
114                    StringDef::PARAM_MAX_BYTES => ReadingListRepository::$fieldLength['rl_description'],
115                ]
116            ] + $this->getTokenParamDefinition() + $this->getReadingListsTokenParamDefinition();
117    }
118}