Passed
Push — master ( 96ad23...e674df )
by Timo
12:01
created

PageIndexerInitialization   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
c 1
b 0
f 0
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 26 3
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Middleware;
3
4
/***************************************************************
5
 * Copyright notice
6
 *
7
 * (c) 2019 Achim Fritz <[email protected]>
8
 * All rights reserved
9
 *
10
 * This script is part of the TYPO3 project. The TYPO3 project is
11
 * free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * The GNU General Public License can be found at
17
 * http://www.gnu.org/copyleft/gpl.html.
18
 * A copy is found in the textfile GPL.txt and important notices to the license
19
 * from the author is found in LICENSE.txt distributed with these scripts.
20
 *
21
 *
22
 * This script is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU General Public License for more details.
26
 *
27
 * This copyright notice MUST APPEAR in all copies of the script!
28
 ***************************************************************/
29
30
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequest;
31
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequestHandler;
32
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
33
use Psr\Http\Message\ResponseInterface;
34
use Psr\Http\Message\ServerRequestInterface;
35
use Psr\Http\Server\MiddlewareInterface;
36
use Psr\Http\Server\RequestHandlerInterface;
37
use TYPO3\CMS\Core\Http\NullResponse;
38
use TYPO3\CMS\Core\Utility\GeneralUtility;
39
40
/**
41
 * Class PageIndexerInitialization
42
 * @package ApacheSolrForTypo3\Solr\Middleware
43
 */
44
class PageIndexerInitialization implements MiddlewareInterface
45
{
46
47
    /**
48
     * @param ServerRequestInterface $request
49
     * @param RequestHandlerInterface $handler
50
     * @return ResponseInterface
51
     */
52
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
53
    {
54
        if ($request->hasHeader(PageIndexerRequest::SOLR_INDEX_HEADER)) {
55
            // disable TSFE cache for TYPO3 v10
56
            $request = $request->withAttribute('noCache', true);
57
            $jsonEncodedParameters = $request->getHeader(PageIndexerRequest::SOLR_INDEX_HEADER)[0];
58
            $pageIndexerRequestHandler = GeneralUtility::makeInstance(PageIndexerRequestHandler::class, $jsonEncodedParameters);
0 ignored issues
show
Bug introduced by
$jsonEncodedParameters of type string is incompatible with the type array|array<mixed,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

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

58
            $pageIndexerRequestHandler = GeneralUtility::makeInstance(PageIndexerRequestHandler::class, /** @scrutinizer ignore-type */ $jsonEncodedParameters);
Loading history...
59
60
            $pageIndexerRequest = $pageIndexerRequestHandler->getRequest();
61
            if (!$pageIndexerRequest->isAuthenticated()) {
62
                $logger = GeneralUtility::makeInstance(SolrLogManager::class, self::class);
63
                $logger->log(
64
                    SolrLogManager::ERROR,
65
                    'Invalid Index Queue Frontend Request detected!',
66
                    [
67
                        'page indexer request' => (array)$pageIndexerRequest,
68
                        'index queue header' => $jsonEncodedParameters
69
                    ]
70
                );
71
                return new NullResponse('Invalid Index Queue Request!', 403);
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Core\Http\NullResponse::__construct() has too many arguments starting with 'Invalid Index Queue Request!'. ( Ignorable by Annotation )

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

71
                return /** @scrutinizer ignore-call */ new NullResponse('Invalid Index Queue Request!', 403);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72
            }
73
            $pageIndexerRequestHandler->run();
74
75
        }
76
77
        return $handler->handle($request);
78
    }
79
}
80