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

PageIndexerFinisher::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 18
rs 9.7998
cc 2
nc 2
nop 2
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 Psr\Http\Message\ResponseInterface;
33
use Psr\Http\Message\ServerRequestInterface;
34
use Psr\Http\Server\MiddlewareInterface;
35
use Psr\Http\Server\RequestHandlerInterface;
36
use TYPO3\CMS\Core\Http\Response;
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
38
use TYPO3\CMS\Core\Http\Stream;
39
40
/**
41
 * Class PageIndexerFinisher
42
 * @package ApacheSolrForTypo3\Solr\Middleware
43
 */
44
class PageIndexerFinisher 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
        $response = $handler->handle($request);
55
        if ($request->hasHeader(PageIndexerRequest::SOLR_INDEX_HEADER)) {
56
            $pageIndexerRequestHandler = GeneralUtility::makeInstance(PageIndexerRequestHandler::class);
57
            $pageIndexerRequestHandler->shutdown();
58
            $pageIndexResponse = $pageIndexerRequestHandler->getResponse();
59
            $response = new Response();
60
61
            $body = new Stream('php://temp', 'rw');
62
            $content = $pageIndexResponse->getContent();
63
            $body->write($content);
64
            $response = $response
65
                ->withBody($body)
66
                ->withHeader('Content-Length',  (string)strlen($content))
67
                ->withHeader('Content-Type',  'application/json');
68
        }
69
        return $response;
70
    }
71
72
}
73