Completed
Push — master ( 9be0de...4e104c )
by Enrico
06:20
created

ReadResourceController::read()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
crap 3.0175
1
<?php
2
3
namespace Resourceful;
4
5
use Silex\Application;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
9
use Doctrine\Common\Cache\Cache;
10
11
class ReadResourceController
12
{
13
	private $schemaId;
14
15 16
    public function __construct($schemaId)
16
    {
17 16
    	assert (!empty($schemaId));
18
		
19 16
    	$this->schemaId = $schemaId;
20 16
    }
21
22
23 15
	public function getSchemaId()
24
	{
25 15
		return $this->schemaId;
26
	}
27
28
	
29 14
	public function getDatastore($app)
30
	{
31 14
		$schemaId= $this->getSchemaId();
32 14
		return isset($app["$schemaId.store"])?$app["$schemaId.store"]:$app['data.store'];
33
	}
34
35
36 2
    public function read(Application $app, Request $request)
37
    {
38 2
    	$datastore = $this->getDatastore($app);
39 2
        if (!$datastore->contains($request->getRequestUri())) {
40 1
            throw new NotFoundHttpException("Not Found");
41
        }
42
43 1
        $resource = $datastore->fetch($request->getRequestUri());
44 1
        if ($resource === false) {
45
            throw new ServiceUnavailableHttpException(null, "Failed to retrieve resource");
46
        }
47
		
48 1
        return $app->json($resource);
49
    }
50
}
51