Passed
Push — main ( 9da72c...4c09d2 )
by Felix
20:30 queued 11:53
created

Dispatcher::process()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 39
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 35.321

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 9
eloc 15
c 4
b 0
f 0
nc 9
nop 2
dl 0
loc 39
ccs 5
cts 16
cp 0.3125
crap 35.321
rs 8.0555
1
<?php
2
namespace Aoe\Restler\System;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2015 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\Restler\System\Restler\Routes;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\MiddlewareInterface;
32
use Psr\Http\Server\RequestHandlerInterface;
33
use TYPO3\CMS\Core\Http\Response;
34
use TYPO3\CMS\Core\Http\Stream;
35
36
class Dispatcher extends RestlerBuilderAware implements MiddlewareInterface
37
{
38
    /**
39
     * Process an incoming server request.
40
     *
41
     * Processes an incoming server request in order to produce a response.
42
     * If unable to produce the response itself, it may delegate to the provided
43
     * request handler to do so.
44
     */
45 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
46
    {
47 1
        if ($this->isRestlerPrefix($this->extractSiteUrl($request))) {
48 1
            $restlerObj = $this->getRestlerBuilder()->build($request);
49
50 1
            if ($this->isRestlerUrl('/' . $restlerObj->url)) {
51
                /**
52
                 * We might end up with a loaded TSFE->config but an empty
53
                 * TSFE->tmpl->setup. That is depending on the state of the caches.
54
                 * This in turn will lead to an empty extbase configuration.
55
                 * And this will lead to failures loading sys_file_reference
56
                 * as it will use the default tableName of tx_extbase_domain_model_filereference
57
                 */
58
                // See https://review.typo3.org/c/Packages/TYPO3.CMS/+/60713 for reasons
59
60
                // check for proper template config state
61
                if (!$GLOBALS['TSFE']->tmpl->loaded) {
62
                    if (empty($GLOBALS['TSFE']->rootLine) && !empty($GLOBALS['TSFE']->id)) {
63
                        $GLOBALS['TSFE']->determineId();
64
                        if ($GLOBALS['TSFE']->tmpl === null) {
65
                            $GLOBALS['TSFE']->getConfigArray();
66
                        }
67
                    }
68
69
                    if (!empty($GLOBALS['TSFE']->tmpl) && !empty($GLOBALS['TSFE']->rootLine)) {
70
                        $GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine);
71
                    }
72
                }
73
74
                // wrap reponse into a stream to pass along to the rest of the Typo3 framework
75
                $body = new Stream('php://temp', 'wb+');
76
                $body->write($restlerObj->handle());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $restlerObj->handle() targeting Aoe\Restler\System\Restl...stlerExtended::handle() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
77
                $body->rewind();
78
79
                return new Response($body, $restlerObj->responseCode);
80
            }
81
        }
82
83 1
        return $handler->handle($request);
84
    }
85
86 1
    private function isRestlerUrl($uri): bool
87
    {
88 1
        return Routes::containsUrl($uri);
89
    }
90
91 1
    protected function extractSiteUrl($request)
92
    {
93
        // set base path depending on site config
94 1
        $site = $request->getAttribute('site');
95 1
        if ($site !== null && $site instanceof \TYPO3\CMS\Core\Site\Entity\Site) {
96
            $siteBasePath = $request->getAttribute('site')->getBase()->getPath();
97
            if ($siteBasePath !== '/' && $siteBasePath[-1] !== '/') {
98
                $siteBasePath .= '/';
99
            }
100
        } else {
101 1
            $siteBasePath = '/';
102
        }
103
104
        // set url with base path removed
105 1
        return '/' . rtrim(preg_replace('%^' . preg_quote($siteBasePath, '%') . '%', '', $request->getUri()->getPath()), '/');
106
    }
107
}
108