SwaggerUIAssets::getAssetsPath()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 6
Ratio 60 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 6
loc 10
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace SwaggerMiddleware\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 Zend\Diactoros\Response;
10
11
class SwaggerUIAssets implements MiddlewareInterface
12
{
13
    const ALLOWED_FILES = [
14
        'favicon-16x16.png',
15
        'favicon-32x32.png',
16
        'oauth2-redirect.html',
17
        'swagger-ui-bundle.js',
18
        'swagger-ui-bundle.js.map',
19
        'swagger-ui-standalone-preset.js',
20
        'swagger-ui-standalone-preset.js.map',
21
        'swagger-ui.css',
22
        'swagger-ui.css.map',
23
        'swagger-ui.js',
24
        'swagger-ui.js.map',
25
    ];
26
27 9
    public function process(ServerRequestInterface $request, RequestHandlerInterface $delegate): ResponseInterface
28
    {
29 9
        $asset = $request->getAttribute('asset');
30
31 9
        if (!in_array($asset, self::ALLOWED_FILES, true)) {
32 3
            return $delegate->handle($request);
33
        }
34
35 6
        $file = $this->getAssetsPath() . $asset;
36
37 6
        return new Response(
38 6
            fopen($file, 'rb'),
39 6
            200,
40 6
            ['Content-Type' => $this->getMimeType($file)]
41
        );
42
    }
43
44 6
    private function getMimeType(string $file) : string
45
    {
46 6
        $extension = pathinfo($file)['extension'];
47 2
        switch ($extension) {
48 6
            case 'css':
49 6
                return 'text/css';
50 6
            case 'png':
51 6
                return 'image/png';
52 6
            case 'html':
53 6
                return 'text/html';
54
            default:
55 6
                return 'application/javascript';
56
        }
57
    }
58
59 6
    private function getAssetsPath()
60
    {
61 6 View Code Duplication
        if (file_exists(dirname(__DIR__, 4) . '/swagger-api/swagger-ui/dist/')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            return dirname(__DIR__, 4) . '/swagger-api/swagger-ui/dist/';
63
        }
64 6 View Code Duplication
        if (file_exists(dirname(__DIR__, 2) . '/vendor/swagger-api/swagger-ui/dist/')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65 6
            return dirname(__DIR__, 2) . '/vendor/swagger-api/swagger-ui/dist/';
66
        }
67
        throw new \RuntimeException('Unable to find SwaggerUI assets path.');
68
    }
69
}
70