ReadResourceController::getDatastore()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
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 17
    public function __construct($schemaId)
16
    {
17 17
    	assert (!empty($schemaId));
18
		
19 17
    	$this->schemaId = $schemaId;
20 17
    }
21
22
23 16
	public function getSchemaId()
24
	{
25 16
		return $this->schemaId;
26
	}
27
28
	
29 15
	public function getDatastore($app)
30
	{
31 15
		$schemaId= $this->getSchemaId();
32 15
		return isset($app["$schemaId.store"])?$app["$schemaId.store"]:$app['data.store'];
33
	}
34
35
36 3
    public function read(Application $app, Request $request)
37
    {
38 3
    	$datastore = $this->getDatastore($app);
39 3
        if (!$datastore->contains($request->getRequestUri())) {
40 1
            throw new NotFoundHttpException("Not Found");
41
        }
42
43 2
        $resource = $datastore->fetch($request->getRequestUri());
44 2
        if ($resource === false) {
45 1
            throw new ServiceUnavailableHttpException(null, "Failed to retrieve resource");
46
        }
47
		
48 1
        return $app->json($resource);
49
    }
50
}
51