Passed
Push — release-11.5.x ( 1520e1...01d568 )
by Rafael
43:11
created

ApiEid::main()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 23
rs 9.7
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Eid;
17
18
use ApacheSolrForTypo3\Solr\Api;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use TYPO3\CMS\Core\Http\Response;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Core\Utility\HttpUtility;
24
25
class ApiEid
26
{
27
    public function main(ServerRequestInterface $request): ResponseInterface
28
    {
29
        $api = GeneralUtility::_GP('api');
30
        $apiKey = trim(GeneralUtility::_GP('apiKey'));
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Core\Utility\G...lUtility::_GP('apiKey') can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        $apiKey = trim(/** @scrutinizer ignore-type */ GeneralUtility::_GP('apiKey'));
Loading history...
31
32
        if (!Api::isValidApiKey($apiKey)) {
33
            header(HttpUtility::HTTP_STATUS_403);
34
            header('Content-Type: application/json; charset=utf-8');
35
            echo json_encode(['errorMessage' => 'Invalid API key']);
36
        } else {
37
            switch ($api) {
38
                case 'siteHash':
39
                    include('SiteHash.php');
40
                    break;
41
42
                default:
43
                    header(HttpUtility::HTTP_STATUS_400);
44
                    header('Content-Type: application/json; charset=utf-8');
45
                    echo json_encode(['errorMessage' => 'You must provide an available API method, e.g. siteHash.']);
46
                    break;
47
            }
48
        }
49
        return new Response();
50
    }
51
}
52