Completed
Push — master ( 70d014...b7bac5 )
by Enrico
02:36
created

IndexControllerProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 25
ccs 13
cts 14
cp 0.9286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A connect() 0 15 2
1
<?php
2
3
namespace Resourceful\IndexControllerProvider;
4
5
use Doctrine\Common\Cache\Cache;
6
use Resourceful\Controller\GetResourceController;
7
use Resourceful\ResourcefulServiceProvider\AddSchema;
8
use Silex\Api\ControllerProviderInterface;
9
use Silex\Application;
10
use Symfony\Component\HttpFoundation\Request;
11
use Twig_Loader_Filesystem;
12
13
class IndexControllerProvider implements ControllerProviderInterface
14
{
15
    private $service;
16
17 1
    public function __construct(Cache $service)
18
    {
19 1
        $this->service = $service;
20 1
    }
21
22 1
    public function connect(Application $app, $schema='/schema/index')
23
    {
24 1
        $resource = $app["resources_factory"]($schema);
25 1
        $app["twig.loader"]->addLoader(new Twig_Loader_Filesystem(__DIR__ . "/templates"));
26 1
        $app->before(new AddSchema($schema, "index"));
27
        // Generate default Index resource
28 1
        $resource->before(function (Request $request, Application $app) {
29 1
            $index = $app["url_generator"]->generate("index");
30 1
            if (!$this->service->contains($index)) {
31
                $this->service->save($index, json_decode($app["twig"]->render("default.json.twig")));
32
            }
33 1
        });
34 1
        $resource->get("/", new GetResourceController($this->service))->bind("index");
35 1
        return $resource;
36
    }
37
}
38