ApieRouteLoader::renderContext()   A
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 25
nc 10
nop 1
dl 0
loc 36
rs 9.2088
c 1
b 0
f 0
1
<?php
2
3
4
namespace W2w\Laravel\Apie\Services;
5
6
use W2w\Laravel\Apie\Middleware\HandleAcceptLanguage;
7
8
class ApieRouteLoader
9
{
10
    private $context;
11
12
    private $routeLoader;
13
14
    public function __construct(ApieContext $context, RouteLoaderInterface $routeLoader) {
15
        $this->context = $context;
16
        $this->routeLoader = $routeLoader;
17
    }
18
19
    public function renderRoutes()
20
    {
21
        $this->renderContext($this->context);
22
    }
23
24
    private function renderContext(ApieContext $context)
25
    {
26
        if (!$context->getConfig('disable-routes')) {
27
            $this->routeLoader->context(
28
                $context->getContextKey(),
29
                $context->getConfig('api-url'),
30
                $context->getConfig('swagger-ui-test-page-middleware'),
31
                function () use ($context) {
32
                    $this->routeLoader->addDocUrl($context->getContextKey());
33
                }
34
            );
35
            $middleware = $context->getConfig('apie-middleware');
36
            if ($context->getConfig('translations')) {
37
                array_unshift($middleware, HandleAcceptLanguage::class. ':' . implode(',', $context->getConfig('translations')));
38
            }
39
            $this->routeLoader->context(
40
                $context->getContextKey(),
41
                $context->getConfig('api-url'),
42
                $middleware,
43
                function () use ($context) {
44
                    $this->routeLoader->addResourceUrl($context->getContextKey());
45
                }
46
            );
47
            if ($context->getConfig('swagger-ui-test-page')) {
48
                $this->routeLoader->context(
49
                    $context->getContextKey(),
50
                    $context->getConfig('swagger-ui-test-page'),
51
                    $context->getConfig('swagger-ui-test-page-middleware'),
52
                    function () use ($context) {
53
                        $this->routeLoader->addSwaggerUiUrl($context->getContextKey());
54
                    }
55
                );
56
            }
57
        }
58
        foreach ($context->allContexts() as $childContext) {
59
            $this->renderContext($childContext);
60
        }
61
    }
62
}
63