Completed
Push — master ( 5b1884...46f07a )
by Ole
04:10
created

SwaggerProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 0 30 1
A boot() 0 12 1
1
<?php
2
3
namespace Basster\Silex\Provider\Swagger;
4
5
use Silex\Application;
6
use Silex\ServiceProviderInterface;
7
use Swagger\Annotations as SWG;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class SwaggerProvider implements ServiceProviderInterface, SwaggerServiceKey
11
{
12
    /** {@inheritdoc} */
13
    public function register(Application $app)
14
    {
15
        $app[self::SWAGGER_API_DOC_PATH] = '/api/api-docs';
16
        $app[self::SWAGGER_EXCLUDE_PATH] = null;
17
        $app[self::SWAGGER_CACHE]        = [];
18
        $app[self::SWAGGER_SERVICE_PATH] = null;
19
        $app[self::SWAGGER_ANALYSER]     = null;
20
        $app[self::SWAGGER_ANALYSIS]     = null;
21
        $app[self::SWAGGER_PROCESSORS]   = [];
22
23
        $app[self::SWAGGER_CONFIG] = $app::share(function (Application $app) {
24
            return new SwaggerConfig(
25
              $app[self::SWAGGER_SERVICE_PATH],
26
              $app[self::SWAGGER_ANALYSER],
27
              $app[self::SWAGGER_ANALYSIS],
28
              $app[self::SWAGGER_PROCESSORS],
29
              $app[self::SWAGGER_EXCLUDE_PATH]
30
            );
31
        });
32
33
        $app[self::SWAGGER_SERVICE] = $app::share(function (Application $app) {
34
            return new SwaggerService($app[self::SWAGGER_CONFIG]);
35
        });
36
37
        $app[self::SWAGGER] = $app::share(function (Application $app) {
38
            /** @var SwaggerService $swagger */
39
            $swagger = $app[self::SWAGGER_SERVICE];
40
            return $swagger->getSwagger();
41
        });
42
    }
43
44
    /** {@inheritdoc} */
45
    public function boot(Application $app)
46
    {
47
        $app->get($app[self::SWAGGER_API_DOC_PATH],
48
          function (Application $app, Request $request) {
49
              /** @var SwaggerService $swagger */
50
              $swagger  = $app[self::SWAGGER_SERVICE];
51
              $response = $swagger->getSwaggerResponse($app[self::SWAGGER_CACHE]);
52
              $response->isNotModified($request);
53
54
              return $response;
55
          });
56
    }
57
}
58