Passed
Push — master ( eda005...2d3321 )
by Andrey
02:01
created

ResourceDefinitionController::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 3
dl 0
loc 24
rs 9.7
c 0
b 0
f 0
ccs 17
cts 17
cp 1
crap 2
1
<?php
2
3
namespace JDesrosiers\Silex\Provider;
4
5
use Silex\Application;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
10
class ResourceDefinitionController
11
{
12
    /**
13
     * Route for GETting each of the resource definitions
14
     *
15
     * @param Application $app
16
     * @param Request $request
17
     * @param string $service
18
     *
19
     * @return Response
20
     * @throws NotFoundHttpException
21
     */
22 6
    public function __invoke(Application $app, Request $request, $service)
23
    {
24 6
        $resourceName = "/" . str_replace("-", "/", $service);
25 6
        $resourceNames = $app["swagger"]->getResourceNames();
26 6
        if (!in_array($resourceName, $resourceNames)) {
27 6
            $resourceNamesDisplay = implode('", "', $resourceNames);
28 6
            throw new NotFoundHttpException("Resource \"$resourceName\" not found, try \"$resourceNamesDisplay\"");
29
        }
30
31
        $options = array(
32 6
            "output" => "json",
33 6
            "json_pretty_print" => $app["swagger.prettyPrint"],
34 6
            "defaultBasePath" => $app["swagger.basePath"],
35 6
            "defaultApiVersion" => $app["swagger.apiVersion"],
36 6
            "defaultSwaggerVersion" => $app["swagger.swaggerVersion"],
37
        );
38 6
        $json = $app["swagger"]->getResource($resourceName, $options);
39
40 6
        $response = Response::create($json, 200, array("Content-Type" => "application/json"));
41 6
        $response->setCache($app["swagger.cache"]);
42 6
        $response->setEtag(md5($json));
43 6
        $response->isNotModified($request);
44
45 6
        return $response;
46
    }
47
}