SwaggerUIAssets   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 10.17 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 6
loc 59
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 16 2
A getMimeType() 0 14 4
A getAssetsPath() 6 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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