Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.87% covered (success)
94.87%
37 / 39
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListsUpdateHandler
94.87% covered (success)
94.87%
37 / 39
80.00% covered (warning)
80.00%
4 / 5
6.00
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%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 run
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
2.03
 getParamSettings
100.00% covered (success)
100.00%
20 / 20
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\SimpleHandler;
11use MediaWiki\Rest\Validator\Validator;
12use MediaWiki\User\CentralId\CentralIdLookup;
13use Psr\Log\LoggerInterface;
14use Wikimedia\ParamValidator\ParamValidator;
15use Wikimedia\ParamValidator\TypeDef\NumericDef;
16use Wikimedia\ParamValidator\TypeDef\StringDef;
17use Wikimedia\Rdbms\LBFactory;
18
19/**
20 * Handle PUT requests to /readinglists/v0/lists/{id}
21 *
22 * Updates reading lists
23 */
24class ListsUpdateHandler extends SimpleHandler {
25    use ReadingListsHandlerTrait;
26    use ReadingListsTokenAwareHandlerTrait;
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        parent::validate( $restValidator );
68        $this->validateToken();
69    }
70
71    /**
72     * @param int $id the list to update
73     * @return array
74     */
75    public function run( int $id ) {
76        $this->checkAuthority( $this->getAuthority() );
77
78        $params = $this->getValidatedBody() ?? [];
79        $this->requireAtLeastOneParameter( $params, 'name', 'description' );
80        try {
81            $list = $this->getRepository()->updateList( $id, $params['name'], $params['description'] );
82        } catch ( ReadingListRepositoryException $e ) {
83            $this->die( $e->getMessageObject() );
84        }
85
86        $listData = $this->getListFromRow( $list );
87        return [
88            'list' => $listData,
89        ];
90    }
91
92    /**
93     * @return array[]
94     */
95    public function getParamSettings() {
96        return [
97            'id' => [
98                ParamValidator::PARAM_TYPE => 'integer',
99                ParamValidator::PARAM_REQUIRED => true,
100                NumericDef::PARAM_MIN => 1,
101                Handler::PARAM_SOURCE => 'path',
102            ],
103            'name' => [
104                self::PARAM_SOURCE => 'body',
105                ParamValidator::PARAM_TYPE => 'string',
106                ParamValidator::PARAM_REQUIRED => false,
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                StringDef::PARAM_MAX_BYTES => ReadingListRepository::$fieldLength['rl_description'],
114            ]
115        ] + $this->getTokenParamDefinition() + $this->getReadingListsTokenParamDefinition();
116    }
117}