Passed
Push — main ( 04a742...5b603f )
by
unknown
04:37 queued 12s
created

Dispatcher   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 37.04%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 26
c 8
b 0
f 0
dl 0
loc 57
ccs 10
cts 27
cp 0.3704
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 19 3
A extractSiteUrl() 0 22 6
A isRestlerUrl() 0 3 1
1
<?php
2
3
namespace Aoe\Restler\System;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2015 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use Aoe\Restler\System\TYPO3\Loader;
30
use Aoe\Restler\System\Restler\Routes;
31
use Psr\Http\Message\ResponseInterface;
32
use Psr\Http\Message\ServerRequestInterface;
33
use Psr\Http\Server\MiddlewareInterface;
34
use Psr\Http\Server\RequestHandlerInterface;
35
use TYPO3\CMS\Core\Http\Response;
36
use TYPO3\CMS\Core\Http\Stream;
37
38
class Dispatcher extends RestlerBuilderAware implements MiddlewareInterface
39
{
40
    /**
41
     * Process an incoming server request.
42
     *
43
     * Processes an incoming server request in order to produce a response.
44
     * If unable to produce the response itself, it may delegate to the provided
45
     * request handler to do so.
46
     */
47 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
48
    {
49 1
        Loader::setRequest($request);
50
51 1
        if ($this->isRestlerPrefix($this->extractSiteUrl($request))) {
52
            $restlerObj = $this->getRestlerBuilder()
53
                ->build($request);
54
55
            if ($this->isRestlerUrl('/' . $restlerObj->url)) {
56
                // wrap reponse into a stream to pass along to the rest of the Typo3 framework
57
                $body = new Stream('php://temp', 'wb+');
58
                $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...
59
                $body->rewind();
60
61
                return new Response($body, $restlerObj->responseCode);
62
            }
63
        }
64
65 1
        return $handler->handle($request);
66
    }
67
68 1
    protected function extractSiteUrl(ServerRequestInterface $request): string
69
    {
70
        // set base path depending on site config
71 1
        $site = $request->getAttribute('site');
72 1
        if ($site instanceof \TYPO3\CMS\Core\Site\Entity\Site) {
73
            $siteBasePath = $request->getAttribute('site')
74
                ->getBase()
75
                ->getPath();
76
            if ($siteBasePath === '/' || $siteBasePath === '') {
77
                $siteBasePath = null;
78
            } else if ($siteBasePath[-1] !== '/') {
79
                $siteBasePath .= '/';
80
            }
81
        } else {
82 1
            $siteBasePath = null;
83
        }
84
85 1
        if ($siteBasePath) {
86
            return '/' . rtrim(preg_replace('%^' . preg_quote($siteBasePath, '%') . '%', '', $request->getUri()->getPath()), '/');
87
        }
88
89 1
        return $request->getUri()->getPath();
90
    }
91
92
    private function isRestlerUrl(string $uri): bool
93
    {
94
        return Routes::containsUrl($uri);
95
    }
96
}
97