Passed
Push — issue/729 ( 6c1c7d )
by Tomas Norre
14:47
created

ContentFinisher::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Middleware;
6
7
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use TYPO3\CMS\Core\Context\Context;
13
use TYPO3\CMS\Core\Http\Response;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
16
class ContentFinisher implements MiddlewareInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $headerName = 'X-T3CRAWLER';
22
23
    /**
24
     * @var Context
25
     */
26
    protected $context;
27
28
    public function __construct(?Context $context = null)
29
    {
30
        $this->context = $context ?? GeneralUtility::makeInstance(Context::class);
31
    }
32
33
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
34
    {
35
        $output = $handler->handle($request);
36
37
        $crawlerInformation = $request->getHeaderLine($this->headerName) ?? null;
38
        if (empty($crawlerInformation)) {
39
            return $output;
40
        }
41
42
        // Output log data for crawler (serialized content):
43
        $content = serialize($GLOBALS['TSFE']->applicationData['tx_crawler']);
44
        $response = new Response();
45
        $response->getBody()->write($content);
46
47
        return $response;
48
    }
49
50
}
51