Completed
Pull Request — master (#125)
by
unknown
12:47
created

DefaultController::swaggerUiAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 23
ccs 0
cts 0
cp 0
crap 12
rs 9.0856
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Form\Type\FormLoginType;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Session\Session;
11
use Symfony\Component\Security\Core\Security;
12
13
class DefaultController extends Controller
14
{
15
    /**
16
     * @param  Request $request
17
     * @return \Symfony\Component\HttpFoundation\Response
18
     * @SuppressWarnings(PHPMD.ElseExpression)
19
     */
20 22
    public function loginAction(Request $request)
21
    {
22
        /** @var Session $session */
23 22
        $session = $this->get('session');
24
25 22
        $form = $this->createForm(new FormLoginType());
26
27 22
        if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
28
            $error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
29
        } else {
30 22
            $error = $session->get(Security::AUTHENTICATION_ERROR);
31 22
            $session->remove(Security::AUTHENTICATION_ERROR);
32
        }
33
34 22
        return $this->render(':default:login.html.twig', [
35 22
            'error' => $error,
36 22
            'form'  => $form->createView(),
37
        ]);
38
    }
39
40
    /**
41
     * @return \Symfony\Component\HttpFoundation\Response
42
     */
43
    public function swaggerUiAction()
44
    {
45
        $docUrl = $this->get('service_container')->getParameter('app_swagger_ui_resource_url');
46
47
        if (preg_match('/^(https?:)?\/\//', $docUrl)) {
48
            // If https://..., http://..., or //...
49
            $url = $docUrl;
50
        } elseif (strpos($docUrl, '/') === 0) {
51
            //If starts with "/", interpret as an asset.
52
            $url = $this->get('templating.helper.assets')->getUrl($docUrl);
53
        } else {
54
            // else, interpret as route-name.
55
            $url = $this->generateUrl($docUrl, [
56
                'resource' => $this->get('service_container')->getParameter('app_swagger_ui_static_resource_filename')
57
            ]);
58
        }
59
60
        $url = rtrim($url, '/');
61
62
        return $this->render(':default:swagger_ui.html.twig', [
63
            'resource_url' => $url
64
        ]);
65
    }
66
67
    /**
68
     * @param string $resource
69
     * @return JsonResponse
70
     */
71
    public function swaggerUiStaticResourceAction($resource)
72
    {
73
        $dir = $this->get('kernel')->getRootDir() . '/../' . $this->get('service_container')->getParameter('app_swagger_ui_static_resource_dir');
74
75
        try {
76
            $finder = new Finder();
77
            $files = $finder->in($dir)->files()->name($resource);
78
79
            if (count($files) === 0){
80
                throw new \Exception(sprintf('Cannot find API Documentation: %s', $resource));
81
            }
82
83
            $doc = file_get_contents($dir . $resource);
84
85
            return new JsonResponse(json_decode($doc));
86
        } catch (\Exception $e) {
87
            throw $this->createNotFoundException($e->getMessage());
88
        }
89
    }
90
}
91