Completed
Pull Request — master (#125)
by Serhii
14:40
created

DefaultController::swaggerUiAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

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 9
cts 9
cp 1
crap 3
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 1
    public function swaggerUiAction()
44
    {
45 1
        $docUrl = $this->get('service_container')->getParameter('app_swagger_ui_resource_url');
46
47 1
        if (preg_match('/^(https?:)?\/\//', $docUrl)) {
48
            // If https://..., http://..., or //...
49
            $url = $docUrl;
50 1
        } 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 1
            $url = $this->generateUrl($docUrl, [
56 1
                'resource' => $this->get('service_container')->getParameter('app_swagger_ui_static_resource_filename')
57
            ]);
58
        }
59
60 1
        $url = rtrim($url, '/');
61
62 1
        return $this->render(':default:swagger_ui.html.twig', [
63 1
            'resource_url' => $url
64
        ]);
65
    }
66
67
    /**
68
     * @param string $resource
69
     * @return JsonResponse
70
     */
71
    public function swaggerUiStaticResourceAction($resource)
72
    {
73
        $dir = sprintf(
74
            '%s/../%s',
75
            $this->get('kernel')->getRootDir(),
76
            $this->get('service_container')->getParameter('app_swagger_ui_static_resource_dir')
77
        );
78
79
        try {
80
            $finder = new Finder();
81
            $files = $finder->in($dir)->files()->name($resource);
82
83
            if (count($files) === 0) {
84
                throw new \Exception(sprintf('Cannot find API Documentation: %s', $resource));
85
            }
86
87
            $doc = file_get_contents($dir . $resource);
88
89
            return new JsonResponse(json_decode($doc));
90
        } catch (\Exception $e) {
91
            throw $this->createNotFoundException($e->getMessage());
92
        }
93
    }
94
}
95