Completed
Push — master ( e78b78...0489ac )
by Vytautas
01:27
created

SwaggerUIAssets::getMimeType()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
crap 4
1
<?php
2
3
namespace SwaggerMiddleware\Middleware;
4
5
use Interop\Http\ServerMiddleware\DelegateInterface;
6
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Zend\Diactoros\Response;
9
10
class SwaggerUIAssets implements ServerMiddlewareInterface
11
{
12
    const ALLOWED_FILES = [
13
        'favicon-16x16.png',
14
        'favicon-32x32.png',
15
        'oauth2-redirect.html',
16
        'swagger-ui-bundle.js',
17
        'swagger-ui-bundle.js.map',
18
        'swagger-ui-standalone-preset.js',
19
        'swagger-ui-standalone-preset.js.map',
20
        'swagger-ui.css',
21
        'swagger-ui.css.map',
22
        'swagger-ui.js',
23
        'swagger-ui.js.map',
24
    ];
25
26 9
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
27
    {
28 9
        $asset = $request->getAttribute('asset');
29
30 9
        if (!in_array($asset, self::ALLOWED_FILES, true)) {
31 3
            return $delegate->process($request);
32
        }
33
34 6
        $file = $this->getAssetsPath() . $asset;
35
36 6
        return new Response(
37 6
            fopen($file, 'rb'),
38 6
            200,
39 6
            ['Content-Type' => $this->getMimeType($file)]
40
        );
41
    }
42
43 6
    private function getMimeType(string $file) : string
44
    {
45 6
        $extension = pathinfo($file)['extension'];
46 2
        switch ($extension) {
47 4
            case 'css':
48 6
                return 'text/css';
49 4
            case 'png':
50 6
                return 'image/png';
51 4
            case 'html':
52 6
                return 'text/html';
53
            default:
54 6
                return 'application/javascript';
55
        }
56
    }
57
58 6
    private function getAssetsPath()
59
    {
60 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...
61
            return dirname(__DIR__, 4) . '/swagger-api/swagger-ui/dist/';
62
        }
63 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...
64 6
            return dirname(__DIR__, 2) . '/vendor/swagger-api/swagger-ui/dist/';
65
        }
66
        throw new \RuntimeException('Unable to find SwaggerUI assets path.');
67
    }
68
}
69