Passed
Push — typo3v9 ( 580ae6...b5443c )
by Tomas Norre
05:36
created

CrawlerInitialization::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Middleware;
6
7
/*
8
 * (c) 2020 AOE GmbH <[email protected]>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Psr\Http\Server\MiddlewareInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
27
/**
28
 * Evaluates HTTP headers and checks if Crawler should register itself.
29
 * Needs to be run after TSFE initialization AND Frontend User Authentication.
30
 *
31
 * Once done, the queue is fetched, and then the frontend request runs through.
32
 *
33
 * Finally, at the very end, if the crawler is still running, output the data and replace the response.
34
 */
35
class CrawlerInitialization implements MiddlewareInterface
36
{
37
    /**
38
     * @param ServerRequestInterface $request
39
     * @param RequestHandlerInterface $handler
40
     * @return ResponseInterface
41
     * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
42
     * @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
43
     */
44
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
45
    {
46
        $queueParameters = $request->getAttribute('tx_crawler');
47
        if ($queueParameters === null) {
48
            return $handler->handle($request);
49
        }
50
51
        $GLOBALS['TSFE']->applicationData['tx_crawler']['running'] = true;
52
        $GLOBALS['TSFE']->applicationData['tx_crawler']['parameters'] = $queueParameters;
53
        $GLOBALS['TSFE']->applicationData['tx_crawler']['log'] = [];
54
        $GLOBALS['TSFE']->applicationData['tx_crawler']['log'] = [
55
            'User Groups: ' . $queueParameters['feUserGroupList'],
56
        ];
57
58
        // Execute the frontend request as is
59
        $handler->handle($request);
60
61
        $GLOBALS['TSFE']->applicationData['tx_crawler']['vars'] = [
62
            'id' => $GLOBALS['TSFE']->id,
63
            'gr_list' => implode(',', $this->context->getAspect('frontend.user')->getGroupIds()),
0 ignored issues
show
Bug Best Practice introduced by
The property context does not exist on AOE\Crawler\Middleware\CrawlerInitialization. Did you maybe forget to declare it?
Loading history...
64
            'no_cache' => $GLOBALS['TSFE']->no_cache,
65
        ];
66
67
        $this->runPollSuccessHooks();
68
69
        // Output log data for crawler (serialized content):
70
        $content = serialize($GLOBALS['TSFE']->applicationData['tx_crawler']);
71
        $response = new Response();
0 ignored issues
show
Bug introduced by
The type AOE\Crawler\Middleware\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
        $response->getBody()->write($content);
73
74
        return $response;
75
    }
76
77
    /**
78
     * Required because some extensions (staticpub) might never be requested to run due to some Core side effects
79
     * and since this is considered as error the crawler should handle it properly
80
     */
81
    protected function runPollSuccessHooks(): void
82
    {
83
        if (!is_array($GLOBALS['TSFE']->applicationData['tx_crawler']['content']['parameters']['procInstructions'])) {
84
            return;
85
        }
86
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'] ?? [] as $pollable) {
87
            if (in_array($pollable, $GLOBALS['TSFE']->applicationData['tx_crawler']['content']['parameters']['procInstructions'])) {
88
                if (empty($GLOBALS['TSFE']->applicationData['tx_crawler']['success'][$pollable])) {
89
                    $GLOBALS['TSFE']->applicationData['tx_crawler']['errorlog'][] = 'Error: Pollable extension (' . $pollable . ') did not complete successfully.';
90
                }
91
            }
92
        }
93
    }
94
}
95