CRUDResourceController::create()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 16
nc 18
nop 2
crap 6
1
<?php
2
3
namespace Resourceful;
4
5
use Silex\Application;
6
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
10
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
11
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
12
use Jsv4;
13
14
class CRUDResourceController extends ReadResourceController
15
{
16
17
	/**
18
	 * @return string $schemaUrl
19
	 */
20 9
    protected function validate(Application $app, $resourceId, \stdClass $data)
21
    {
22 9
    	assert( isset($app["schema.cache"]));
23
		
24 9
		if (!isset($data->id) || ($resourceId !== $data->id)) {
25 1
            throw new BadRequestHttpException("The `id` in the body must match the `id` in the URI");
26
        }
27
		
28 8
		$schemaId=$this->getSchemaId();
29
    	
30
31 8
		$schemaUrl = SchemaHandler::getSchemaUrl($schemaId, $app);
32 8
        $schema = $app["schema.cache"]->get($schemaUrl);
33 8
        $validation = Jsv4::validate($data, $schema);
34 8
        if (!$validation->valid) {
35
            throw new BadRequestHttpException(json_encode($validation->errors));
36
        }
37
		
38 8
		return $schemaUrl;
39
    }
40
	
41
	
42 5
    public function create(Application $app, Request $request)
43
    {
44 5
    	assert( isset($app['uniqid']));
45
		
46 5
		$schemaId=$this->getSchemaId();
47 5
		$datastore = $this->getDatastore($app);
48
49 5
        $requestJson = $request->getContent()?:"{}";
50 5
        $data = json_decode($requestJson);
51 5
		if(!isset($data->id)) {
52 1
			$uniqudFunction = isset($app["$schemaId.uniqid"])?$app["$schemaId.uniqid"]:$app["uniqid"];
53 1
			$data->id = $uniqudFunction($data);
54
		}
55 5
        $this->validate($app, $data->id, $data);
56
57 5
        $location = $app["url_generator"]->generate($schemaId, array("id" => $data->id));
58 4
		if ($datastore->contains($location)){
59 1
			throw new ConflictHttpException("Sorry $location already exists.");
60 3
		} elseif ($datastore->save($location, $data) === false) {
61 1
            throw new ServiceUnavailableHttpException(null, "Failed to save resource");
62
        }
63
64 2
        return $app->json($data, 201, array("Location" => $location));
65
    }
66
67
	
68 4
    public function update(Application $app, Request $request, $id)
69
    {
70 4
        $requestJson = $request->getContent() ?: "{}";
71 4
        $data = json_decode($requestJson);
72 4
        $this->validate($app, $id, $data);
73 3
		$datastore = $this->getDatastore($app);
74
75 3
		$requestUri = $request->getRequestUri();
76 3
        $isCreated = !$datastore->contains($requestUri);
77 3
        if ($datastore->save($requestUri, $data) === false) {
78 1
            throw new ServiceUnavailableHttpException(null, "Failed to save resource");
79
        }
80
81 2
		return $app->json($data,$isCreated?201:200);
82
    }
83
	
84
	
85 2
    public function delete(Application $app, Request $request)
86
    {
87 2
		$datastore = $this->getDatastore($app);	
88 2
        if ($datastore->delete($request->getRequestURI()) === false) {
89 1
            throw new ServiceUnavailableHttpException(null, "Failed to delete resource");
90
        }
91
92 1
        return Response::create('', Response::HTTP_NO_CONTENT);
93
    }
94
}
95
96