Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListsCreateBatchHandler
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 postInitSetup
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 validate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 getParamSettings
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
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 Wikimedia\ParamValidator\ParamValidator;
14use Wikimedia\Rdbms\LBFactory;
15
16/**
17 * Handle POST requests to /readinglists/v0/lists/batch
18 *
19 * Creates reading lists
20 */
21class ListsCreateBatchHandler extends Handler {
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(),
57            $this->dbProvider,
58            $this->config,
59            $this->centralIdLookup,
60            $this->logger
61        );
62    }
63
64    /**
65     * @inheritDoc
66     */
67    public function validate( Validator $restValidator ) {
68        parent::validate( $restValidator );
69        $this->validateToken();
70    }
71
72    /**
73     * @return array|Response
74     */
75    public function execute() {
76        $result = [];
77        $repository = $this->getRepository();
78        $this->checkAuthority( $this->getAuthority() );
79
80        $validatedBody = $this->getValidatedBody() ?? [];
81        $batch = $validatedBody['batch'];
82
83        $listData = $listIds = [];
84        foreach ( $this->getBatchOps( $batch ) as $op ) {
85            $description = $op['description'] ?? '';
86            $this->requireAtLeastOneBatchParameter( $op, 'name' );
87            try {
88                $list = $repository->addList( $op['name'], $description );
89            } catch ( ReadingListRepositoryException $e ) {
90                $this->die( $e->getMessageObject() );
91            }
92            $listIds[] = (object)[ 'id' => (int)$list->rl_id ];
93            $listData[] = $this->getListFromRow( $list );
94        }
95        $result['batch'] = $listIds;
96        $result['lists'] = $listData;
97
98        return $this->getResponseFactory()->createJson( $result );
99    }
100
101    /**
102     * @return array[]
103     */
104    public function getParamSettings() {
105        return [
106                // TODO: consider additional validation on "batch", once we have that capability.
107                'batch' => [
108                    self::PARAM_SOURCE => 'body',
109                    ParamValidator::PARAM_TYPE => 'array',
110                    ParamValidator::PARAM_REQUIRED => true,
111                ],
112            ] + $this->getTokenParamDefinition() + $this->getReadingListsTokenParamDefinition();
113    }
114}