Passed
Pull Request — master (#198)
by Alexander
02:21
created

SubFolder::process()   C

Complexity

Conditions 12
Paths 74

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 29
c 1
b 0
f 0
nc 74
nop 2
dl 0
loc 46
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Yiisoft\Yii\Web\Middleware;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
use Yiisoft\Aliases\Aliases;
10
use Yiisoft\Router\UrlGeneratorInterface;
11
use Yiisoft\Yii\Web\Exception\BadUriPrefixException;
12
13
/**
14
 * This middleware supports routing when webroot is not the same folder as public
15
 */
16
final class SubFolder implements MiddlewareInterface
17
{
18
    public ?string $prefix = null;
19
    private UrlGeneratorInterface $uriGenerator;
20
    private Aliases $aliases;
21
22
    public function __construct(UrlGeneratorInterface $uriGenerator, Aliases $aliases)
23
    {
24
        $this->uriGenerator = $uriGenerator;
25
        $this->aliases = $aliases;
26
    }
27
28
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
29
    {
30
        $uri = $request->getUri();
31
        $path = $uri->getPath();
32
        $prefix = $this->prefix;
33
        $auto = $prefix === null;
34
        $length = $auto ? 0 : strlen($prefix);
35
36
        if ($auto) {
37
            // automatically checks that the project is in a subfolder
38
            // and URI contains a prefix
39
            $scriptName = $request->getServerParams()['SCRIPT_NAME'];
40
            if (strpos($scriptName, '/', 1) !== false) {
41
                $tmpPrefix = substr($scriptName, 0, strrpos($scriptName, '/'));
42
                if (strpos($path, $tmpPrefix) === 0) {
43
                    $prefix = $tmpPrefix;
44
                    $length = strlen($prefix);
45
                }
46
            }
47
        } elseif ($length > 0) {
48
            if ($prefix[-1] === '/') {
49
                throw new BadUriPrefixException('Wrong URI prefix value');
50
            }
51
            if (strpos($path, $prefix) !== 0) {
52
                throw new BadUriPrefixException('URI prefix does not match');
53
            }
54
        }
55
56
        if ($length > 0) {
57
            $newPath = substr($path, $length);
58
            if ($newPath === '') {
59
                $newPath = '/';
60
            }
61
            if ($newPath[0] !== '/') {
62
                if (!$auto) {
63
                    throw new BadUriPrefixException('URI prefix does not match completely');
64
                }
65
            } else {
66
                $request = $request->withUri($uri->withPath($newPath));
67
                $this->uriGenerator->setUriPrefix($prefix);
0 ignored issues
show
Bug introduced by
It seems like $prefix can also be of type null; however, parameter $name of Yiisoft\Router\UrlGenera...terface::setUriPrefix() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
                $this->uriGenerator->setUriPrefix(/** @scrutinizer ignore-type */ $prefix);
Loading history...
68
                // rewrite alias
69
                $this->aliases->set('@web', $prefix . '/');
70
            }
71
        }
72
73
        return $handler->handle($request);
74
    }
75
}
76