Passed
Push — main ( 644501...e2679f )
by Felix
02:55
created

Dispatcher::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 4
b 0
f 0
nc 3
nop 2
dl 0
loc 18
ccs 3
cts 6
cp 0.5
crap 4.125
rs 9.9666
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\TYPO3\Loader;
29
use Aoe\Restler\System\Restler\Routes;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Server\MiddlewareInterface;
33
use Psr\Http\Server\RequestHandlerInterface;
34
use TYPO3\CMS\Core\Http\Response;
35
use TYPO3\CMS\Core\Http\Stream;
36
37
class Dispatcher extends RestlerBuilderAware implements MiddlewareInterface
38
{
39
    /**
40
     * Process an incoming server request.
41
     *
42
     * Processes an incoming server request in order to produce a response.
43
     * If unable to produce the response itself, it may delegate to the provided
44
     * request handler to do so.
45 1
     */
46
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
47 1
    {
48 1
        Loader::setRequest($request);
49
50 1
        if ($this->isRestlerPrefix($this->extractSiteUrl($request))) {
51
            $restlerObj = $this->getRestlerBuilder()->build($request);
52
53
            if ($this->isRestlerUrl('/' . $restlerObj->url)) {
54
                // wrap reponse into a stream to pass along to the rest of the Typo3 framework
55
                $body = new Stream('php://temp', 'wb+');
56
                $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...
57
                $body->rewind();
58
59
                return new Response($body, $restlerObj->responseCode);
60
            }
61
        }
62
63
        return $handler->handle($request);
64
    }
65
66
    private function isRestlerUrl($uri): bool
67
    {
68
        return Routes::containsUrl($uri);
69
    }
70
71
    protected function extractSiteUrl($request)
72
    {
73
        // set base path depending on site config
74
        $site = $request->getAttribute('site');
75
        if ($site !== null && $site instanceof \TYPO3\CMS\Core\Site\Entity\Site) {
76
            $siteBasePath = $request->getAttribute('site')->getBase()->getPath();
77
            if ($siteBasePath !== '/' && $siteBasePath[-1] !== '/') {
78
                $siteBasePath .= '/';
79
            }
80
        } else {
81
            $siteBasePath = '/';
82
        }
83 1
84
        // set url with base path removed
85
        return '/' . rtrim(preg_replace('%^' . preg_quote($siteBasePath, '%') . '%', '', $request->getUri()->getPath()), '/');
86 1
    }
87
}
88