ServiceProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 10
dl 0
loc 69
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 0 46 1
A boot() 0 15 2
1
<?php
2
3
namespace Resourceful;
4
5
use Pimple\Container;
6
use Pimple\ServiceProviderInterface;
7
use Silex\Provider\ServiceControllerServiceProvider;
8
use Silex\Provider\TwigServiceProvider;
9
use Silex\Application;
10
use Silex\Api\BootableProviderInterface;
11
use Twig_Loader_Filesystem;
12
use SchemaStore;
13
use JDesrosiers\Silex\Provider\ContentNegotiationServiceProvider;
14
use JDesrosiers\Silex\Provider\CorsServiceProvider;
15
use Resourceful\Stores\FileCache;
16
use Symfony\Component\Debug\ErrorHandler;
17
18
class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface
19
{
20
    const ERROR_HANDLER_PRIORITY = 0;
21
22
23 22
    public function register(Container $app)
24
    {
25 22
    	assert($app instanceof Application);
26
		
27 22
    	$app->register(new TwigServiceProvider());
28
		
29
		// if true, create schema from templates
30 22
		$app["createDefault"] = true;
31
		
32 22
		$app['data.dir'] = sys_get_temp_dir() . '/resourceful';
33 22
		$app['resourceful.templates.dir'] = __DIR__ . "/../templates";
34
		
35
		// create a storage service for data and schema
36
		$app['data.store'] = function($app) {
37
			return new FileCache($app['data.dir']);
38
		};
39
		
40
        // JSON Schema application
41
        $app["schema.cache"] = function () {
42 8
            return new SchemaStore();
43
        };
44
45 22
        $app["uniqid"] = $app->protect(function ($data) {
46
            return uniqid();
47 22
        });
48
				
49
    	// JSON/REST application
50 22
        $app->register(new ContentNegotiationServiceProvider(), array(
51 22
            "conneg.responseFormats" => array("json"),
52
            "conneg.requestFormats" => array("json"),
53
            "conneg.defaultFormat" => "json",
54
        ));
55
		
56
		// cors support
57 22
        $app->register(new CorsServiceProvider());
58
		
59
		// allow to create controllers as a service 
60 22
		$app->register(new ServiceControllerServiceProvider());
61
62
		//=====================================================		
63
		// set application middleware
64
		//=====================================================	
65
		
66
		// manage cors
67 22
		$app->after($app["cors"]);
68 22
    }
69
70
71 2
    public function boot(Application $app)
72
    {
73 2
    	assert( isset($app["twig.loader"]));
74
75
		// ensure that schema route exists
76 2
		if( !isset($app['schema.controller'])) {
77 1
    		$app->abort(500,'Schema endpoint not found. Do you have mounted a schema?');
78
    	}
79
				
80 1
		$app["twig.loader"]->addLoader(new Twig_Loader_Filesystem($app['resourceful.templates.dir']));
81
82
		// Error Handling
83 1
        ErrorHandler::register();
84 1
        $app->error(new JsonErrorHandler($app), self::ERROR_HANDLER_PRIORITY);
85 1
    }
86
}
87