Completed
Push — master ( 5c8e3c...73346b )
by Enrico
02:14
created

IndexControllerProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 26
ccs 14
cts 15
cp 0.9333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A connect() 0 16 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)
23
    {
24 1
        $schema = $app["url_generator"]->generate("schema", array("type" => "index"));
25 1
        $resource = $app["resources_factory"]($schema);
26 1
        $app["twig.loader"]->addLoader(new Twig_Loader_Filesystem(__DIR__ . "/templates"));
27 1
        $app->before(new AddSchema($schema, "index"));
28
        // Generate default Index resource
29 1
        $resource->before(function (Request $request, Application $app) {
30 1
            $index = $app["url_generator"]->generate("index");
31 1
            if (!$this->service->contains($index)) {
32
                $this->service->save($index, json_decode($app["twig"]->render("default.json.twig")));
33
            }
34 1
        });
35 1
        $resource->get("/", new GetResourceController($this->service))->bind("index");
36 1
        return $resource;
37
    }
38
}
39