Completed
Push — master ( cc1718...5a6c69 )
by Harm
08:30
created

DocsController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 0
loc 123
ccs 42
cts 49
cp 0.8571
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 15 2
A redirectAction() 0 12 2
B swaggerFileAction() 0 23 4
B getFilePath() 0 25 5
A getRedirectUrlToSpec() 0 15 3
1
<?php
2
3
namespace HarmBandstra\SwaggerUiBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12
use Symfony\Component\Yaml\Yaml;
13
14
class DocsController extends Controller
15
{
16
    /**
17
     * @param Request $request
18
     *
19
     * @return Response
20
     */
21 1
    public function indexAction(Request $request)
22
    {
23 1
        if (!$request->get('url')) {
24
            // if there is no ?url=... parameter, redirect to the default one
25 1
            $specFiles = $this->container->getParameter('hb_swagger_ui.files');
26
27 1
            $defaultSpecFile = reset($specFiles);
28
29 1
            return $this->redirect($this->getRedirectUrlToSpec($defaultSpecFile));
30
        }
31
32
        $indexFilePath = $this->container->getParameter('kernel.root_dir') . '/../vendor/swagger-api/swagger-ui/dist/index.html';
33
34
        return new Response(file_get_contents($indexFilePath));
35
    }
36
37
    /**
38
     * @param Request $request
39
     * @param string $fileName
40
     *
41
     * @return RedirectResponse
42
     */
43 1
    public function redirectAction(Request $request, $fileName)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45 1
        $validFiles = $this->container->getParameter('hb_swagger_ui.files');
46
47
        // redirect to swagger file if that's what we're looking for
48 1
        if (in_array($fileName, $validFiles, true)) {
49 1
            return $this->redirect($this->getRedirectUrlToSpec($fileName));
50
        }
51
52
        // redirect to the assets dir so that relative links work
53
        return $this->redirect('/bundles/hbswaggerui/' . $fileName);
54
    }
55
56
    /**
57
     * @param string $fileName
58
     *
59
     * @return JsonResponse|Response
60
     */
61 4
    public function swaggerFileAction($fileName)
62
    {
63
        try {
64 4
            $filePath = $this->getFilePath($fileName);
65 4
        } catch (\Exception $e) {
66 2
            return new JsonResponse($e->getMessage(), Response::HTTP_NOT_FOUND);
67
        }
68
69 2
        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
70 2
        if ($extension === 'yml' || $extension === 'yaml') {
71 1
            $fileContents = Yaml::parse(file_get_contents($filePath));
72
73 1
            return new JsonResponse($fileContents);
74
        }
75
76 1
        $fileContents = file_get_contents($filePath);
77
78 1
        return new Response(
79 1
            $fileContents,
80 1
            Response::HTTP_OK,
81 1
            ['Content-Type' => 'application/json']
82 1
        );
83
    }
84
85
    /**
86
     * @param string $fileName
87
     *
88
     * @return string
89
     */
90 4
    private function getFilePath($fileName = '')
91
    {
92 4
        $validFiles = $this->container->getParameter('hb_swagger_ui.files');
93
94 4
        if ($fileName !== '' && !in_array($fileName, $validFiles)) {
95 1
            throw new \RuntimeException(
96 1
                sprintf('File [%s] not defined under [hb_swagger_ui.files] in config.yml.', $fileName)
97 1
            );
98
        }
99
100 3
        $directory = $this->container->getParameter('hb_swagger_ui.directory');
101
102 3
        if ($directory === '') {
103
            throw new \RuntimeException(
104
                'Directory [hb_swagger_ui.directory] not defined or empty in config.yml.'
105
            );
106
        }
107
108 3
        $filePath = realpath($this->container->getParameter('hb_swagger_ui.directory') . DIRECTORY_SEPARATOR . $fileName);
109 3
        if (!is_file($filePath)) {
110 1
            throw new FileNotFoundException(sprintf('File [%s] not found.', $fileName));
111
        }
112
113 2
        return $filePath;
114
    }
115
116
    /**
117
     * @param string $fileName
118
     *
119
     * @return string
120
     */
121 2
    private function getRedirectUrlToSpec($fileName)
122
    {
123 2
        if (strpos($fileName, '/') === 0 || preg_match('#http[s]?://#', $fileName)) {
124
            // if absolute path or URL use it raw
125
            $specUrl = $fileName;
126
        } else {
127 2
            $specUrl = $this->generateUrl(
128 2
                'hb_swagger_ui_swagger_file',
129 2
                ['fileName' => $fileName],
130
                UrlGeneratorInterface::ABSOLUTE_PATH
131 2
            );
132
        }
133
134 2
        return $this->generateUrl('hb_swagger_ui_default', ['url' => $specUrl]);
135
    }
136
}
137