| Conditions | 6 |
| Paths | 12 |
| Total Lines | 27 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 15 |
| CRAP Score | 6 |
| Changes | 0 | ||
| 1 | <?php |
||
| 14 | 5 | public function __invoke(Application $app, Request $request) |
|
| 15 | { |
||
| 16 | 5 | $store = $this->getStore($app); |
|
| 17 | |||
| 18 | 5 | $requestJson = $request->getContent() ?: "{}"; |
|
| 19 | 5 | $data = json_decode($requestJson); |
|
| 20 | 5 | if(!isset($data->id)) { |
|
| 21 | 1 | $data->id = $app["uniqid"]; |
|
| 22 | 1 | $requiredUniquenessTesting = false; |
|
| 23 | } else { |
||
| 24 | 4 | $requiredUniquenessTesting = true; |
|
| 25 | } |
||
| 26 | |||
| 27 | 5 | $this->validate($app, $data->id, $data); |
|
| 28 | |||
| 29 | 5 | $location = $app["url_generator"]->generate($this->schema, array("id" => $data->id)); |
|
| 30 | |||
| 31 | 4 | if ($requiredUniquenessTesting && $store->contains($location)){ |
|
| 32 | 1 | throw new ConflictHttpException("Sorry $location already exists."); |
|
| 33 | } |
||
| 34 | |||
| 35 | 3 | if ($store->save($location, $data) === false) { |
|
| 36 | 1 | throw new ServiceUnavailableHttpException(null, "Failed to save resource"); |
|
| 37 | } |
||
| 38 | |||
| 39 | 2 | return $app->json($data, 201, array("Location" => $location)); |
|
| 40 | } |
||
| 41 | } |
||
| 42 |